Tutorial Sunwen Tutorial----C # Advanced
(11)
Mrfat@china.com
OK, I came back, I wrote so much in front of, and compared with Java enough, now I use the example of my own experiment, to see the C # This is called java# of the next generation of programming languages and Java how much difference.
First we set up a C # program file Cs1.cs, and then build a Java source program file Cs1.java. Their content is:
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 are you!");
Sunwen mysunwen=new Sunwen ();
System.out.println (Mysunwen.name);
}
}
Class sunwen{
String name= "chenbin!";
}
OK, let's run these two programs. During the compilation process, we found that C # is indeed much faster than Java. (not to say m$) in fact, the output of two programs is the same, are:
I am sunwen!
chenbin!
One important difference is that a row is PUBLC string name= "chenbin!", while in Java it is a string name= "chenbin!". If we remove this public in Cs1.cs, we will have an error, because in C #, the default is protect, and therefore cannot be accessed outside the class, without any range modifiers.
This is one of the important differences. And there is: if we put cs1.cs this C # program in a public class CS1 changed to public class CS2, save, and then compile, you can see that the program runs normally. And in Java, this obviously doesn't work 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 portal. If a program does not have a method named Main, it will have "errors that cannot be found". Do not write main main yo, Xi hee, I often make such a mistake.
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