.net中的原始类型

Geo*_*mms 14 .net c# int clr primitive-types

在.net中,AIUI int只是一个语法糖System.Int32,它是一个struct.

csharp> typeof(System.Int32).IsPrimitive 
true
csharp> typeof(System.Int32).Equals(typeof(int))
true
Run Code Online (Sandbox Code Playgroud)

我在源头看到:

https://github.com/mono/mono/blob/master/mcs/class/corlib/System/Int32.cs http://referencesource.microsoft.com/#mscorlib/system/int32.cs

System.Int32只是参考一个m_value本身的成员来定义int- 这是如何工作的?当然,我们是int根据自己定义的?那么我们如何避免循环定义呢?

Pat*_*man 15

在Dixin的博客文章Understanding .NET Primitive Types中有一个很好的解释.

答案可以在生成的IL中找到.他的以下问题实际上就是你问题的答案:

那么int32(IL),int(C#)和System.Int32(C#)之间的关系是什么?

在IL中可以发现int里面struct是:

.field assembly int32 m_value
Run Code Online (Sandbox Code Playgroud)

所以这int32实际上存在于.NET之外,并且是.NET int在程序集中的实际表示.