Differences between C # and Java variable naming (incorrect analysis indicates that C # is still learning)

Source: Internet
Author: User

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)

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.