Checked and Unchecked operators don’t seem to be working when…

T

trubar a

hi

1) Is UNCHECKED operator in effect only when expression inside
UNCHECKED context uses an explicit cast ( such as byte
b1=unchecked((byte)2000); ) and when conversion to particular type can
happen implicitly? I’m assuming this since the following expression
throws a compile time error:

byte b1=unchecked(2000); //compile time error

2)

a) Do CHECKED and UNCHECKED operators work only when resulting value
of an expression or conversion is of an integer type? I’m assuming
this since in the first example ( where double type is being converted
to integer type ) CHECKED operator works as expected:

double m = double.MaxValue;
b = checked( (byte) m ); // reports an exception

, while in second example ( where double type is being converted to a
float type ) CHECKED operator doesn’t seem to be working. since it
doesn't throw an exception:

double m = double.MaxValue;
float f = checked( (float) m ); // no exception thrown

b) Why don’t the two operators also work with expressions where type
of a resulting value is of floating-point type?

2) Next quote is from Microsoft’s site:

"The unchecked keyword is used to control the overflow-checking
context for integral-type arithmetic operations and conversions"

I’m not sure I understand what exactly have expressions and
conversions such as unchecked( (byte) (100+200) ); in common with
integrals?

Thank you
 
G

Göran Andersson

hi

1) Is UNCHECKED operator in effect only when expression inside
UNCHECKED context uses an explicit cast ( such as byte
b1=unchecked((byte)2000); ) and when conversion to particular type can
happen implicitly? I’m assuming this since the following expression
throws a compile time error:

byte b1=unchecked(2000); //compile time error

It's the assignment that causes the overflow, not the literal value
itself, and the assignment is outside the unchecked context.
2)

a) Do CHECKED and UNCHECKED operators work only when resulting value
of an expression or conversion is of an integer type? I’m assuming
this since in the first example ( where double type is being converted
to integer type ) CHECKED operator works as expected:

double m = double.MaxValue;
b = checked( (byte) m ); // reports an exception

, while in second example ( where double type is being converted to a
float type ) CHECKED operator doesn’t seem to be working. since it
doesn't throw an exception:

double m = double.MaxValue;
float f = checked( (float) m ); // no exception thrown

Casting a double to float doesn't cause an overflow. If the value is
outside the range that the float can handle, the result is infinity or
negative infinity. This is just a loss of precision, not an overflow.
b) Why don’t the two operators also work with expressions where type
of a resulting value is of floating-point type?

As floating point values have a representation for infinite values, they
simply can't overflow.
2) Next quote is from Microsoft’s site:

"The unchecked keyword is used to control the overflow-checking
context for integral-type arithmetic operations and conversions"

I’m not sure I understand what exactly have expressions and
conversions such as unchecked( (byte) (100+200) ); in common with
integrals?

The term integral in this case comes from abstract algebra, not
calculus, and means integer numbers.
 
T

trubar a

As with every other construct in C# that uses parentheses, the thing
outside the parentheses applies only to those things inside the parentheses.

So, in your example above, the "unchecked" happily allows the constant
2000, but then you still have an "int" constant as the result of the
"unchecked" expression, which is still not valid to assign to a "byte".

Try:

   byte b1 = unchecked((byte)2000);

a) Couldn’t same be argued about checked operator? Namely, that
checked also allows the constant 2000 and the result of an error is
actually assignment of int value to variable of type byte.?

b) The following gives me an overflow error:

long l = checked (int.MaxValue + 16);

Since there was no casting, what exactly did overflow? I realize the
expression inside checked context was of int type and that result
exceeded the max value that int type can hold, but instead of
overflowing why didn’t expression automatically get converted to
long type?


c) “If neither checked nor unchecked is used, a constant expression
uses the default overflow checking at compile time, which is checked.”

If I understand the above quote correctly, then at compile time it is
as if our whole program code is inside some “invisible” checked
context, which monitors explicit/ implicit conversions of constants
( which are part of constant expression ) and it is thus this checked
context that actually throws an error when the statements like byte b1
= 2000; are encountered:
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top