none
Convert type Int to Byte without declare Int RRS feed

  • Question

  • Hello,

    I don't have any Int type in this program but why do we have this error?

    Thanks

    Friday, October 11, 2019 6:07 PM

Answers

  • There is no addition of bytes in C#. In order to add the two bytes, the compiler promotes them to ints and adds the ints. Therefore, the result of the addition is an int. You can re-assign the result to a byte by using a cast:

    byte b3 = (byte)(b1 + b2);

    However, be aware that this may not produce the expected result in case the sum of the two bytes is a value larger than what you can store in one byte.

    • Marked as answer by Arash_89 Friday, October 11, 2019 7:12 PM
    Friday, October 11, 2019 6:54 PM
    Moderator