C# learning related series of data type class definitions (1)

1. Definition of class

        A class in C# is a reference type used to represent objects with state and behavior. Classes can contain fields, properties, methods, events, and other members.

public class MyClass  
{  
    // 类的生员简义  
}

Commonly used keywords are as follows:

1. public is an access modifier used to specify the access level of class members. When a class member is declared public, it means that the member can be accessed from anywhere in the program. (Accessible to all

2. private is an access modifier used to specify the access level of class members. When a class member is declared private, it means that the member can only be accessed within the current class and cannot be accessed by other classes. (Only accessible within this category)

3. protected is an access modifier used to specify the access level of class members. When a class member is declared protected, it means that the member can only be accessed in the current class and its subclasses, and cannot be accessed by other classes. (Can only be accessed in subclasses and parent classes)

4. internal is an access modifier used to specify the access level of class members. When a class member is declared as internal, it means that the member can only be accessed in the current assembly and cannot be accessed by other assemblies. (It can only be accessed in this project, and projects that reference the dll cannot access)

2. Members within the class

1. Fields: Fields are private or public members of a class and are used to store class data.

public class MyClass  
{  
    private int myField; // 私有字段  
    public int MyPublicField; // 公共字段  
}

2. Properties (Properties): Properties are a means of encapsulating a class, providing methods to access the fields of the class, and can also be accessed as data members.

public class MyClass  
{  
    private int myField;  
    public int MyProperty { get; set; } // 自动属性  
      
    public int MyCustomProperty  
    {  
        get { return myField; }  
        set { myField = value; } // 也可以有更复杂的逻辑  
    }  
}
//其中value是get set 中默认使用的关键字

//快速定义属性的方法:
MyCustomProperty =>myField  //简单方便省略的get set

3. Methods: A method is the specific implementation of the behavior or function of a class.

public class MyClass  
{  
    public void MyMethod() // 公共方法  
    {  
        // 方法体逻辑  
    }  
}

4. Events: Events are the notification mechanism of a class, allowing other classes to subscribe to and respond to events that occur within the class.

public class MyClass  
{  
    public event EventHandler MyEvent; // 事件声明  
}

5, Constructors (Constructors): Constructors are used to create and initialize a special state of an object created by this class.

public class MyClass  
{  
    public MyClass() // 构造函数,无参数的构造函数通常是公共的  
    {  
        // 构造逻辑  
    }  
}

6, Indexers: Indexers allow classes to be accessed through indexes like arrays.

public class MyClass  
{  
    public int this[int index] { get; set; } // 索引器声明,通常用于索引数组或集合的类中  
}

The specific introduction to the indexer is:

        Indexers allow a class or structure to be accessed by index like an array. This is typically used for custom collection or container classes to be able to access objects with array-like syntax.​ 

public class MyIndexedClass  
{  
    private int[] array = new int[5];  
  
    // 索引器定义  
    public int this[int index]  
    {  
        get  
        {  
            return array[index];  
        }  
        set  
        {  
            array[index] = value;  
        }  
    }  
}

In the above example, the MyIndexedClass class has a private array of integers array. By defining an indexer, we can access and modify elements in an array using array-like syntax.

Now, you can use the following code to create an instance of MyIndexedClass and use the indexer to access and modify the elements in the array:

MyIndexedClass indexedClass = new MyIndexedClass();  
indexedClass[0] = 10; // 设置第一个元素为10  
int firstElement = indexedClass[0]; // 获取第一个元素(值为10)

//需要注意的是,索引器可以具有多个参数,这使得它们非常灵活。
//例如,你可以定义一个双参数索引器来访问二维数组或类似的数据结构。

7. Nested Types: Other classes, structures, enumerations or interfaces can be defined inside a class. These nested types are private within the scope of the outer class.

public class OuterClass  
{  
    private class NestedClass { } // 嵌套类作为类的私有成员类型使用  
}

In subsequent classes, I will specifically explain this nested type, such as methods nested within methods, classes nested within classes, etc. 

Guess you like

Origin blog.csdn.net/qiaodahua/article/details/134980393