Why is this outputting 0 instead of throwing an error | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Why is this outputting 0 instead of throwing an error

byte b = 255; b += 1; Console.Write(b); // 0 Any answer is appreciated.

28th Aug 2021, 4:21 AM
Tim
Tim - avatar
11 Answers
+ 5
Nick error is only thrown in checked context , otherwise it truncates the higher order bits as this article says, https://docs.microsoft.com/en-us/dotnet/api/system.overflowexception?view=net-5.0
28th Aug 2021, 4:45 AM
Abhay
Abhay - avatar
+ 2
https://code.sololearn.com/ck3yC93hsWIA/?ref=app
28th Aug 2021, 4:34 AM
Vtec Fan
Vtec Fan - avatar
+ 2
Rushikesh byte in C# is an unsigned value
28th Aug 2021, 4:34 AM
Tim
Tim - avatar
+ 2
Rushikesh I appreciate the answers that you've given, but this thread has nothing to do with Java if you take a look at the tag.
28th Aug 2021, 4:41 AM
Tim
Tim - avatar
+ 1
In c# sbyte - -128 to 127 Byte 0-255
28th Aug 2021, 4:42 AM
Vtec Fan
Vtec Fan - avatar
+ 1
Rushikesh But can you explain why the integer didn't overflow
28th Aug 2021, 4:43 AM
Tim
Tim - avatar
0
Nick iam talking about java for just to know about byte size
28th Aug 2021, 4:40 AM
Vtec Fan
Vtec Fan - avatar
0
That means your adding one to 255(heighest byte value) Which is not possible
28th Aug 2021, 4:45 AM
Vtec Fan
Vtec Fan - avatar
0
Byte can only have max 255. sbyte can have -128-128. Use int or long.
1st Sep 2021, 3:05 PM
GDSHNIK
GDSHNIK - avatar
- 1
Byte highest value is just 127 to -128🤔 but it can go to 255 266 is out of range according to file sizes
28th Aug 2021, 4:32 AM
Vtec Fan
Vtec Fan - avatar
- 1
C# does not check overflow exceptions like any other languages
28th Aug 2021, 4:48 AM
Vtec Fan
Vtec Fan - avatar