Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any reason to use byte/short etc.. in C#?

Tags:

java

c#

types

Over the int type?

A lot of code either uses int with double/floats.

I know there is the likes of .NET mobile versions so byte/short comes into their own but for desktop apps is there any point?

When I did C++ work (Games programming) I was very aware of each data type I was using though I don't have this feeling in C#/Java work.

Would there be any benefit of me using a byte say if I know my loop will never go over the bounds of byte?

like image 531
Finglas Avatar asked May 25 '09 10:05

Finglas


People also ask

Is short better than int?

Using short can conserve memory if it is narrower than int , which can be important when using a large array. Your program will use more memory in a 32-bit int system compared to a 16-bit int system.

Is Short bigger than byte?

short datatype is the variable range is more than byte but less than int and it also requires more memory than byte but less memory in comparison to int. The compiler automatically promotes the short variables to type int, if they are used in an expression and the value exceeds their range.

Why byte is not used in Java?

You don't achieve any space saving by using byte or short in simple variables instead of int , because most Java implementations align stack variables and object members on word boundaries. However, primitive array types are handled differently; i.e. elements of boolean , byte , char and short arrays are byte aligned.


1 Answers

A single byte compared to a long won't make a huge difference memory-wise, but when you start having large arrays, these 7 extra bytes will make a big difference.

What's more is that data types help communicate developers' intent much better: when you encounter a byte length; you know for sure that length's range is that of a byte.

like image 118
Anton Gogolev Avatar answered Nov 10 '22 07:11

Anton Gogolev