2

int is an alias for Int32 and is also defined as a keyword in the C# language specification.

The Reference Source has this code:

[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;

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

Int32 encapsulates an int. Can somebody explain why the alias is used internally for the alias' definition? This is the same as long pointing to Int64 and then Int64 declaring a long variable. I suppose it's the compilers prerogative to do what it pleases and int always means 32 bit memory location and this is just a way of adding methods but it is bugging me.

user247702
  • 23,641
  • 15
  • 110
  • 157
Jaycee
  • 3,098
  • 22
  • 31
  • Remember that what your decompiler generates isn't the same as the code that was originally written. The name of the struct is `Int32`, so that won't change when decompiled, but the decompiler will choose inbuilt types (`Int32` vs `int` or `String` vs `string`) according to its own logic, not the original dev's. (The question of how an `int` can contain an `int` is answered elsewhere; I'm only referring here to the discrepancy between the type name in the decompiled code.) – Dan Puzey Sep 19 '14 at 08:53
  • @Dirk thanks, I was trying to find the same question. You won. – Sriram Sakthivel Sep 19 '14 at 08:53
  • Built-in types are magic and they have to be, otherwise it's turtles all the way down. – harold Sep 19 '14 at 08:54
  • @Dan I've replaced the decompiled code with the official code and it's the same. – user247702 Sep 19 '14 at 08:55
  • You are correct int always refer to the 32 bit memory, and int is an alias for System.Int32 struct, check out the following links for more information: http://www.dotnet-tricks.com/Tutorial/csharp/PQEO291212-Difference-between-int,-Int16,-Int32-and-Int64.html http://blogs.msdn.com/b/csharpfaq/archive/2004/03/12/88418.aspx http://stackoverflow.com/questions/62503/c-int-or-int32-should-i-care – Mrinal Kamboj Sep 19 '14 at 09:00
  • Yes this http://stackoverflow.com/questions/16113850/if-int32-is-just-an-alias-for-int-how-can-the-int32-class-use-an-int answers the question admirably – Jaycee Sep 19 '14 at 09:09

0 Answers0