6

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.

dandan78
  • 13,328
  • 13
  • 64
  • 78
Mohamad Shiralizadeh
  • 8,329
  • 6
  • 58
  • 93
  • Int32 is `struct`.It containd methods and stuffs for manipulating int, int is system defined datatype. – Amit Kumar Ghosh Jun 08 '15 at 10:56
  • That *is* the original source code for `int`. Some of the actual inner workings is part of the compiler and of the runtime, but insofar as there is source code, that is the source code. –  Jun 08 '15 at 10:56
  • 1
    Maybe a duplicate: http://stackoverflow.com/questions/16113850/if-int32-is-just-an-alias-for-int-how-can-the-int32-class-use-an-int Only related: http://stackoverflow.com/questions/18018281/regarding-primitive-data-types-in-c-sharp – Tim Schmelter Jun 08 '15 at 10:58
  • Why do you think it doesn't have to use int? – Matteo Umili Jun 08 '15 at 10:59
  • 1
    @TimSchmelter Definitely duplicate. Nice find. :) –  Jun 08 '15 at 10:59

1 Answers1

7

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.

Community
  • 1
  • 1
David Arno
  • 42,717
  • 16
  • 86
  • 131
  • 1
    So Int32 "includes" itself "recursively"? – Matteo Umili Jun 08 '15 at 10:56
  • 5
    This doesn't answer the question at all. –  Jun 08 '15 at 10:57
  • @hvd, I think you are confused. Perhaps though you'd care to explain why it doesn't answer the question and maybe even answer the question yourself "correctly"? – David Arno Jun 08 '15 at 11:01
  • @codroipo, no `int` is a compiler keyword alias for `System.Int32`. – David Arno Jun 08 '15 at 11:02
  • 2
    @DavidArno You made significant edits to your answer after I posted my comment (as can be verified from the comment time and edit times). You do answer the question now. Not as well as Eric Lippert in the linked question, but it's far better than before. –  Jun 08 '15 at 11:02
  • @hvd, then I look forward to you - and all the other downvoters - removing your -1 :) – David Arno Jun 08 '15 at 11:03
  • In general, don't count on that happening, it's perfectly reasonable when people downvoting a bad answer no longer even see it after your edits, because they've moved on to other questions. But here, sure, okay. :) –  Jun 08 '15 at 11:05
  • What's confusing when looking at the source code for an `int` is that we're not really looking at the source code for an `int`. We're looking at the extensions that are added to the primitive `int` type, which in C# doesn't exist. The primitive part of an `int` is handled internally. Maybe your answer should focus on that if you want it to be useful (I haven't downvoted you but I don't think your answer is helpful at the moment). – Alex Jun 08 '15 at 11:11
  • @Alex, fair point. As the question has been closed though, I'll just leave it as-is. – David Arno Jun 08 '15 at 11:12