1

What are the advantages/disadvantages of int and Int32? And Why should we use first instead of second?

WaterDance
  • 105
  • 1
  • 1
  • 6
  • 1
    Both are the same ... [Have a look][1] [1]: http://stackoverflow.com/questions/62503/c-int-or-int32-should-i-care – Asif Mushtaq May 07 '12 at 15:32
  • Providing a proper answer for this question requires a time machine. Once that forwards you into a future where everybody has a 256-bit cpu in their wrist watch. Whether int is then *still* an alias for Int32 is unanswerable. – Hans Passant May 07 '12 at 15:38
  • 1
    check this also http://stackoverflow.com/a/215422/926460 – Timeless May 07 '12 at 16:06

5 Answers5

3

They are in fact one and the same -- both declare 32-bit integers, and for the most part their behavior will be identical. The shorthand int is just an alias for the Int32 system type.

From the language specification:

4.1.4 Simple Types
C# provides a set of predefined struct types called the simple types. The simple types are identified through reserved words, but these reserved words are simply aliases for predefined struct types in the System namespace, as described in the table below.

Here is a list of the simple types and their aliases:

Reserved word   Aliased type
sbyte           System.SByte
byte            System.Byte
short           System.Int16
ushort          System.UInt16
int             System.Int32
uint            System.UInt32
long            System.Int64
ulong           System.UInt64
char            System.Char
float           System.Single
double          System.Double
bool            System.Boolean
decimal         System.Decimal

There are only a couple instances I can think of where using one over the other would matter. The first is where it's important to know the limitations of the type (e.g. cryptography), but that's only for readability. The other is with an enum:

public enum MyEnum : Int32
{
    member1 = 0 //no good
}

public enum MyEnum : int
{
    member1 = 0 //all is well
}
James Johnson
  • 45,496
  • 8
  • 73
  • 110
  • +1 for mentioning the `enum` difference (the only place it matters to the compiler). – Gabe Jun 08 '12 at 03:39
2

There is no any practical advantage or disadvantage.

The only difference is can be that you esplicitly visualize in case of int32 that you're ddealing with 32 bit value.

That is.

Tigran
  • 61,654
  • 8
  • 86
  • 123
  • 1
    I saw Int32 in reflector and it has 5 parent interface and many methods. I think it should affect performance or something. thanks. – WaterDance May 07 '12 at 15:39
  • @WaterDance: no, there is no any performance differencies, feel free to use any of them. – Tigran May 07 '12 at 15:42
1

int is just an alias to Int32. So, just use what do you like more.

ie.
  • 5,982
  • 1
  • 29
  • 44
1

int is an alias for System.Int32

If you're not using System; you don't have Int32. That's why int is preferred.

Agent_L
  • 4,960
  • 28
  • 30
1

As stated an int is basically an Int32. But one difference would be that the "using System;" namespace would have to be included while using Int32.

Krishna
  • 636
  • 3
  • 8