Support us .Net Basics C# SQL ASP.NET Aarvi MVC Slides C# Programs Subscribe Download

Part 12 - Difference between int and Int32 in c#

Suggested Videos:
Part 9 - Call an abstract method from an abstract class constructor
Part 10 - What happens if finally block thows an exception
Part 11 - What is the difference between is and as keyword in c#



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.

7 comments:

  1. Dear Vankat
    In an interview I was asked to Give a practical example of Interface? Why we use interface instead of classes?
    I was know the interface but I can’t answer him because the purpose of interface was not cleared. Kindly upload the answer of this question.

    ReplyDelete
    Replies
    1. in c# multiple inheritance is not supported by the "classes".
      so
      by using interface instead of class,we can achieve multiple inheritance.
      please study about the multiple inheritance.

      Delete
    2. when you use Interface instead class you could achieve loose coupling between your objects. for example suppose you have class called LuxCar:ICAR and another one called Sportcar:ICAR and you want to use one of them to somewhere but you dont know wich one would be use yet and it would be depend on real time user decision so if you used some if/else statement to handle this situation you are defiantly treating against SOLID laws because if you want to add another class called Traditionalcar:ICAR you'd have to change your if/else statement and it means your class not closed to change and it's obviously against OPEN TO EXTEND AND CLOSE TO CHANGE.

      Delete
  2. Hi

    An interview is taken from me in which the interviewer stuck on DELEGATE, i satisfy him by
    definiation, declaration but when he asked Why we use delegate? give practical example?
    I watch your video i understand but reason and purpose is not cleare me.
    An other Question he asked why we use of Lambda Expression, i was again blank. He also asked about
    Interface uasge and its purpose.
    Please upload a video in which these questions are discussed. i really thank full to you!

    ReplyDelete
  3. Hi Venkat

    int is a C# syntax and INT32 is a Base Class Library syntax. This is a main difference between int and INT32.

    ReplyDelete
  4. Vankat Bro!
    it is mostly asked that when we use delegate and when we use interface?
    please up load the answer

    ReplyDelete
  5. yes this is the only difference i can think of.. int is c# syntax whereas int32 is from base class library of .NET framework

    ReplyDelete

It would be great if you can help share these free resources