First we build a C # program file Cs1.cs, and then build a Java source program file Cs1.java. Their contents are:
Cs1.cs:
Using System;
public class cs1{
public static void Main () {
Console.WriteLine ("I Am sunwen!");
Sunwen mysunwen=new Sunwen ();
Console.WriteLine (Mysunwen.name);
}
}
Class sunwen{
Public String name= "chenbin!";
}
Cs1.java:
Import system.*;
public class cs1{
public static void Main (String args[]) {
System.out.PRintln ("I am Sunwen,how is you!");
Sunwen mysunwen=new Sunwen ();
System.out.println (Mysunwen.name);
}
}
Class sunwen{
String name= "chenbin!";
}
OK, let's run the two programs. In the process of compiling, we find that C # is actually much faster than Java. (not to say m$) in fact, the output of the two programs is the same:
I am sunwen!
chenbin!
One important difference is that there is a line of PUBLC string name= "chenbin!", while in Java it is a string name= "chenbin!". If we remove this public from the Cs1.cs, it will produce an error, because in C #, without any scope modifier, the default is protect and cannot be accessed outside the class.
This is one of the important distinctions. There is also: if we cs1.cs this C # program in a public class CS1 to public class CS2, save, and then compile, you can see, the program runs normally. In Java, this is obviously not possible because Java rules , there can be only one public class in a file, and the name of the class must be exactly the same as the file name. This is another difference, in C #, it is the main method to locate the entry. If there is not a method named Main in a program, there will be "No entry errors found". Don't write main to the main yo, hehe, I often make such mistakes.
The above is the Sunwen tutorial----C # Advanced (11) content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!