2

I have the following code -

using System;

namespace MyApp {
class Program {
    static void Main() {
        byte x = 10, y = 20;
        Console.WriteLine(x.GetType());
        Console.WriteLine(y.GetType());
        //byte z = (x+y); Error: (9,14): error CS0266: 
        //Cannot implicitly convert type 'int' to 'byte'. 
        //An explicit conversion exists (are you missing a cast?)
        byte z = (byte)(x+y);
        Console.WriteLine(z.GetType());
        Console.WriteLine(x);
        Console.WriteLine(y);
        Console.WriteLine(z);
    }
}
}

Output:

System.Byte
System.Byte
System.Byte
10
20
30

But instead of explicit type casting if byte z = x+y is used it generates the error -

(9,14): error CS0266: Cannot implicitly convert type 'int' to 'byte'. An explicit conversion exists (are you missing a cast?)

But as shown in the code, compiler reads x and y as byte.

So, why addition of two bytes is generating int requiring explicit type casting?

puregeek
  • 175
  • 3
  • 9
  • 3
    Because `x+y` is actually an `int`. See [byte + byte = int… why?](https://stackoverflow.com/q/941584/3744182) and [Why is a cast required for byte subtraction in C#?](https://stackoverflow.com/q/927391/3744182). – dbc Dec 19 '17 at 22:38

1 Answers1

2

This is an example of an Implicit Conversion.

This link explains further:

https://blogs.msdn.microsoft.com/oldnewthing/20040310-00/?p=40323

People complain that the following code elicits a warning:

byte b = 32; byte c = ~b; // error CS0029: Cannot implicitly convert type 'int' to 'byte'

"The result of an operation on 'byte' should be another 'byte', not an 'int'," they claim.

Be careful what you ask for. You might not like it.

Suppose we lived in a fantasy world where operations on 'byte' resulted in 'byte'.

byte b = 32; byte c = 240; int i = b + c; // what is i?

In this fantasy world, the value of i would be 16! Why? Because the two operands to the + operator are both bytes, so the sum "b+c" is computed as a byte, which results in 16 due to integer overflow. (And, as I noted earlier, integer overflow is the new security attack vector.)

Similarly,

int j = -b;

would result in j having the value 224 and not -32, for the same reason. ... In our imaginary world, this code would return incorrect results once the total number of games played exceeded 255. To fix it, you would have to insert annoying int casts.

return ((int)captain.Wins + cocaptain.Wins) >= ((int)captain.Games + cocaptain.Games) / 2;

So no matter how you slice it, you're going to have to insert annoying casts. May as well have the language err on the side of safety (forcing you to insert the casts where you know that overflow is not an issue) than to err on the side of silence (where you may not notice the missing casts until your Payroll department asks you why their books don't add up at the end of the month).

paulsm4
  • 114,292
  • 17
  • 138
  • 190
  • 3
    int x = 2147483647; int y = 2147483647; int z = x+y; This will result in overflow as well. So why nobody is worried here? @paulsm4 – puregeek Dec 19 '17 at 22:48
  • 1
    Because Raymond thinks that int overflows are normal. The liability of being a C++ programmer ;) – Hans Passant Dec 19 '17 at 22:56