Keep quiet read CLR via C # (02)-primitive type, reference type, Value Type

Source: Internet
Author: User
Keep quiet read CLR via C # (02)-primitive type, reference type, Value Type 1, primitive type

The data types directly supported by the compiler are called primitive types. Such as int and string. There is a direct ing between the primitive type and the. NET Framework class library FCL.

StringAnd string?

I was asked this question during the interview. The primitive type string in C # actually corresponds to the system. String (FCL) type, so there is no difference between the two.

Type conversion

The compiler can perform explicit or implicit conversion between primitive types. If the conversion is secure, that is, the conversion process will not cause data loss, you can directly use implicit conversion. If it is insecure, Explicit conversions must be used.

Int32 A = 5;

Int64 B =;

Int32 c = (int32) B;

Ii. Reference Type and Value Type

Differences between the reference type and value type:

Reference Type

Value Type

Allocate from managed heap

Allocate from the thread Stack

Take into account the garbage collection mechanism.

The garbage collection mechanism is not considered.

All classes are referenced.

Both the structure and enumeration are value types.

Inherited from system. valuetype

Only packing format

There are two formats: Packing and unboxing.

Can inherit and derive

Cannot be a base class or a virtual Method

The default value of the reference type variable during initialization is null.

The default value is 0 during initialization.

Copy only the memory address during replication

Copy of "field-to-field" during replication

   

Struct directly inherits from system. valuetype, while enumeration directly inherits from system. Enum, and enum directly inherits from system. valuetype.

The following example shows their differences:

First, define the class and struct:

Class someref {public int32 X ;}

Struct someval {public int32 X ;}

Someref R1 = new someref (); // allocate to heap

Someval V1 = new someval (); // allocate to stack

R1.x = 5; // modify the data in the referenced heap Space

V1.x = 5; // re-assign values directly on the stack

Console. writeline (r1.x); // "5"

Console. writeline (v1.x); // "5"

Someref r2 = R1; // only copies the pointer to R2.

Someval v2 = V1; // allocate space on the stack and copy the variable content

R1.x = 8; // Changes the content that R1 points to (or R2 points)

V1.x = 9; // only V1 content is modified. V2 content is not affected.

Console. writeline (r1.x); // "8"

Console. writeline (r2.x); // "8"

Console. writeline (v1.x); // "9"

Console. writeline (v2.x); // "5"

You can see the memory allocation at a glance.

Iii. packing and unpacking of value types

1.Packing process?

Packing: Convert the value type to the reference type. When we pass the value type parameter to a method that needs to reference the type parameter, the packing operation is automatically performed. The process is as follows:

    • Allocate the size from the managed heap for the reference type to be generated. Size: the size of the Value Type instance + extra space (method table pointer and syncblockindex ).
    • Copy the value type field to the allocated memory.
    • Returns the address of the newly allocated memory in the managed heap. That is, the reference to the object.

2.Unpacking process?

Unpack: Gets the pointer to the value type part of the object. After unpacking, the field copy operation is generally performed. The two operations add up to the mutual inversion between packing and packing. The process is as follows:

    • If the reference is null, an nullreferenceexception is thrown.
    • If the referenced object is not a boxed object of the expected type, an invalidcastexception is thrown.
    • Returns a pointer to the value type Part Of The boxed object.

3.Instance

    • The transformation result of unpacking must be the type in which it was not packed.

Public static void main (){

Int32 x = 5;

Object o = x; // boxed

Int16 y = (int16) O; // unpack and throw an invalidcastexception

}

Fixed: int16 z = (int16) (int32) O; // The binning is successful.

    • This sectionCodeHow many times has it been packed?

Public static void main (){

Int32 v = 5; // create a value variable

Object o = V; // boxed

V = 123; // Changes the unboxed value to 123

Console. writeline (V + "," + (int32) O); // displays "123, 5", boxed twice

}

The code above is packed three times. In the last line, V is packed as the reference type, and O is first split and then boxed as the reference type.

Let's take a look at its il code:

A string connection actually calls the Concat method. The parameter of this method must be an object. Therefore, all parameters must be converted to the object type.

 

Optimization:

Console. writeline (V. tostring () + "," + O); // boxed 0 times

we should try to reduce the number of value-Type Packing operations to improve the Program performance.

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.