Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Primitive types in .net

In .net, AIUI int is just syntactic sugar for System.Int32, which is a struct.

csharp> typeof(System.Int32).IsPrimitive 
true
csharp> typeof(System.Int32).Equals(typeof(int))
true

I see in the source:

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

That System.Int32 is just defined with reference to a member m_value that is itself an int - how does that work? Surely we're defining int with reference to itself? So how do we avoid circular definition then?

like image 408
George Simms Avatar asked Jul 29 '14 06:07

George Simms


People also ask

What is primitive types in C#?

Primitive data types are predefined data types such as Byte, SByte, Boolean, Int16, UInt16, Int32, UInt32, Char, Double, Int64, UInt64, Single, etc. Whereas non-primitive data types are user-defined data types such as enum, class, etc.

What are the 5 primitive data types?

Data types are divided into two groups: Primitive data types - includes byte , short , int , long , float , double , boolean and char.

Is string primitive type C#?

No, the string is not a primitive type.


1 Answers

There is an excellent explanation in Dixin's blog article Understanding .NET Primitive Types.

The answer can be found in the generated IL. His following question is actually the answer to your question:

So what is the relationship among int32 (IL), int (C#) and System.Int32 (C#)?

In the IL can be found that the int inside the struct is:

.field assembly int32 m_value

So that int32 actually exists outside .NET and is the actual representation of the .NET int in assembly.

like image 109
Patrick Hofman Avatar answered Oct 11 '22 23:10

Patrick Hofman