Using non-primitive variable type in C # can affect performance?

8

Using non-primitive variable type in C # can affect performance?

I've seen a lot of code in which, instead of using primitive C # types, many use types similar to other languages that IDE supports.

I already questioned a programmer why he did this and he told me it was because he liked the color combination, hehe ...

For example:

Primitivo  --> Utilizado
int        --> Int32;
string     --> String;
long       --> Int64;
    
asked by anonymous 30.01.2014 / 19:07

3 answers

12

No, these cases that you exemplified, int - > Int32 , for example, not only do not affect performance but it does not make any difference the use of one or the other.

int is just an alias for Int32 , just as string is for String and long is for Int64 .

Every time you type int , the compiler translates the code to into type System.Int32 .

However it is worth keeping a certain style when writing code, using different nicknames of the data type just for "liking the color combination" does not seem very wise since doubts such as yours can arise for who will read and keep the code.

    
30.01.2014 / 19:13
6

int is an alias for System.Int32 , such as string is for System.String .

They compile the same code, so technically there will be no performance difference between the two. C # alias list:

object:  System.Object
string:  System.String
bool:    System.Boolean
byte:    System.Byte
sbyte:   System.SByte
short:   System.Int16
ushort:  System.UInt16
int:     System.Int32
uint:    System.UInt32
long:    System.Int64
ulong:   System.UInt64
float:   System.Single
double:  System.Double
decimal: System.Decimal
char:    System.Char
    
30.01.2014 / 19:16
2

How can you see in this SOEN answer the difference between the types Int32 and int , which we can extend to String and Int64 .

The only difference is the increase in legibility of the code, in the case of Int32 and Int64 , allowing, where applicable, the developer performing maintenance on the code, not you need to worry, since it's already the size assured.

    
30.01.2014 / 19:17