Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can I pass 1 as a short, but not the int variable i?

People also ask

Can you pass a short as an int?

Yes, a short will be automatically promoted to an int.

Is 1 short or integer?

1 will be by default treated as an int in Java. So, when you're doing ((short)1), the you're passing 1 as a short parameter through the function. The receiving argument should be of a short type. Whereas, when you have short s =1, then it is obviously a short integer.

Can a short be assigned to an int without a cast?

Please note that the value of all integral types (int, long, byte, short, and char) can be assigned to a variable of the float data type without using an explicit cast, BUT a float value must be cast before it is assigned to a variable of any integral data type int, long, byte, short, or char.


The first two are constant expressions, the last one isn't.

The C# specification allows an implicit conversion from int to short for constants, but not for other expressions. This is a reasonable rule, since for constants the compiler can ensure that the value fits into the target type, but it can't for normal expressions.

This rule is in line with the guideline that implicit conversions should be lossless.

6.1.8 Implicit constant expression conversions

An implicit constant expression conversion permits the following conversions:

  • A constant-expression (§7.18) of type int can be converted to type sbyte, byte, short, ushort, uint, or ulong, provided the value of the constant-expression is within the range of the destination type.
  • A constant-expression of type long can be converted to type ulong, provided the value of the constant-expression is not negative.

(Quoted from C# Language Specification Version 3.0)


There is no implicit conversion from int to short because of the possibility of truncation. However, a constant expression can be treated as being of the target type by the compiler.

1? Not a problem: it’s clearly a valid short value. i? Not so much – it could be some value > short.MaxValue for instance, and the compiler cannot check that in the general case.


an int literal can be implicitly converted to short. Whereas:

You cannot implicitly convert nonliteral numeric types of larger storage size to short

So, the first two work because the implicit conversion of literals is allowed.


I believe it is because you are passing in a literal/constant in the first two, but there is not automatic type conversion when passing in an integer in the third.

Edit: Someone beat me to it!


The compiler has told you why the code fails:

cannot convert `int' expression to type `short'

So here's the question you should be asking: why does this conversion fail? I googled "c# convert int short" and ended up on the MS C# page for the short keyword:

http://msdn.microsoft.com/en-us/library/ybs77ex4(v=vs.71).aspx

As this page says, implicit casts from a bigger data type to short are only allowed for literals. The compiler can tell when a literal is out of range, but not otherwise, so it needs reassurance that you've avoided an out-of-range error in your program logic. That reassurance is provided by a cast.

Write((short)i)