You can access a member variable of a class in two ways: domain and attribute. The domain is accessed as a member variable of the public type, but the attribute cannot be accessed directly. It must be accessed through the accessors.
Field)
Field)
-The field indicates the variables associated with objects or classes.
-If the readonly modifier is added to the declaration of the domain, it indicates that the domain is a read-only domain. For a read-only domain, we can only modify it in the definition of the domain and the constructor of its class. In other cases, the domain is "read-only.
-The role of static readonly is similar to that of # define and const. The difference is that the value of the const expression is formed during compilation, and the value of the static readonly expression is formed only when the program is running. For example:
Public class
{
Public static readonly int X = 1;
}
-Uninitialized variables in C/C ++ cannot be used. In C #, the system provides a default value for each uninitialized variable. For all referenced variables, the default value is null. The default values of all value types are fixed. For static fields, the class is initialized when it is loaded; for non-static fields, it is initialized when the class instance is created. Before the default initialization, the field value is unpredictable.
For example, the following code is valid:
Class Test
{
Static int a = B + 1;
Static int B = a + 1;
}
In fact, it is equivalent to: a = 1; B = 2;
The following code is invalid:
Class
{
Int x = 1;
Int y = x + 1;
}
Because non-static variable x is not initialized before class A is instantiated, code y = x + 1 cannot get the correct value of x.
Property)
-Fully embodies the encapsulation of objects: instead of directly operating the data content, the data is accessed through the accessors, that is, the attribute values are read and written by means of get and set; on the other hand, you can also control the access attribute of data (of course, you can also implement it by adding the readonly keyword to a common domain.