Reputation: 261
This code cannot be compiled because of overflowing:
byte number = 1000;
Console.WriteLine(number);
But this one gives a value of 1 because of the same reason as of overflowing:
byte number = 255;
number += 2;
Console.WriteLine(number);
My question is why the first example gives an error and the second one won't?
Upvotes: 0
Views: 137
Reputation: 6630
The discrepancy is due to the rules surrounding C#'s checked
and unchecked
contexts.
Constant values (those the compiler can determine at compile time, using well-defined rules of the C# specification) default to checked
. Attempting to assign a value out of range for a numeric type (in your case, assigning 1000
to a byte
, which has the range [0,255]
) will result in a compile time error. You can surpress this by wrapping the expression in an unchecked
context.
byte number = unchecked((byte) 1000);
Console.WriteLine(number);
results in 232
.
Non-constant values - i.e., those calculated at runtime - default to unchecked
. This allows overflow / underflow to occur. This blog post explains the rationale for choosing unchecked
as the default. You can choose to make an expression or block checked
, incurring a slight runtime performance penalty to have the runtime throw an exception if overflow / underflow occurs.
checked {
byte number = 255;
number += 2;
Console.WriteLine(number);
}
results in System.OverflowException
being thrown on the line with the +=
operation.
Or, you can set the -checked
compiler flag to default to checked
everywhere except places you mark unchecked
.
Upvotes: 0
Reputation: 7188
A byte
is 8 bits, and a bit can be on our off. Represented mathematically, this is either 0 or 1, off and on respectfully.
So if you put 8 bits in a row, you can start representing numbers in binary :
00000000 = 0
00000001 = 1
00000010 = 2
...
11111111 = 255
As other people have pointed out, when you add 2 to 255, it overflows and wraps back around :
11111111 (255)
+00000010 (2)
---------------
00000001 (1)
You need a larger primitive, one with at least 10 bits, to represent a number like 1000, and your compiler knows this and is trying to alert you.
Upvotes: 0
Reputation: 16079
byte
type is 0 to 255
In first example, When you are assigning number = 1000
it is exceeding limit of byte range and gives compile time error.
In second example, you are assigning number = 255
which is acceptable as 255
is in the byte range. When you are adding 2
to max byte limit i.e 255
, it start from 0
and prints 1
as a result
Upvotes: 1
Reputation: 219047
The first one doesn't compile because a byte
can't be set to 1000
. The second one does compile because a byte
can be set to 255
, and because a value will overflow when performing arithmetic that takes it beyond its bounds.
At no point in the second example do you try to set a byte
to 1000
. Each operation present in the second example is valid. But one operation in the first example is not valid.
Basically, the compiler isn't going to try to calculate your overflow for you. The runtime will handle an overflow.
Upvotes: 1