● Field, also known as Member Variable, is divided into instance domains and static domains. The domain access restrictions embodies the principle of object-oriented programming; ● Behind the attributes are two functions: assignment function (GET) and value functions (SET). Domain Domain (Field) Also known as Member Variable, it represents a storage location, which is an indispensable part in the C # class. The type of domain can be any data type in the C #, but for other reference types in the String type, the operation is placed in the later chapters due to the operation of some classes of the constructor during initialization. The domain is divided into instance domains and static domains. The instance domain belongs to the specific object, which is proprietary for a particular object. The static domain belongs to the class and is shared for all objects. C # Strictly specify the instance field can only be obtained by objects, and the static domain can only be obtained by class. The domain access restrictions reflect the principle of encapsulation of object-oriented programming. As mentioned earlier, the access restriction modifier in C # has five, which is applicable to these five pairs of domains. C # just extension C original Friend modifiers with INTERNAL. These classes are declared as Internal, and then they are compiled in one composite when certain areas of the two classes are visible. If you need to be inherited to their inheritance, it is possible to declare it as protected internal. C # introduces the ReadOnly modifier to represent a read-only domain, introducing consts, indicating that the usual amount is not constant. As the name suggests that the only way to write is not written, the constant constant cannot be modified, what is the difference between these two? The read-only domain can only assign a value during initialization (declaration initialization, or constructor initialization), otherwise the compiler will report an error. A read-only domain can be an instance domain or a static domain. The type of read-only domain can be any type of C # language. The constant constant constant must be assigned to the statement, and the compiler can calculate this determined value at compile. Const modified constants are static variables that cannot be obtained for objects. The type of Const modified value is also limited, it can only be one of the following types (or can be converted to the following types): Sbyte, Byte, Short, USHORT, INT, UINT, Long, Ulong, Char, Float, Double, Decimal, BOOL, STRING, ENUM type, or reference type. It is worth noting that the reference type here is that all types (except values NULL) are compiled, so they can be declared by compiler, so they can declare that CONST reference types can be declared Other reference types that can be string or values NULL. If a constant constant is required, its type limits it cannot be determined at compile time, which can be resolved to static Readonly to resolve it. Constly modified constant is calculated at compiled, and is replaced to each place referenced to the constant, and readonly determines the value at runtime, do not want its value to change again, this is both Main difference. Initialization of the domain is a problem that needs special attention in object-oriented programming. The C # compiler defaults to initialize each domain as its default value. Simply put, the default value of the numeric type (enumeration type) is 0 or 0.0, the default value of the character type is '/ x0000', the default value of the Boolean type is false, the default value of the reference type is NULL, the default of the structure type Values all types of all types take appropriate default values. Although the C # compiler sets the default type for each type, but according to the object-oriented design principle, it is also necessary to correctly initialize the variable. This is also a C # recommended approach. No domain is initialized to make a warning message. . There are two places in C # to initialize the domain: while the declaration is initialized and initialized in the constructor.
As mentioned earlier, the domain declaration is actually being executed as an assignment statement as an assignment statement in the first place within the constructor. Example variable initialization will be placed in an instance constructor, and static variable initialization will be placed in a static constructor. If a static variable is declared and initialized at the same time, the compiler will construct a static constructor to turn this initialization statement into an assignment statement. As a constant domain of const modifications, from strict sense cannot count the initialization statement, you can see it to make a macro change in C . Attribute properties can be said to be an innovation of C # language, of course, it can also be said. The reason is that the implementation behind it is actually two functions: assignment function (GET) and value function (SET), which can be clearly seen from the intermediate language code generated. Yes, it is that it does implement a special interface for object-oriented programming has always been a special interface for "attribute" OO style. C # does not advocate the setting of the domain's protection level to public, and users can operate outside of the class - that is too unsafe! For all the domains other than the class, C # recommends expressing attributes. Attributes do not represent storage locations, which is the foundation of attributes and domains. The following is a typical property design: using System; class MyClass {int integer; public int Integer {get {return integer;} set {integer = value;}}} class Test {public static void Main () {MyClass MyObject = new Myclass (); console.write (MyObject.integer); MyObject.integer ; console.write (MyObject.integer);}} is as expected, the program output "0 1". You can see that attributes provide a friendly domain member access interface by packaging to the method. The value here is the keyword of the C #, is the implied parameters of the set function when the property is operated, that is, the right value when the attribute write operation is performed. The attribute provides only three interface operations that read (GET), write (SET), read and write (GET, and SET). These three operations on the domain must be declared under the same attribute name without separating them. Of course, attributes are not limited to the domain interface operation, the essence of the attribute is also a method, and can perform some inspections, warnings, etc., the following examples can be made according to the extraction or assignment of the program logic, and see the following example: class myclass {private string name; public String name {get {return name;} set {if (value == null) name = "microsoft"; else name = value;}}} Due to the physical method, the attributes, of course, various modifications, 5 kinds of attributes Access the modifier, but attribute access modifications are often public, otherwise it will lose the meaning of the public interface of the class as a class. In addition to the multi-parameters of the method, the special attributes such as overloading, Virtual, Sealed, Override, Abstract and other modifiers are the same behavior, but because attributes are in nature of two methods. Its behavior needs to be paying attention.