Take the 2-minute tour ×

Previously today I was trying to add two ushorts and I noticed that I had to cast the result back to ushort. I thought it might've become an uint (to prevent a possible unintended overflow?), but to my surprise it was an int (System.Int32).

Is there some clever reason for this or is it maybe because int is seen as the 'basic' integer type?


ushort a = 1;
ushort b = 2;

ushort c = a + b; // <- "Cannot implicitly convert type 'int' to 'ushort'. An explicit conversion exists (are you missing a cast?)"
uint d = a + b; // <- "Cannot implicitly convert type 'int' to 'uint'. An explicit conversion exists (are you missing a cast?)"

int e = a + b; // <- Works!

Edit: Like GregS' answer says, the C# spec says that both operands (in this example 'a' and 'b') should be converted to int. I'm interested in the underlying reason for why this is part of the spec: why doesn't the C# spec allow for operations directly on ushort values?

share|improve this question
bytes.com/topic/c-sharp/answers/… (see J.Skeets answer) –  Tim Schmelter Apr 8 '12 at 18:36
I have updated my answer from c# specifications from microsoft –  Habib Apr 14 '12 at 20:14
add comment

5 Answers

up vote 21 down vote accepted

The simple and correct answer is "because the C# Language Specification says so".

Clearly you are not happy with that answer and want to know "why does it say so". You are looking for "credible and/or official sources", that's going to be a bit difficult. These design decisions were made a long time ago, 13 years is a lot of dog lives in software engineering. They were made by the "old timers" as Eric Lippert calls them, they've moved on to bigger and better things and don't post answers here to provide an official source.

It can be inferred however, at a risk of merely being credible. Any managed compiler, like C#'s, has the constraint that it needs to generate code for the .NET virtual machine. The rules for which are carefully (and quite readably) described in the CLI spec. It is the Ecma-335 spec, you can download it for free from here.

Turn to Partition III, chapter 3.1 and 3.2. They describe the two IL instructions available to perform an addition, add and add.ovf. Click the link to Table 2, "Binary Numeric Operations", it describes what operands are permissible for those IL instructions. Note that there are just a few types listed there. byte and short as well as all unsigned types are missing. Only int, long, IntPtr and floating point (float and double) is allowed. With additional constraints marked by an x, you can't add an int to a long for example. These constraints are not entirely artificial, they are based on things you can do reasonably efficient on available hardware.

Any managed compiler has to deal with this in order to generate valid IL. That isn't difficult, simply convert the ushort to a larger value type that's in the table, a conversion that's always valid. The C# compiler picks int, the next larger type that appears in the table. Or in general, convert any of the operands to the next largest value type so they both have the same type and meet the constraints in the table.

Now there's a new problem however, a problem that drives C# programmers pretty nutty. The result of the addition is of the promoted type. In your case that will be int. So adding two ushort values of, say, 0x9000 and 0x9000 has a perfectly valid int result: 0x12000. Problem is: that's a value that doesn't fit back into an ushort. The value overflowed. But it didn't overflow in the IL calculation, it only overflows when the compiler tries to cram it back into an ushort. 0x12000 is truncated to 0x2000. A bewildering different value that only makes some sense when you count with 2 or 16 fingers, not with 10.

Notable is that the add.ovf instruction doesn't deal with this problem. It is the instruction to use to automatically generate an overflow exception. But it doesn't, the actual calculation on the converted ints didn't overflow.

This is where the real design decision comes into play. The old-timers apparently decided that simply truncating the int result to ushort was a bug factory. It certainly is. They decided that you have to acknowledge that you know that the addition can overflow and that it is okay if it happens. They made it your problem, mostly because they didn't know how to make it theirs and still generate efficient code. You have to cast. Yes, that's maddening, I'm sure you didn't want that problem either.

Quite notable is that the VB.NET designers took a different solution to the problem. They actually made it their problem and didn't pass the buck. You can add two UShorts and assign it to an UShort without a cast. The difference is that the VB.NET compiler actually generates extra IL to check for the overflow condition. That's not cheap code, makes every short addition about 3 times as slow. But otherwise the reason that explains why Microsoft maintains two languages that have otherwise very similar capabilities.

Long story short: you are paying a price because you use a type that's not a very good match with modern cpu architectures. Which in itself is a Really Good Reason to use uint instead of ushort. Getting traction out of ushort is difficult, you'll need a lot of them before the cost of manipulating them out-weighs the memory savings. Not just because of the limited CLI spec, an x86 core takes an extra cpu cycle to load a 16-bit value because of the operand prefix byte in the machine code. Not actually sure if that is still the case today, it used to be back when I still paid attention to counting cycles. A dog year ago.

share|improve this answer
"That isn't difficult, simply convert the ushort to an uint, a conversion that's always valid." Indeed, that is what I thought as well, but it won't be converted to an uint, but to an int, which no one has been able to really explain yet. A very nice answer, though! –  lesderid Apr 15 '12 at 7:48
Yes, I flubbed that, fixed. Look at the table I pointed to, it doesn't have unsigned types. –  Hans Passant Apr 15 '12 at 14:27
Ah, I see. I guess that answers my question! I'm awarding Habib.OSU the bounty though, as he fully answered the original question, before I edited it. –  lesderid Apr 15 '12 at 15:24
Excellent read; you certainly deserve that checkmark! I guess this is another case of why premature optimization (using ushort instead of just uints) is evil. –  Kyle Baran Jan 18 at 5:03
add comment
ushort x = 5, y = 12;


ushort z = x + y;   // Error: conversion from int to ushort


In case of arithmetic operations on ushort, the operands are converted to a type which can hold all values. So that overflow can be avoided. Operands can change in the order of int, uint, long and ulong. Please see the C# Language Specification In this document go to section 4.1.5 Integral types (around page 80 in the word document). Here you will find:

For the binary +, –, *, /, %, &, ^, |, ==, !=, >, <, >=, and <= operators, the operands are converted to type T, where T is the first of int, uint, long, and ulong that can fully represent all possible values of both operands. The operation is then performed using the precision of type T, and the type of the result is T (or bool for the relational operators). It is not permitted for one operand to be of type long and the other to be of type ulong with the binary operators.

Eric Lipper has stated in a question

Arithmetic is never done in shorts in C#. Arithmetic can be done in ints, uints, longs and ulongs, but arithmetic is never done in shorts. Shorts promote to int and the arithmetic is done in ints, because like I said before, the vast majority of arithmetic calculations fit into an int. The vast majority do not fit into a short. Short arithmetic is possibly slower on modern hardware which is optimized for ints, and short arithmetic does not take up any less space; it's going to be done in ints or longs on the chip.

share|improve this answer
That still doesn't explain why it becomes an int and not a uint. –  lesderid Apr 15 '12 at 7:42
@lesderid, well this is due to the operator + overloading precedence defined in the c# specification (7.8.4, page 192). In this the first overloaded signature is int operator+(int x, int y), and after that its uint operator+(uint x, uint y). So that is why it becomes int not uint –  Habib Apr 15 '12 at 13:42
add comment

From the C# language spec: Binary numeric promotions Binary numeric promotion occurs for the operands of the predefined +, –, *, /, %, &, |, ^, ==, !=, >, <, >=, and <= binary operators. Binary numeric promotion implicitly converts both operands to a common type which, in case of the non-relational operators, also becomes the result type of the operation. Binary numeric promotion consists of applying the following rules, in the order they appear here:

· If either operand is of type decimal, the other operand is converted to type decimal, or a binding-time error occurs if the other operand is of type float or double.

· Otherwise, if either operand is of type double, the other operand is converted to type double.

· Otherwise, if either operand is of type float, the other operand is converted to type float.

· Otherwise, if either operand is of type ulong, the other operand is converted to type ulong, or a binding-time error occurs if the other operand is of type sbyte, short, int, or long.

· Otherwise, if either operand is of type long, the other operand is converted to type long.

· Otherwise, if either operand is of type uint and the other operand is of type sbyte, short, or int, both operands are converted to type long.

· Otherwise, if either operand is of type uint, the other operand is converted to type uint.

· Otherwise, both operands are converted to type int.

share|improve this answer
That indeed confirms my findings, but it doesn't really explain why it happens. –  lesderid Apr 8 '12 at 18:47
@lesderid: I don't understand. It happens because you are using a compiler that complies with the C# spec. Perhaps you are asking the reasoning behind section, and why the rules stop at int and don't keep going to smaller types? –  GregS Apr 8 '12 at 18:50
Well yes, I'm interested in why the spec says that both operands should be converted to int. I merely added the compiler errors to clarify and because I wasn't sure if this was a spec or an implementation decision. –  lesderid Apr 8 '12 at 18:54
I can guess but I don't really know the answer, so you should edit your question to ask why the language rules are that way. –  GregS Apr 8 '12 at 19:22
Thanks for your input! I edited the question accordingly. –  lesderid Apr 8 '12 at 19:30
add comment

There is no reason that is intended. This is just an effect or applying the rules of overload resolution which state that the first overload for whose parameters there is an implicit conversion that fit the arguments, that overload will be used.

This is stated in the C# Specification, section 7.3.6 as follows:


It goes on illustrating with an example:


int operator *(int x, int y);

uint operator *(uint x, uint y);

long operator *(long x, long y);

ulong operator *(ulong x, ulong y);

float operator *(float x, float y);

double operator *(double x, double y);

decimal operator *(decimal x, decimal y);

When overload resolution rules (§7.5.3) are applied to this set of operators, the effect is to select the first of the operators for which implicit conversions exist from the operand types. For example, for the operation b * s, where b is a byte and s is a short, overload resolution selects operator *(int, int) as the best operator.

share|improve this answer
add comment

Your question is in fact, a bit tricky. The reason why this specification is part of the language is... because they took that decision when they created the language. I know this sounds like a disappointing answer, but that's just how it is.

However, the real answer would probably involve many context decision back in the day in 1999-2000. I am sure the team who made C# had pretty robust debates about all those language details.

  • ...
  • Support for internationalization is very important.
  • ...

The quote above is from Wikipedia C#

All of those design goals might have influenced their decision. For instance, in the year 2000, most of the system were already native 32-bits, so they might have decided to limit the number of variable smaller than that, since it will be converted anyway on 32-bits when performing arithmetic operations. Which is generally slower.

At that point, you might ask me; if there is implicit conversion on those types why did they included them anyway? Well one of their design goals, as quoted above, is portability.

Thus, if you need to write a C# wrapper around an old C or C++ program you might need those type to store some values. In that case, those types are pretty handy.

That's a decision Java did not make. For instance, if you write a Java program that interacts with a C++ program in which way your are received ushort values, well Java only has short (which are signed) so you can't easily assign one to another and expect correct values.

I let you bet, next available type that could receive such value in Java is int (32-bits of course). You have just doubled your memory here. Which might not be a big deal, instead you have to instantiate an array of 100 000 elements.

In fact, We must remember that those decision are been made by looking at the past and the future in order provide a smooth transfer from one to another.

But now I feel that I am diverging of the initial question.

So your question is a good one and hopefully I was able to bring some answers to you, even though if I know that's probably not what you wanted to hear.

If you'd like, you could even read more about the C# spec, links below. There is some interesting documentation that might be interesting for you.

Integral types

The checked and unchecked operators

Implicit Numeric Conversions Table

By the way, I believe you should probably reward habib-osu for it, since he provided a fairly good answer to the initial question with a proper link. :)


share|improve this answer
add comment

Your Answer


