I am studing C# and this for me sound really strange...
Why to add to short
type I have to do an explicit conversion?
short x = 1, y = 1;
short z = x + y; // Compile-time error
Explicit Cast:
short z = (short) (x + y); // OK
I am studing C# and this for me sound really strange...
Why to add to short
type I have to do an explicit conversion?
short x = 1, y = 1;
short z = x + y; // Compile-time error
Explicit Cast:
short z = (short) (x + y); // OK
that shows the compilation error because because the arithmetic expression on the right-hand side of the assignment operator evaluates to int by default. So you would need to do type casting.
It is possible though to use plus operand with other data types like long int double, where the destination variable has the same storage size or a larger storage size but as short has smaller size, so you can't use it directly.