Let's talk about variable scope!

Source: Internet
Author: User
Before writing the program, I only knew that the variables should be initialized before they were used, but I did not think carefully about the reason until yesterday I encountered the following problem by accident: static void main (string [] ARGs)
{
Int val;

For (INT I = 0; I <10; I ++)
{
Val = I + I;
}

// {Val = 1 ;}

// A local variable with no value is displayed.
// Console. writeline (VAL );

Console. readkey ();
}

Tip: the unassigned local variable "Val" is used"

So compare with the following code: static void main (string [] ARGs)
{
Int I;

For (I = 0; I <10; I ++)
{
}

Console. writeline (I );

Console. readkey ();
}

The Int I variable is also declared outside the for loop, but the I value can be output through console. writeline. By viewing the decompiled il code, we can find that the variable I is initialized outside the loop BODY OF THE for structure. The IL code is as follows:

. Locals Init ([0] int32 I,
[1] bool CS $4 $0000)
Il_0000: NOP
Il_0001: LDC. i4.0 // output Stack
Il_0002: stloc.0 // press the stack and assign 0 to variable I
// Enter the loop body only below
Il_0003: Br. s il_000b
Il_0005: NOP
Il_0006: NOP
Il_0007: ldloc.0
Il_0008: LDC. i4.1
Il_0009: add
Il_000a: stloc.0
Il_000b: ldloc.0
Il_000c: LDC. i4.s 10
Il_000e: CLT
Il_0010: stloc.1
Il_0011: ldloc.1
Il_0012: brtrue. s il_0005
// The loop ends.
Il_0014: ldloc.0
Il_0015: Call void [mscorlib] system. Console: writeline (int32)

In the above example, the int Val variable is actually initialized within the loop body of the for structure. You can find the answer by referring to "C # Getting Started classic". The original Article in the book is referenced below:
"In fact, only one simple variable type is declared for any variable and will not cause other changes. This value will only occupy a piece of memory space after the variable is assigned a value.If this activity occupies the memory space in a loop, the value is actually defined as a local value, which is out of its scope outside the loop., Thus losing its role. An external value assignment in the loop ensures that the value is a partial value of the Subject Code and is still in the scope within the loop ."

If you comment out the console in the first code above. writeline (VAL); this statement can be compiled and adjusted to the value of the variable Val through the "instant window" after the for loop ends. ()

In fact, CLR allows any code with correct syntax. In fact, this is only the logic check function of the C # compiler. It ensures the correctness of the program. It thinks that for can be neither cyclically nor cyclically, in this way, variables can be used without value assignment, so that the program may have unexpected effects from programmers. This is a helper function of the compiler. Of course, you can also set compiler options, this problem can be avoided by lowering the code security check standards of the compiler, but this is generally not recommended.

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.