While i used this code i got the output as "The value is 0"

int var1 = 2147483647;// Maximum limit of int
         short var2;
         var2 =(short)var1;
         var2 +=1;
            
         Console.WriteLine("The value is {0}",var2);

I got error : Cannot implicitly convert type 'int' to 'short' while i tried to use var2 = var2 + 1; Instead of var2 + =1;

What is the reason behind this confusion ?

Read Me. Either cast the result of the expression, or use a larger type. Typically, there's nothing wrong with using int for all of your signed integer needs to conform to defaults, even if you don't plan to use the full 32-bit range.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.