5

Possible Duplicate:
C#, int or Int32? Should I care?

Please any one let me know MSIL of System.Int32 and int will be same or different, If different then which one we should use.

Edit:

this won't compile

public  enum MyEnum : Int32
{
    AEnum = 0
}

but this will

public enum MyEnum : int
{
    AEnum = 0
}
Community
  • 1
  • 1
Pankaj Agarwal
  • 11,191
  • 12
  • 43
  • 59
  • 1
    It is explained here: http://stackoverflow.com/questions/62503/c-int-or-int32-should-i-care – Longball27 May 31 '11 at 11:55
  • For more details check this http://stackoverflow.com/questions/5005319/why-cant-i-declare-an-enum-inheriting-from-byte-but-i-can-from-byte – Lukasz Madon May 31 '11 at 12:11

6 Answers6

10

int is an alias for System.Int32, they are completely the same.

enums only take integral types as type, as this would be possible otherwise:

using Int32 = System.String;

public enum Something : Int32
{
}

This is according to C# spec, which states

enum-declaration:
[attributes] [enum-modifiers ] enum identifier [enum-base] enum-body [;]

where
enum-base:
":" integral-type

With integral type specified as:

integral-type:
sbyte
byte
short
ushort
int
uint
long
ulong
char
Femaref
  • 60,705
  • 7
  • 138
  • 176
  • I have edited my question, Please check now – Pankaj Agarwal May 31 '11 at 11:56
  • 1
    @Pankaj: Your edit shows one small corner-case where `int` and `Int32` aren't interchangeable, and in that situation the compiler tells you about it. You can safely assume that whenever the compiler doesn't produce any errors then the resulting IL will be exactly the same for `int` or `Int32`. – LukeH May 31 '11 at 12:15
1

http://msdn.microsoft.com/en-us/library/5kzh1b5w(v=VS.100).aspx

Type: int
.NET Framework type: System.Int32

Andrey
  • 59,039
  • 12
  • 119
  • 163
1

Both are same but int is language specification is the definitive source for C# syntax and usage

Int32 is an immutable value type that represents signed integers with values that range from negative 2,147,483,648 (which is represented by the Int32.MinValue constant) through positive 2,147,483,647 (which is represented by the Int32.MaxValue constant. From MSDN

Muhammad Akhtar
  • 51,913
  • 37
  • 138
  • 191
0

int is an alias for System.Int32 so there is no difference

IndigoDelta
  • 1,481
  • 9
  • 11
0

These are Exactly same as everyone already told int is an alias for Int32.

You can experiment it yourself place a mouse over int in visual studio and Click "Go to Decleration". Same as int string is also an alias for String

crypted
  • 10,118
  • 3
  • 39
  • 52
0

Confirm. System.Int32 doesn't work. Mono 2.8 compiler sais:

t.cs(3,29): error CS1008: Type byte, sbyte, short, ushort, int, 
uint, long or ulong expected

So it appears to be a hardcoded language definition rule.


Trying to be devil's advocate here:

Perhaps you are running into a name clash, and you have Int32 defined elsewhere (I haven't tried whether that is legal, but it might be a problem). Try qualifying it:

 public  enum MyEnum : System.Int32
 {
    AEnum = 0
 }

sehe
  • 374,641
  • 47
  • 450
  • 633