What is checked in the C # code?

12

I saw a code:

using (IEnumerator<TSource> e = source.GetEnumerator()) {
     checked {
         while (e.MoveNext()) count++;
     }
}

What is the purpose of the code checked {} in this C # code?

    
asked by anonymous 07.10.2016 / 19:10

2 answers

13

checked is used to determine whether the arithmetic overflow will be considered as an error . So if the value goes beyond the limit the type supports an exception will be thrown preventing the value from being used inadvertently. For performance reasons the default is to not issue an exception and let the code go wrong.

Obviously this mode is only active within the block delimited by the keys.

There are rare cases where it is necessary. It can be useful in cases where the problem will almost never occur, but when it does, it's best not to "hit it." The most common is to ensure that it does not burst. Curiously enough to check before is less performative since there is a reasonable cost to make a branch ( if ). Failure to throw the exception gets faster. Casting the exception will slow down . We should always avoid exceptions until it makes a lot of sense.

There is also unchecked to disregard errors. Obviously, if there is no error, the calculation can produce unexpected results. It is only required when the code is compiled with /checked on, changing the default, there if you want to turn off at some specific point you have to make the timely shutdown. It is almost never used.

The articles by Eric Lippert, who is authority on the subject, talks about the details. Part 1 and Part 2 .

See the sample documentation in dotNetFiddle and in CodingGround .

    
07.10.2016 / 19:17
5

The keyword checked is used to explicitly allow overflow verification of integer types arithmetic operations and conversions.

By default, an expression that contains only constant values causes a compiler error if the expression produces a value that is outside the range of the target type. If the expression contains one or more non-constant values, the compiler does not detect the overflow. Example:

int x = int.MaxValue;  
int y = x + x;          
Console.WriteLine(y);   

Placing this snippet in a% block% block prevents overflow, and instead the runtime throws an OverflowException:

checked 
{ 
     int x = int.MaxValue; 
     int y = x + x; 
     Console.WriteLine(y); 
} 

You can handle the exception by adding blocks checked try :

checked 
{ 
    try 
    { 
        int x = int.MaxValue; 
        int y = x + x; 
        Console.WriteLine(y); 
    } 
    catch (Exception  exception) 
    { 
        Console.WriteLine(exception); 
    } 
} 

The unchecked keyword is used to suppress the overflow check for arithmetic operations of integral type and conversions.

In a catch context, if an expression produces a value that is outside the target type range, the overflow is not flagged.

The "ideal" solution would be to use unchecked together:

checked 
{ 
    try 
    { 
        int x = int.MaxValue; 
        int y = x + x; 
        Console.WriteLine (y); 
    } 
    catch (Exception  exception) 
    { 
        Console.WriteLine (exception); 
    } 

    unchecked 
    {
        int z = 2 + 10 ; 
        Console.WriteLine(z); 
    } 

} 
    
07.10.2016 / 19:49