What you need to know
In a previous blog post, I analyzed how. Net recognizes the real types of references.
Transport: http://blog.csdn.net/only_lonely/archive/2010/03/11/5370787.aspx
This article is a continuation of this article. It mainly discusses some things about value types.
Here, I assume that you have learned some basic facts about the value type and reference type. For convenience, I will list them here.
1. Value-type instances are allocated in the stack, and reference-type instances are allocated in the heap (GC heap, or other)
2. The value type can only inherit from valuetype, and the reference type can inherit from any type (except valuetype)
If you understand the above two points and are clear without any doubt, OK, you can continue to look at it.
1.CodeStart
Using system;
Namespace only_lonely
{
Class demo
{
Static void main ()
{
Int32 I = 10;
I. tostring ();
}
}
}
Let's look at its il code.
. Method private hidebysig static void main () cel managed
{
. Entrypoint
// Code size 12 (0xc)
. Maxstack 1
. Locals Init ([0] int32 I)
Il_0000: LDC. i4.s 10
Il_0002: stloc.0
Il_0003: ldloca. s I
Il_0005: Call instance string [mscorlib] system. int32: tostring ()
Il_000a: Pop
Il_000b: Ret
} // End of method Demo: Main
Well, it's easy ~ I. tostring () is similar to tostring (I) here, which is a simple function call.
Next, modify the code.
Using system;
Namespace only_lonely
{
Class demo
{
Static void main ()
{
Int32 I = 10;
I. GetType ();
}
}
}
Let's dasm it
. Method private hidebysig static void main () cel managed
{
. Entrypoint
// Code size 16 (0x10)
. Maxstack 1
. Locals Init ([0] int32 I)
Il_0000: LDC. i4.s 10
Il_0002: stloc.0
Il_0003: ldloc.0
Il_0004: Box [mscorlib] system. int32
Il_0009: Call instance class [mscorlib] system. Type [mscorlib] system. Object: GetType ()
Il_000e: Pop
Il_000f: Ret
} // End of method Demo: Main
It is packed, isn't it?
Now there is only one problem, why? Why does calling tostring () of an int32 instance become a simple function call, while calling GetType () causes packing?
Let's review the concept of packing and change a value type to a reference type.
Turning the value type into the reference type makes things interesting.
Review my previous blog posts
Reference = sizeof (void *) size object header + all instance fields
The object header contains information that can accurately identify the real type.
What do you think?
For example, the value type does not support type security (accurately identifies the object type). To obtain the actual type of the object, it must be boxed (converted to the reference type)
Now, it's time to get to know our value type again.
In. net
1. All value types are inherited from valuetype, and valuetype is inherited from the object type.
2. All value types are closed and cannot be inherited.
Are you familiar with the above two statements?
:-) As long as there are more or less books such as <XXX from getting started to proficient>, the above two sentences will be mentioned.
However, Are you confused about the above two sentences? Why is the value type always like that? . Net mandatory? That's right, but why does. Net stipulate this?
OK. Now, let's use another method to explain the above two sentences to you.
All value types are closed and cannot be inherited.-> This means that the value type has no subclass (1)
All value types inherit from valuetype. valuetype is a reference type.> all value types have a parent class of the reference type. (2)
In OO, it is very important to implicitly convert subclass to parent class-> This is a condition (3)
(1) + (2) + (3)-> the value type does not have any subclass. Therefore, a value type (subclass) is implicitly converted to another value type (parent class) ()
To implicitly convert a value type, you can only implicitly convert the value type (subclass) to valuetype (parent class, determined reference type) (B)
. Net type security, a very important part is the ability to dynamically identify the actual types of types and convert them-> This is a condition (c)
(A) + (B) + (c)-> when the value type needs to perform type security operations on the type conversion ~ Must be converted to the reference type (at this point, please return to the first part of this article and take a look at the code)
Again, review one of my blog http://blog.csdn.net/only_lonely/archive/2010/03/11/5370787.aspx
. Net relies on special object headers of the pre-reference size (sizeof (void *) for dynamic type recognition.
The value type does not have those. What do you think of in combination?
"Value Type" in C ++"
The Bulls said that C # cannot be interpreted in C ++, but it is helpless ~ My strength is really limited. I can only draw a strong picture ~ Therefore, this article ~ It is only a reference and assumption that may be wrong ~ Just understand what it means.
In C ++, there is no concept of "Value Type", but there is a concept of basic type.
What is the basic type? It is a type that the compiler is sufficient to fully understand, such as the following code.
Int;
Int B;
Int c = a + B; // equivalent to C = add (A, B );
Is there an overload operator that allows two int-type numbers to perform addition operations through the symbol "+?
Yes, indeed ~, But the "function", the compiler did it for us.
Let's make a more bold assumption, if the compiler does more for us... ...
Int A = 100;
A. tostring (); // equivalent to tostring ();
When the compiler sees the above Code, it can automatically call a function, such as string tostring (INT); and pass a as a parameter. This is done!
Note: Because the value type is closed and inherited from the reference type, when it reloads a parent class method, you do not need to consider polymorphism, even if tostring () is to overload the method of the parent class, do not need to worry about it, just call a call function.
What the compiler cannot do
The compiler is static and can only be run at a time. It cannot provide the security guarantee for the dynamic runtime type. That is what CLR does, so ''when you need to know the type of the Value Type accurately, you need to convert it to the reference type and let the CLR analyze it .... ...