c# interview questions and answers
Lesson 12 of 15 · Part 12 Difference between int and Int32 in c# · Database
Part 12 Difference between int and Int32 in c#
Link for all dot net and sql server video tutorial playlists
http://www.youtube.com/user/kudvenkat/playlists
Link for slides, code samples and text version of the video
http://csharp-video-tutorials.blogspot.com/2014/08/part-12-difference-between-int-and.html
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 indentical.
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
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.