Check and uncheck keyword usage in asp.net C #

Source: Internet
Author: User

1. A compilation of code not through the process

The code is as follows Copy Code
int a = Int. MaxValue * 2;

The above code snippet compilation does not pass the procedure, in VS2010 there will be a red wavy line to indicate that the code has a title: "The operation overflows at compile time in checked mode". This says that the compiler will check whether the math operation overflows at compile time. But the compilation can be checked out the overflow of the scene is limited to the application of constant operations. The code compiler in 2 will not be reported as a mistake.


2. A piece of compilation through the process but cannot get the correct results of the code

The code is as follows Copy Code
int temp = Int. MaxValue;
int a = temp * 2;
Console.Write (a);

I'm going to put the constant int. The value of the MaxValue is given to the temp, and then the variable is multiplied by the 2-plot result assigned to A; This code can be performed normally, and fulfillment results will output 2.

This affirms that at runtime the default scene is not to check whether arithmetic operations overflow, the CPU, in spite of the rules for it, the result of the error is not his fault.

Normal performance, and the results are wrong, this is a very dangerous scene, how to avoid this danger? Please look at 3


3. Apply checked keyword, alarm when overflow

The code is as follows Copy Code

int temp = Int. MaxValue;

Try

{

int a = checked (temp * 2);

Console.WriteLine (a);

}

catch (OverflowException)

{

Console.WriteLine ("Overflow, to deal with punishment Yo");

}

Apply the checked keyword to retouch temp*2 's plot results and apply a try catch to handle penalties when an overflow occurs. The above code will output: "Overflow, to deal with punishment yo"

The question is if a section of code has a lot of arithmetic operations need to do overflow examination, there will be many checked retouching expression, how to do? Please look at 4


4. Checked keyword can be polished a statement block, see the following code

The code is as follows Copy Code

int temp = Int. MaxValue;

Try

{

Checked

{

int num = TEMP/20;

int a = temp * 2;

int c = temp * 1000;

}

}

catch (OverflowException)

{

Console.WriteLine ("Overflow, to deal with punishment Yo");

}

The above results are output and 31 samples

5. Checked is effective in avoiding arithmetic overflows, so unchecked, does it work? The answer is inevitable, sometimes we do not need to correct the results of the plot, we just need a number, as the overflow does not overflow the relationship is not, for example, to generate an object of hashcode, for example, according to an algorithm to draw a relative random number, this is not necessary to correct results. The following code fragment

The code is as follows Copy Code

Class Person

{

public string Name {get; set;}

public string Title {get; set;}

public override int GetHashCode ()

{

return unchecked (Name.gethashcode () + Title.gethashcode ());

}

}

Unchecked can also retouch statement blocks, and they are used exactly as checked.

6. Checked and unchecked can be nested applications, of course, it doesn't make much sense. Whether the statement is checked to be nested checked or unchecked resolution


7. Checked keywords from IL

C # code:

The code is as follows Copy Code

static void Main (string[] args)

{

int a = Int. MaxValue;

int B = A * 2;

int c = Checked (A * 2);

int d = Unchecked (A + 3);

Console.read ();

}
Corresponding IL

. method private Hidebysig static void Main (string[] args) cil managed

{

. entrypoint

Code size num (0 x1a)

. maxstack 2

. Locals init ([0] int32 a,

[1] int32 B,

[2] int32 C,

[3] int32 D)

Il_0000:nop

IL_0001:LDC.I4 0 X7FFFFFFF

il_0006:stloc.0

il_0007:ldloc.0

il_0008:ldc.i4.2

Il_0009:mul

Il_000a:stloc.1

il_000b:ldloc.0

il_000c:ldc.i4.2

IL_000d:mul.ovf

Il_000e:stloc.2

il_000f:ldloc.0

il_0010:ldc.i4.3

Il_0011:add

Il_0012:stloc.3

Il_0013:call int32 [Mscorlib]system.console::read ()

Il_0018:pop

Il_0019:ret

}//End of method Program::main


See the red and green in the IL display code, you can see the application of checked, IL operation is mul.ovf not apply checked or apply unchecked when the IL operator is mul or add, without. ovf.


8. Checked or unchecked only the statements that affect the flank, and will not affect the code block that calls the function within the closing statement, as follows:

The code is as follows Copy Code

static void Main (string[] args)

{

int a = Int. MaxValue;

int B = 20;

Checked

{

int c = TestMethod (A, b);

Console.WriteLine (c);

}

}

static int TestMethod (int a, int b)

{

return a * b;

}

The above code will perform normally, the checked statement block does not have the influence.


9. Global Open or Closed checked compilation options

On the Project Properties page, select the Build tab, and then click the Advanced button to select the Check Math overflow option, as shown in the following illustration

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.