HI WELCOME TO Sirees

Difference between int and Int32 in c#

Leave a Comment

This is a very basic and a common c# interview question


Int32 and int are synonymous, both of them allow us to create a 32 bit integer. int is shorthand notation (alias) for Int32. When declaring an integer in a c# program most of us prefer using int over Int32.  



Whether we use int or Int32 to create an integer, the behaviour is identical. 

int i = 10;
Int32 j = 10;

Console.WriteLine("int i = " + i);
Console.WriteLine("Int32 j = " + j);
Console.WriteLine("int i + Int32 j =  " + (i + j));

I think the only place where Int32 is not allowed is when creating an enum. The following code will raise a compiler error stating - Type byte, sbyte, short, ushort, int, uint, long, or ulong expected.
enum Test : Int32
{
    XXX = 1
}

The following code will compile just fine
enum Test : int
{
    XXX = 1
}

I can think of only the following minor differences between int and Int32
1. One of the difference is in readability. When we use Int32, we are being explicitl about the size of the variable.
2. To use Int32, either we need to use using System declaration or specify the fully qualified name (System.Int32) where as with int it is not required.

If you can think of any other differences, please feel free to leave a comment.

The interviewer may also ask, what is the difference between string and System.String
There is no difference string is an alias for System.String. 

0 comments:

Post a Comment

Note: only a member of this blog may post a comment.