I. c # value type and reference type
1. value types: struct value type, such as int type, bool type, user-defined struct type), enumeration type, and controllable type. The value type exists on the stack.
2. Reference Type: array, user-defined class, interface, Delegate, object, String. The reference type is on the stack.
Memory blocks are divided into Stack, heap, and static storage areas)
3. Notes:
1) The struct is still a value type even if it contains a member of the reference type.
2) elements of the array, whether of the reference type or value type, are stored on the managed stack.
3) The value type is always assigned to the place where it is affirmed. When it is used as a field, it follows the variable instance to which it belongs.) stored as a local variable on the stack.
4) value types have better efficiency in memory management and do not support polymorphism. They are suitable for In-storage carriers. The reference types support polymorphism and are suitable for defining the behavior of applications.
Ii. Access Control for Class c:
Public, protect, private, internal, protect internal
Public, any other object can be accessed, and The subclass inherits. The access of this attribute of other object subclasses is determined by the access of the subclass.
Protected by protect, subclass can inherit, and other classes cannot be accessed directly
Private: The subclass inherits, but the subclass is inaccessible, and other objects cannot be accessed.
Internal, that is, it can be accessed in the same assembly
Protect internal The subclass within the same assembly can be inherited, and other classes cannot be accessed
The system is private by default.
3. Class fields, methods, and attributes
Class is the basic unit of object-oriented programming. class members include events, fields, nested types, methods, and attributes. As a beginner, we must first understand the fields, methods, and attributes in the class.
1. Fields of the class. The field of a class should be a data member of the class, which is defined as a variable used to store instance-related data of the Class and Class.
2. Class method. A method is also called a behavior. It is a function member of a class and is a function to implement a specific function of the class.
3. attributes of a class. The so-called "attribute" can be viewed as a encapsulation of "field" to a large extent, it uses a get/set accessor to control the read/write operations on fields and expose an attribute value.
The following is an example to describe the role of an attribute.
Class Student
{
// Data member or field Definition
Public string name;
Private string accnumber; // here, the student account is defined as private
//, Cannot be inherited or accessed.
// Attribute Definition
Public string Accnumber
{
Get
{
Return name;
}
Set
{
Accnumber = value;
}
}
...
}
// Here, we can use the property to read the value of accumber through the get accesser. Through the set accesser, we can assign a new value to the accumber. In subclass and other classes, we can regard Accnumber as a substitute for accnumber, such as Student std = std. accnumber = "s001", we successfully assigned the value "s001" to the accnumber ".
Of course, attributes are not used by get and set. When I only need to read the value of accnumber and do not change its value, I only need to use get accessors. In the future, we will introduce the readonly access modifier, which controls that variables can only be read but not written.
In the above example, get is the value, and set is the set value. In set, we can also add some restrictions and selection conditions, such as if (value. Length = 4) {accnumber = value };
A field is a variable used to store data. The attribute is a method or a function member. So since the attribute is a method, what is the difference between it and the method? From the definition, we can see that attributes are actually methods. However, since attributes and methods are defined as two concepts, they must be different. Personally, the biggest difference between them is that there is no parameter list for the attribute, and the method must have a parameter list. Even if there is no parameter, an empty parenthesis should be placed there. Second, the attribute definition should have two accessors: set and get, which are used to obtain the attribute value and set the attribute value. No difference is found in other places, attribute can also determine the validity of data, which is similar to the method.