Why does `Int32` use `int` in its source code?

1.4k views Asked by At

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.

1

There are 1 answers

9
David Arno On BEST ANSWER

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.