V. C # Editor
You can write C # programs in the text editor or in the integrated development environment Visual Studio. There are also some third-party editors on the market, some of which are free of charge. For more information, see here.
VI. C # program structure
A c # program contains a class, which contains at least one common static method Main, which initializes the program and terminates the program. Create Sub-objects, execute methods, and implement logical processing of software in the Main method. The following is a typical micro-C # routine:
Using System;
Class MyFirstApp
{
Static int Main (String [] args)
{
System. Console. WriteLine ("Hello. NET ");
Return 1;
}
}
In C #, use the following declaration to introduce external definitions, instead of using # include in C ++:
Using System;
Using System. Data;
Then compile the code by using C6. To save the preceding code as the file hello. cs, run the following command:
Csc hello. cs
Hello.exe is generated, which writes "Hello. NET" to the output window of the console ".
However, hello.exe is not a real and clear CPU code segment. In fact, it contains. NET byte code. When hello.exe is started, CLR extracts important metadata written by the compiler into the code. Next, a module called the Just-In-Time compiler maps the code to a specific CPU to start the actual execution process.
VII. C # And namespace
In reality, the C # program usually contains multiple files, each of which can contain one or more namespaces. A namespace is a name that depicts some software entities such as classes, interfaces, enumerations, and embedded namespaces to the compiler. The namespace must have a unique name like the data type. In a C # program, you can identify an element by its full qualification name, which indicates a hierarchy. For example, System. String is a complete qualification name of the. NET String type. To simplify the code, you only need to declare that you are using the System namespace:
Using System;
A relative name, such as String, can be used as the synonym for the complete name, and finally represents System. String.
By using the namesapce keyword, we can also wrap the C # program or class in our own namespace, for example:
Namespace mywn
{
Using System; // for String
Class MyFirstApp
{
Static int Main (String [] args)
{
System. Console. WriteLine ("Hello. NET ");
Return 1;
}
}
}
The namespace mywn is part of the global namespace. You do not need to use a prefix to call it because its full qualification name is simple mywn. Defining a namespace is a way to ensure the uniqueness of public names. In fact, if the names of two classes conflict, as long as they belong to different namespaces, the two classes are still unique.
8. Compiling and compiling a classic routine in C #
1. Write code
"Hello World" is almost the first routine involved in learning any programming language. Let's use C # To complete this job. In any of the C # editors mentioned above (such as wordboards), type the following code:
Using System; class MyClass {static void Main () {Console. WriteLine ("Hello World! ");}}
Save the file as myclass. cs.
2. Compile the program
Note: the C # compiler requires at least one independent variable, such as the file name. Assume that your C # file name is myclass.cs. now use the command line program csc.exe to compile the above myclass. cs file:
Csc myclass. cs
Therefore, the C # compiler generates a myclass.exe file in the bin directory of the project file. Run this exe to see what the output is.
3. Code meaning
Next we will look at the meaning of the code line by line:
The first line of the program is using System. Why the using System? Because System is the namespace of the storage System class, the Console (Console) class used to display the output in the program is defined in the System namespace.
The next line is class MyClass. The class keyword in C # is used to create a new class. Each class has a static void Main () function, which is the entry of a C # program.
The WriteLine method of the Console class outputs text information to the Console.