Compile-time error when adding two short variables [C #]

Possible duplicate:
Blues integer sum, short + = short problem

I summed up my problem in the following snippet.I code there are two short vaiables, and I add these two variables to another short variable, but I get a compile-time error. Why is this so?

1.short x = 1, y = 1; 2.short z = x + y; 

Compile-time error on line 2 EDIT:

 If short+short=int then why int+int !=long 
+4
source share
6 answers

According to the specification short + short -> int . Do short z = (short)(x + y);

The best answer is given by Eric Lippert here: Whole blues summation, short + = short problem

+6
source

There is no addition operator for the short operator. The compiler automatically converts these values ​​to int to complete the append. Therefore, the type of the expression x + y will be int . When assigning an int expression to a short variable, a cast is required. Like this:

 short z = (short)(x + y); 

Note The following information is usually not needed.

If you are concerned about overflow in a validated context, do the following:

 short z = unchecked((short)(x + y)); 

This is usually not necessary, since unchecked is the default for most (or all) C # compilers, and this parameter is unlikely to ever change. If the assignment appears inside the checked statement, then presumably the person who writes the code knows what he is doing.

+2
source

This is documentary behavior.

You need to press short z = (short)(x + y);

+2
source

This is a "feature." Seriously, I posted a similar Microsoft question regarding byte math some time ago. I do not know if you can see my record without logging in, but the answer was:

https://connect.microsoft.com/VisualStudio/feedback/details/92880/byte-bit-wise-and-math-requires-work-around#details

It is by design and driven by the rules around digital advertising and there are no predefined operators for bytes. (It also helped me when I first came across this. ;-)

Here is an important part of the language specification. Although the example for multiplication, the same for the plus.

hope this helps

Santos

14.2.6 Numeric Promotions This subclause is informative. numerical advancement consists of automatically performing certain implicit operand conversions, predefined unary and binary numerical operators. Numerical promotion is not a clear mechanism, the effect of applying overload resolution to predefined operators. numerical promotion is not specifically affected by the evaluation of user agents, although user agents may have similar effects introduced. As an example of numeric promotion, consider the predefined binary implementations * operator: int operator * (int x, int y); uint operator * (uint x, uint y); long operator * (long x, long y); ULONG operator * (ulong x, ulong y); invalid operator * (long x, ulong y); invalid operator * (ulong x, long y); float operator * (float x, float y); double operator * (double x, double y); decimal operator * (decimal x, decimal y); When overload resolution rules (Β§14.4.2) are applied to this set of statements, the first of the statements should be selected for which implicit conversions exist from operand types. [Example: for operation b * s, where b is byte and s is short, overload resolution selects the * (int, int) operator as the best operator. Thus, the effect is that b and s are converted to int and the result type is int. Similarly, for operation I * d, where I am int and d is double, overload resolution selects the * (double, double) operator as the best operator. end of example] End of informative text.

+1
source

Two shorts MAY be done by INT, so it is not possible to implicitly use X + Y as short. change it to

 int z = x + y; 

and he will work

Based on the comments, I add the following code sample to fix some problems:

  class BasicMath { short s_max = short.MaxValue; // the max value for a short (or an Int16) is 32767 int i_max = int.MaxValue; // the max value for an int (or an Int32) is 2,147,483,647 long l_max = long.MaxValue; // the max value for a long (Int64) is 9,223,372,036,854,775,807 public int AddingShorts(short x, short y) { short addedvalues = (short)(x + y); //yes this will compile and run, but the result of 32767 + 32767 = -2 //short addedShorts = (short)s_max + s_max; int addedInts = i_max + i_max; //No, this doesn't require a cast, but it also achieves the spectaclar result of //2,147,483,647 + 2,147,483,647 = -2 return addedvalues; //casting a short to an int works implicitly (note the return type here is int, not short) //still if you pass in values of exceeding 32767 you will end up with -2 because attempting //cast as a short a value of greater than 32767 results in -2. } } 

Why does the compiler require short + short for (at least) int and not apply the same rule to ints? Ask Anders ... The fact is that this is the "rule that the compiler provides" on all days ending in "Y"

If you try to simply superimpose the sum of two short short positions on a short one, yes, it will be compiled, but it will also be subject to a result that you will not like.

Greetings

0
source

This is because the sum of the two Int16s is Int32, so you may need to throw:

  short z = (short)(x + y); 
0
source

Source: https://habr.com/ru/post/1347947/


All Articles