Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why int and long types are first-class and favored by C# and the runtime?

In Numeric Types topic in Chapter 2 (C# 5.0 in a Nutshell) I found this phrase:

enter image description here

Why int and long are first-class citizens and favored by both C# and the runtime? and, Why the other types are used for interoperability?

like image 795
InfZero Avatar asked Jul 08 '13 23:07

InfZero


2 Answers

Because of the way modern processors are constructed. 32-bit and 64-bit processors are especially optimized to handle 32-bit and 64-bit integers. This is called the word size:

Modern processors, including embedded systems, usually have a word size of 8, 16, 24, 32 or 64 bits, while modern general purpose computers usually use 32 or 64 bits.

This means that a processor can perform arithmetic with these types with more ease than it can the other types. A byte or short, despite theoretically taking up less space in memory, are still loaded into 32-bit or 64-bit registers on the processor (depending on the architecture).

like image 67
p.s.w.g Avatar answered Sep 20 '22 08:09

p.s.w.g


The preference of int and long over uint and ulong is completely arbitrary.

There's no technical reason why unsigned integers would be harder to support than signed integers; if anything, signed integers would be harder to support in hardware.

Furthermore, "compatibility" isn't even a reason, because when .NET was designed, there was nothing to stay compatible with!

So signed types are preferred because they felt like it.
They could very well have designed the CLS to include unsigned integers, and it would've worked too.

like image 34
user541686 Avatar answered Sep 21 '22 08:09

user541686