If you get under the hood, value types in C# are treated very specially by compiler/CLR. But types internal to CLR are treated even more specially. Here is what I mean:
int a = 5;
int b = 10;
int с = a + b;
a.CompareTo(b);
You can hover with your mouse over int
in Visual Studio and see that actually it is System.Int32
struct. That's OK. Now you can grab ILDasm and look into what System.Int32
is: turns over that it is very simple struct with one field of type int32
(this is int32
internal to CLR) and there is no overloaded operator for addition.
So, how this int с = a + b
works then? Again I grab ILDasm and look into IL. Turns out that there is no System.Int32
in IL code: compiler automatically understands that it should replace it with int32
. And there is IL instruction add
that works for couple of int32
on a stack. What amuses me even more, CLR allows to call instance methods for System.Int32
on int32
. Looks like some black magic to me.
So, here goes the pure theoretical question: seems that System.Int32
is type like any other, could it be created in C# somehow? And if it could, could you do anything useful with it (actual int32
field is private)?
Edit: Ok, to make it a bit more clear: this question have nothing about int
being alias to System.Int32
. One can take provided example, replace int
with System.Int32
and skip first paragraph after the example. The real question is about possibility to have valuetype [mscorlib]System.Int32 a
in your IL code instead of just int32 a
.