From the perspective of C #2.0, the. NET 2.0 Type System

Source: Internet
Author: User
All variables in C # Must be initialized before they are used. Otherwise, the compiler will generate a compilation error during initialization.

Each windows thread has a private memory block called a stack. That is to say, this memory block cannot be accessed by other threads (except under certain conditions ).What are the conditions?
The thread stack is mainly used:

    • Saves the input real parameter value of the method being executed;
    • Save the method and return the local jumpCodeAddress;
    • Save the object (but not all ).

A process usually has only (and sometimes multiple) heap, which is shared by all the threads under it.When?
The advantage of heap is that its capacity is much larger than that of stack. Shared by all threads.
Stack is faster than stack. This is mainly due to the dedicated access to the stack's il commands, and the access to the elements on the stack do not need to be synchronized.
Therefore, a stack is used to store large objects and a stack is used to store small objects ..
Instances of the Value Type in C # use static allocation (allocated on the thread stack), while instances of the reference type use Dynamic Allocation (allocated in the process heap ).

The structure and enumeration are value types, and the delegate isReference Type

If a class instance has a value type field, the field will be stored together with the class instance, that is, heap. If a structure field isReference Type, ThenReference TypeThe instance will still be saved in the heap.

In C #, the modulo operation can also be used for floating point numbers.
Float a = 5.5f;
Console. writeline (a % 2.5 );
0.5 is output on the screen.
The C # language provides the checked keyword to verify all type conversion and operations. If any problem occurs, an exception is thrown.

If the integer and decimal types are divided by zero, a dividebyzeroexception occurs.
When float and double values are divided by zero, they will get an infinite number, and 0/0 will get Nan.

The increasing and decreasing Operators of C # can also be used for floating point numbers.
Float a = 5.5f;
A ++;
In this case, a = 6.5.

The structure in C # cannot be derived from other classes or structures, nor can it be used as the base class of other types or structures.
The C # structure can have multiple constructor. If the default constructor cannot be customized (that is, the constructor without parameters ). In addition, the compiler requires that each constructor Initialize all fields in the structure.
By default, the constructor sets all value fields to zero, and all reference fields to empty references.
The structure can have its own method.
Unlike the class field, the structure field cannot display initialization in the Declaration.
Structure instances are often stored in the stack, so the structure should not be too large. It is best to use a class instead of a large structure.

By default, the compiler treats the enumerated value as an int integer. Therefore, any value in this group of constants defined by enumeration is an integer. You can increment or decrease the number.
By default, the first value of enumeration is 0, and each subsequent value is the previous value plus 1. Of course, you can also manually specify the value you want.
You can also define values of other enumeration types:
Enum makerchb: byte {Beijing, Zhejiang}
Enum makerchlorophyll: long {Beijing, Zhejiang}

Allows an enumerated instance to contain multiple enumerated values. This concept is also called binary flag or flag enum ).
Note that the system. Flags attribute flag is applied to the bitfield enumeration. This attribute notifies Clr and client that this enumeration is used to represent the bitfield rather than the standard enumeration. This enumeration is used to represent the bitfield rather than the standard enumeration. This flag affects the result of the tostring () method.It is best to have an instance!
In. NET Framework, system. Threading. threadstate uses the binary flag for enumeration.

In C #, the string class instance is unchangeable. That is, they cannot be modified, and they will remain unchanged by the value assigned by the constructor.
C # Allow defining literal constants without escaping strings by adding the "@" symbol before the double quotation marks starting from the string. Literal constants without escape strings have the following features:

    • It will accept all characters as the content of the string, including "\" backslash characters, but not double quotation marks.
    • It will accept all line breaks in the string.

C # Is a string class instance that is unchangeable. A new string instance is allocated for all string modification operations. If the applicationProgramTo frequently modify a string or process a long string, the better way is to use the system. Text. stringbuilder class.
Insecure code can also break the immutable feature of string instances.

C # allow the use of the delegate keyword to create a special class. We call this class A delegate class. An instance of a delegate class is called a delegate object.
A delegate object is a reference pointing to one or more methods (static or non-static. We can "call" The delegate object like calling a method, which is actually the method referenced by the delegate object. Note that the call of these methods is completed in the thread where the delegate method is called, that is, synchronous call.
The method referenced by the delegate variable can only be the same as the signature method provided in the delegate class declaration. Delegate   Void Deleg1 ();
Delegate   String Deleg2 ( String S );
Delegate   Void F1 ()
{
}
Static   String F2 ( String S)
{
}
Deleg1 d1 =   New Deleg1 (F1 ); // Right
Deleg2 D2 =   New Deleg2 (F2 ); // Right
Deleg2 D3 =   New Deleg2 (F1 ); // Error

C #2.0 the compiler introduces a function to speculate its type when creating a delegate variable. Therefore, you can directly assign a method to an object created by implicit operation. For example
Deleg1 d1 = F1;
Deleg2 D2 = F2;
However, its compiled il code is the same as above, and it still calls the constructor of the deleg1 and deleg2 delegate classes.
You can use one delegate object to apply multiple methods with the same signature (static or non-static ). In this case, calling the delegate object will execute all methods sequentially in the thread of the called object. The order in which a method is called is the order in which the method is added to the delegate object. The parameters of each method are the same.
If the delegate signature has a return value, when multiple methods are referenced, only the return value of the last called method is returned as the return value of the delegate object.
You can use getinvocationlist () to obtain the delegate list in the delegate object.

The null type system. nullable <t> is equivalent to T ?.
Int? I = NULL; and nullable <int> I = NULL; are equivalent.
However, the compiler will block implicit conversion from the null type to the original type. In addition, it is dangerous to convert the display of the empty type to the original type without prior testing, because this may cause invalidoperationexception.

C #2.0 allows the declaration of classes, structures, and interfaces to be dispersed into multiple source files. We call this particular life part type. Note that the delegate class or enumeration class cannot be declared in multiple source files.
The partial keyword must be added before the definition of different parts of the same type. In addition, if the type is generic, the definition of the type parameter must also appear in the definition of each part. The name and location of the type parameter must be consistent in each part definition. That is, add the partial keyword before the class, struct, or interface keyword of each part. And must belong to the same namespace.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.