Phil Phil -4 years ago 58
C# Question

Does portability come to the cost of unchanging variable sizes in C#?

Is this true? For performance-intensive applications using C# on 64-bit systems, I found this surprising in my book:

C# strictly specifies a range and behavior for each value type.
Because of portability requirements, C# is uncompromising on this
account. For example, an int is the same in all execution
environments. There is no need to rewrite code to fit a specific
platform. Although strictly specifying the size of the value types may
cause a small loss of performance in some environments, it is
necessary in order to achieve portability.

Answer Source

Portability is 'more' guaranteed by the unchanging variable sizes in C# (or really, the .NET/CLR/ECMA-355 specifications, of which C# is just one language).

This is just one of many guarantees that ensure that code (and the resulting MSIL) are portable across compilation and run-time systems. On the other hand, making assumptions about the size of int in C is rather unportable.

The "cost" is only in certain implementations that must still provide the guarantee to correctly execute the resulting MSIL - and one requirement is a 64-bit long value. Not all systems have an associated "cost" and on an AMD64 system there is effectively zero "extra work" to do. Also, C# is not a 'bare metal' language.. and is not always the [most] appropriate language to use.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download