Recently the park Teamone wrote a "[C #] int and System.Int32 What difference", found that there are many wonderful comments, so could not help but want to summarize this article:>
The main references in this article are:
1. Understanding System.Int32 and int in C #: not chickens and eggs @Author:D ixin
2. What is the difference between "[C #] int and System.Int32" @Author: Teamone
I. Source of the problem
MSDN says that int is just an alias for System.Int32, which means:
int i=1; System.Int32 I=1;
should be equivalent, or no difference.
But when Dixin was disassembling the. Net Framework's mscorlib.dll with reflector, it didn't seem like that, see:
Is the definition of System.Int32 that dixin reflector to mscorlib.dll after disassembly, in which we see the definition of Int32 used int, and if you press MSDN to say that int is just a System.Int32 alias, the problem is:
(1). There is no difference between using int and using System.Int32.
(2). In the struct definition of C #, the existence of a variable of its own type in a field is not allowed, as in the following example:
struct test{ value;}
The above example compilation will fail. Since System.Int32 is equivalent to int, the compilation of System.Int32 should also fail! But in fact, the Reflector de-compiled code does show that int is used in the System.Int32 definition. This makes people wonder if therelationship between int and System.Int32 is as simple as what MSDN said?? What exactly is int an alias for System.Int32? Or does the System.Int32 encapsulate int?
Two. The relationship between INT and System.Int32
I intercepted it from the official Microsoft Introduction, so let's take a look at the. Net Framework's architecture:
The. Net Framework is a foundation platform that supports the creation of a variety of languages on this basis, as well as communication between cross-language programs.
For these reasons, the resources provided externally by the. Net framework must be generic and avoid the use of a language-specific salutation, so as not to create unnecessary confusion.
So, this has int and System.Int32, and their relationships are as follows:
System.Int32 is the. Net Framework's identification of 32-bit integers, and MSDN's salutation to this type is User type. While int is a unique salutation in the C # language (here it corresponds to the System.Int32 in the. Net framework), MSDN's salutation to C # int is Keyword. int is the alias of System.Int32!
Then why do we get the result of the first picture when we use reflector to decompile the mscorlib.dll?
Yes, in the. Net framework runtime, there is a most basic data type called primitive type (primitive). This data type is only available to the. Net Framework for internal use and is invisible outside. In fact, in the real Microsoft's System.Int32 source code, the use should be int32. But since Int32 is not a type provided by C #, Reflector automatically reverses int32 to C # int, which is why we see the existence of int in the SYSTEM.INT32 definition.
Here I cite a section of IL code in the Dixin article to prove the existence of Int32:>
C # code:
int TestMethod (value) { value * 2;}
The corresponding IL code:
.method public Hidebysig instance Int32 TestMethod (int32
For more information on "primitive types", refer to this article, "Understanding primitive types, FCL types, and compatibility with the CLR."
Three. System.Int32 on 64-bit machines
The System.Int32 represents a 32-bit integer on a 64-bit machine, meaning that the int of C # also represents a 32-bit integer on a 64-bit machine. As for why, look:
If the System.Int32 64-bit machine is 64bit, then this will make the C # program on the 32-bit machine difficult to communicate with the 64-bit C # program, imagine that 64bit of data into the 32bit of space is a disgusting thing ah! So, System.Int32 in 64-bit machine or 32-bit length, is very reasonable.
What is the difference between "[C #] int and System.Int32"