What's wrong with this expression? Cannot implicitly convert type 'int' to 'byte'

后端 未结 5 1205
后悔当初
后悔当初 2020-12-19 02:29

I am getting the error \"Cannot implicitly convert type \'int\' to \'byte\'. An explicit conversion exists (are you missing a cast?)\". Doesn\'t byte + byte = byte

5条回答
  •  借酒劲吻你
    2020-12-19 02:42

    http://msdn.microsoft.com/en-us/library/5bdb6693(VS.71).aspx

    byte + byte = int

    More accurately framework doesn't define operator + on byte, but there is an implicit conversion from byte to int, to

    byte + byte = int + int = int

    I don't quite agree with the justification for this being that it may overflow, since so may int + int. But obviously byte arithmetic is far more 'dangerous' in this respect - and this behaviour forces you to take a close look at what you are doing.

提交回复
热议问题