Java programmers learn C#

  Because of work needs, I want to learn C#. In fact, I think it is good. I like to know more languages, because it is very helpful for my future development. grammar:

  I have learned C, and I am a java programmer, so it is not difficult for me to learn C#. Here I have integrated the basic syntax of C# and the difference between it and JAVA, and also for others who want to learn C# Our partners provide a convenient way to first understand the language of C#;

  Briefly explain C#: Microsoft's development language, similar to java, has many similarities in syntax, object-oriented programming language, and has some relationship with .net. The .Net framework consists of a huge code base for Client languages ​​such as C#. We can use this development tool to develop C#:

  • Visual Studio 2010 (VS)

  Let's make a simple console application first, which is the kind of small black box: //When we create a project, the system will automatically generate the main method (the main entry of the program), and introduce these garbled files , the file used here is using, and the one used in java is import; using Sys using System.Collections.Generic;

using System.Linq; using System.Text; using System.Threading.Tasks; 
//This is the namespace, when you create a project, the system will automatically create a file
namespace FirstApp with the project name .sln for you {
  //This is the class name. Unlike java, the java class name must be the same as the file name. C# does not need
class Program {
     //The main function, which can also be called the main method, is defined in the same way as java, which can be said to be exactly the same.
static void Main( string [] args) {
       //This is the output statement of C#, where Console.WriteLine(); is somewhat similar to System .out .println() in java ; Console.WriteLine(
" Hello World! " );
       //This is a C# definition variable The method is exactly the same as that of java. The difference is that var can be used to define variables in C#. ;
int x = 89 ; Console.WriteLine(x); int z = 10 ; double y = 20 ;
       //output variables as placeholders Console.WriteLine(
"x = {0}; y = {1}", z, y); string yourName;
       
Console.WriteLine(
"What is your name?");        /*This is a statement in C# to get user input, use Console.ReadLine();
       similar to Scanner in java scanner = new Scanner(System.in ); int i = scanner.nextInt();
       The difference is that C# can directly Call ReadLine() in Console to get input, while in java, you need to instantiate Scanner first, and then call the nextInt() method in it, and you need to use different methods to receive it according to the type of input content,
       and C# does not need that trouble, only You need ReadLine to accept all types. If you want to accept only specific types of values, you can use the following method to convert the input content */
       yourName
= Console.ReadLine(); Console.WriteLine("Hello {0}", yourName);        //Use the Convert.ToInt32(); method here to convert the input content to int, if the input content cannot be converted to int, an error will be reported; int age = Convert.ToInt32(Console.ReadLine()); Console.WriteLine( " You are {0} years old " , age);
       //Here is the method to define the constant light. The constant light cannot be assigned a second time. Once assigned, it cannot be changed. This is in front of the variable. Just add the const keyword. In java, you need to add the final keyword
const double PI = 3.14 ; Console.WriteLine(PI);
       //Prevent the small black box from going back in seconds after running, which is equivalent to doing an interception to get the Enter key entered by the user Console.ReadKey(); } } }

  These can be regarded as the basic syntax of C#, and I will not talk about other object-oriented features, such as encapsulation, inheritance, and polymorphism. There are others, such as interfaces, exception handling, file streams, events, collections, generics, and threads. The logic is the same, and there is nothing to say. If not, you can look at my blog homepage related java. Processing methods, appropriate changes to the syntax, you can.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325027027&siteId=291194637