On the so-called "value type" in C #

Source: Internet
Author: User

In C #, I believe you are not unfamiliar with "value type" because it is indispensable in our code design, so what is "value type"? "Value type" is the Int16,int32 these numeric types we use?

We know that all types in C # inherit from the System.Object root type, which means that at the code level, all types in C # are class, that is, everything is class, and since all are class types, where is the value type? We can find in C # System.int16,system.int32,system.boolean ... And so on. These commonly used types are struct struct types, so what are the struct types, we define a struct, and then the IL disassemble tool to view the compiled intermediate code to get the result.

public struct CUSTOMERSTRUCT

{

public string Name{get;set;}

}

The code is very simple, we define a struct type and include a name attribute.

See the above picture, I believe we all have seen that defining a struct type actually compiles the type of System.ValueType that you define, in other words, in C # We often use System.int16,system.int32, System.Boolean.. These numeric struct types are inherited from the System.ValueType type, and the System.ValueType is inherited from the System.Object root type, which verifies that all types in C # that I started talking about are class.

The notion of a value type exists because some types in C # have this particular status (that is, inherited from System.ValueType types), and the CLR treats these types in a special way,

Look at these 2 lines of code here.

Int32 a = new System.Int32 (10);

Customerstruct customer = new Customerstruct ();

First, why is it possible to use new for memory allocation for numeric types? As mentioned above, because they are all class types, of course you can use new to allocate memory.

Second, when the CLR executes such code, the CLR uses reflection (to be seen) or other means to know whether the type of the requested allocated memory inherits from System.ValueType if so, then it is allocated on the stack, if not, then that is what we call the reference type, Allocating memory on the managed heap and allocating the corresponding reference variables on the stack are all the work that the CLR does.

Let's take a look at the boxing operation again.

Object o = new Object ();

Int32 AAA = 100;

o = AAA; Boxing operations will occur here

We all know that when the CLR executes to o = AAA; The boxing operation occurs when the statement occurs. This is because the CLR knows that the type AAA to which the reference variable o is referenced is inherited from the System.ValueType type. The types that inherit from System.ValueType are allocated on the stack, while others are allocated on the managed heap, so the CLR will get a AAA copy of the managed heap, all of which is the work of the CLR.

From this we can draw the conclusion that:

1. In C #, all types are class types on the code level.

2. The so-called "value type"!= numeric type, but all types that inherit from System.ValueType.

3. Language-level support for the value type, the CLR knows how to allocate and process memory for class types such as value types.

To sum up. Personal humble opinion, what is wrong place hope everybody helps point out, common study, common progress.

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.