Type casting needed for byte = byte - byte?

Posted by Vaccano on Stack Overflow See other posts from Stack Overflow or by Vaccano
Published on 2010-04-17T04:42:33Z Indexed on 2010/04/17 4:53 UTC
Read the original article Hit count: 392

Filed under:
|
|

I have the following code:

foreach (byte b in bytes)
{
    byte inv = byte.MaxValue - b;
    // Add the new value to a list....
}

When I do this I get the following error:

Cannot implicitly convert type 'int' to 'byte'. 
An explicit conversion exists (are you missing a cast?)

Each part of this statement is a byte. Why does C# want to convert the byte.MaxValue - b to an int?

Shouldn't you be able to do this some how without casting? (i.e. I don't want to have to do this: byte inv = (byte) (byte.MaxValue - b);)

© Stack Overflow or respective owner

Related posts about c#

Related posts about c#3.0