The following is a test:
C # The Code is as follows, which passes normal compilation and is no different from Java
Using System; namespace Wrox. proCSharp. basics {public class ScopeTest {public static int Main () {for (int I = 0; I <10; I ++) {Console. writeLine (I);} // I is out of the range. For (int I = 9; I >= 0; I --) {Console. WriteLine (I) ;}// I is out of the range. Return 0 ;}}}
The java code is as follows and can be compiled normally
public static void main(String[] args) {for (int i = 0; i < 9; i++) {System.out.println(i);}for (int i = 9; i >= 0; i--) {System.out.println(i);}}
C # The code below cannot be compiled properly:
Using System; namespace Wrox. proCSharp. basics {public class ScopeTest {public static int Main () {for (int I = 0; I <10; I ++) {Console. writeLine (I);} // I is out of the range. Int I = 3; Console. ReadLine (); return 0 ;}}}
Compilation error, see:
<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + parts/parts = "brush: java;"> public static void main (String [] args) {for (int I = 0; I <9; I ++) {System. out. println (I);} int I = 3; System. out. println (I );}
C # learning. If there are any mistakes, please note that compiling is performed in the Visual Studio 2013 environment.
Through the above tests, let's talk about your opinion:
In C # and Java, local variables declared in for, while, or similar statements exist in the loop body.
C # You can declare a local variable with the same name out of the same scope after the out-of-the-loop mechanism is made. in Java, you can declare a local variable with the same name out of the local variable again! (The expression is a bit difficult. You can understand the instance)