Can using non-priminal variable type in C# affect performance?

Asked

Viewed 920 times

8

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

I have seen several codes where, instead of using primitive C#types, many use types similar to other languages that the IDE accepts.

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

For example:

Primitivo  --> Utilizado
int        --> Int32;
string     --> String;
long       --> Int64;
  • 2

    Note that in C# there is not exactly the concept of primitive variable. What you are talking about is just one alias the real kind that is always a object, in some cases more precisely a type derived from struct such as the int or the long.

3 answers

12


No, these cases you exemplified, int -> Int32, for example, not only does not affect performance but makes no difference the use of either.

int is only a nickname (alias) for Int32, just as string is for String and long is for Int64.

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

However it is worth maintaining a certain style when writing code, using different nicknames of the type of data just for "like the combination of colors" does not seem to me very sensible since doubts like yours may arise for those who will read and maintain the code.

6

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

They compile the same code, so technically there will be no difference in performance between the two. List of aliases of C#:

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

2

As you can see in that SOEN response the difference between the types Int32 and int, what we can extend to String and Int64.

The only difference is increase in readability in the case of Int32 and Int64, allowing, when relevant, the developer who is carrying out the maintenance in the code, not to worry, since it already has the guaranteed size.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.