I’ll take this opportunity to explain int16, int32 and int64 as well.
“int” is a data type keyword defined by the language, C#.
Int32 is a data type defined by the .NET Common Type System (CTS). I mentioned it as data type just for the understanding but it is actually a Struct.
When you declare a variable as int or Int32 it will point to System.Int32 only. int is the same as System.Int32 and when compiled it will turn into the same thing in IL. If you disassemble your code with ILDASM, you'll see that the underlying types are used. So there is no difference in it technically. However, for readability factor including myself, most developers suggest to use int to declare a variable and all MSDN sample code uses int.
System.Int32 is a signed integer. So the minimum and maximum capacity of int or Int32 would be -2147483648 and 2147483647 respectively.
How I calculated this?
Simple for Int32, -2^32/2+1 to 2^32/2. Similarly for Int64, -2^64/2+1 to 2^64/2.
Int16 & Int64
System.Int16 is defined as short in C#. It can store a minimum & maximum of -32,769 & 32,768
System.Int64 is defined as long in C#. It can store a minimum & maximum of -9,223,372,036,854,775,808 & 9,223,372,036,854,775,807 respectively.
Unsigned
You may use unsigned short, integer or long if you are sure that you will not store any negative values to the variable. By going for unsigned variables you can double the capacity of the data type. For uint the max value is, 2^32, for ulong it is 2^64.Following are the keywords and examples for using unsigned data types.
int a = 2147483647; // CORRECT
int a = 2147483648; // INCORRECT. THIS WILL THROW ERROR.
uint a = 2147483648; // CORRECT, BECAUSE WE ADDED u TO SPECIFY THIS AS UNSIGNED
Similarly you may use the same technique for short as ushort and long as ulong.
its very helpful...thnx
ReplyDeleteno.. describe in detail
Delete