Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does `Int32` use `int` in its source code? [duplicate]

Why does Int32 use int in its source code? Doesn't Int32 equal int in C#? So what is the original source code for int? I'm confused..

[Serializable]
[System.Runtime.InteropServices.StructLayout(LayoutKind.Sequential)] 
[System.Runtime.InteropServices.ComVisible(true)]
#if GENERICS_WORK
    public struct Int32 : IComparable, IFormattable, IConvertible
        , IComparable<Int32>, IEquatable<Int32>
///     , IArithmetic<Int32>
#else
    public struct Int32 : IComparable, IFormattable, IConvertible
#endif
    {
        internal int m_value; // Here????

        public const int MaxValue = 0x7fffffff;
        public const int MinValue = unchecked((int)0x80000000);

        ...
    }

there is the same issue between Boolean and bool too.

like image 469
Mohamad Shiralizadeh Avatar asked Jun 08 '15 10:06

Mohamad Shiralizadeh


1 Answers

int, bool etc are simply aliases for builtin types like Int32 and Boolean respectively. They are shorthand, so it makes sense to use them. even in their own definitions.

The aliases are built into the compiler, so there is no "chicken and egg" situation of Int32 having to be compiled before int is accessible. There is no "original source code" for int. The "source" is for Int32 and is shared between functionality built into the CLR (for handling the primitives) and the framework (for defining functionality for manipulating those primitives). As this answer to a duplicate question explains, the source for Int32 isn't a valid definition of the type itself; that's built into the CLR. As such, the compiler has to fudge the normally illegal use of int within that file in order to allow the Int32 type to have functionality.

like image 67
David Arno Avatar answered Oct 13 '22 11:10

David Arno