Part 12 Difference between int and Int32 in c#

8
1



Link for all dot net and sql server video tutorial playlists

Link for slides, code samples and text version of the video

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.

Nguồn:https://wijstaanvooronzegrondrechten.org/

Xem Thêm Bài Viết Khác:https://wijstaanvooronzegrondrechten.org/cong-nghe

8 COMMENTS

  1. In Visual Studio 2015 you will no longer get an error if you try to declare an enum which inherit from Int32. I don't know if it was already fixed in VS 2012 og 2013.

  2. Can you please explain these?
    1.Using statement in C#
    2.Enable javascript intellisense in VS 2010 ,
    3.select fourth div in jquery and give some colour .

  3. Hi Venkat, what do you think of codility tests? Could you do some videos on how approach and solve the different codility tests. Thanks

LEAVE A REPLY

Please enter your comment!
Please enter your name here