Why is the result of modulo operator (%) implicitly cast to the left side and not the right side in C #?

Take the following code:

long longInteger = 42;
int normalInteger = 23;
object rem = longInteger % normalInteger;

If it remis the remainder longInteger / normalInteger, shouldn't the remainder always be left with a smaller "int" divisor? However, in C #, the code above leads to what remmatters long.

Is it safe to convert remto intwithout data loss?

int remainder = Convert.ToInt32(rem);
+3
source share
3 answers

There is no overload of the modulo operator that accepts longand inttherefore intwill be converted to longto match another operand.

, CPU , . - . , long, .

, , int, int.

+4

, , , "normalInteger". , "rem" int. , rem int .

+1

, , . , .

0
source

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


All Articles