What is the difference between int16, int32 and int64 in C#?
The difference between all of these data types is the size and the capacity.
When I say the capacity you can think about
Int16 is like two beer bottles, Int32 is like 4 beer bottles and int 64 is like 8 beer bottles.
What do I mean by 2 beer bottles that means this is 2 bytes,
this is of 4 bytes and this is of 8 bytes, that's the capacity. Int16 can store value maximum upto
2 bytes, int 32 can store value maximum like 4 bytes and int64 can store upto 8 bytes.
What is the size exactly? Let us try to get the size of
int16 , say Console.WriteLine Int16 what is maximum value you can store
and what is the minimum value. In the same way you can put a Console.WriteLine for int32.
For Int32 what is the max and min value. And in the same what is the max value for int64 and
min value for 64. If I run this program you will see some maximum and minimum values.
Int16 can range from 32767 to -32768. All int16, int32
and int64 are signed numbers means they can have both positive values as well
as negative values. How did this value derive? Remember, I said 2, 4 and 8 beer bottles so when
you say 2 bytes so here it says 2 =power(2,16) , 1 bytes=8 bit so 2 bytes=16. The size maximum it
can store is 65536 but we see 32767 so divide this 65536/2 you can see we have 32768. From
-32768 to 32768, if we look at postive sign it has 32767 one byte less because for the sign
so for this sign (-) 1 byte needs to be reserve, that is why the value is from 32767 to -32768.
In the same way you can calculate for other things for 4 beer bottles, we can say 4 =power(4,16)
is 4294..... then divide by and that is 214748..... and -1 for the sign.
Basically the difference between int16, int32 and int64 is the size. One is of 2 bytes,
another can store the maximum value of 4 bytes and other one can store the maximum value of 8
bytes. If you ever try to put value which is more than this. If I copy this 32768, you can see it is
trying to give an exception saying i can't store this and if i make it 32767 and it can do this.
Any value which is more than its capacity it will start give an exception. All of these values are
small peg, medium peg and large peg. It is very difficult to remember this int16,
int32 and int64 what C# people have done is they have created Synonyms you can say this as Short,
this as int and this as long these are alias. Also if you are thinking about I don't want the
signed I want it to be unsigned . These signed number but there are situations where you are
not really interested in the negative values. You can use u version of this, we have something
called as ushort, uint and ulong. In the same way we have something called as uInt16, uInt32
and uInt64. u stands for values only from 0 to the +ve sign u means unsigned not the negative value.
Summing up what's the difference between Int16, Int32 and Int64 is the capacity of 2, 4 and 8.
This int is an alias to Int32, short, int and long are alias they are like user friendly words.
Finally, we also have unsigned version of this something called as ushort,
uint, ulong and so on.If you really like this video hit the subscribe button and also
visit the site questpond.com which has lots of these kind of videos which are related to MVC,
.NET Core, fundametals, SQL Server. Click the subscribe button and stay connected with me.
Happy learning! Thank you very much!