Start using C #

zhaozj2021-02-16  59

In order to use the C # and .NET framework libraries, you need to install .NET Framework and .NET Framework SDK. These files can be found on C # Builder's distribution CD, you can also download directly from Microsoft's .NET site. If you haven't installed .NET Framework and .Net Framework SDK on your computer, you will be installed when you install C # Builder.

The next part we will introduce the classic "Hello World" program, as well as courses linking different language knowledge involved in the procedures.

The first C # program: "Hello World"

Let us start the C # development trip through the classic "Hello World" program, the tables and quotes in the code segments below are not necessary, just for reading convenience. (Translation: There is no need to include the line number and table in the actual program, otherwise it will be wrong.)

1.

Using system;

2.

Public class helloworld

3.

{

4.

Public static void main ()

5.

{

6.

// this is a single line comment

7.

/ * This is a

8.

Multiple

9.

Line Comment * /

10.

Console.writeline ("Hello World! From Softsteel Solutions");

11.

}

12.

}

About C #, first need to pay attention to C # is case sensitive. So if you enter 'console'e instead of' console ', it will be wrong when you compile (the translation: error information is: error CS0246: No type or namespace name' console ', is it lacking a USING directive or assembly Quote?).

The second piece is worthy of concern is that every statement is in a semicolon; end, one code segment is included in a pair of braces. (Translation: These are the same as C and Java.)

Because C # is an object-oriented language, the C # program must be included in the class (for discussing the class in Chapter 11, but if you are an object-oriented newer, we recommend reading some object-oriented entry Reading). The classes in this program are declared in the second line, named "Hello World". (Translation: The C # program is included in the class, and the console application is in port () as an entry, these are very similar to Java, and the Java programmer steadow C # may be easier than C and C programmers.)

The first line of code declares that we have quoted a namespace called System (we will discuss namespaces in the eleventh lesson). The starting point of this statement is to save us to enter system time. Because the 'Console' object used in the 10th line of the code belongs to 'system' namespace. Its fully qualified name is 'system.console' (So, you can also do not declare the 'system' namespace, but enter system.console directly in the code). Because we have declared the code in Chapter 1, the code is referenced to 'system' namespace, so we no longer use the name of 'System'. (Translation: If you add this limit name, it is correct.)

When compiling and executes the above program, the program will automatically perform the 'main' method of the fourth line declaration. Remind everyone: C # is sensitive, so don't write 'main' to 'main'. (Translation: Otherwise, the program will not find the entrance, the error message is: the program is not defined.) When the program is compiled, the compiler will automatically ignore the 6-9 line, this four lines are programs given Comment. Chain 6 shows a single-line comment, all the code after //, is ignored by the compiler. The lines 7-9 show a multi-line annotation, all statements between / * and * / will be ignored, although they across multiple lines.

The second line of statement calls the 'WriteLine' method of the Console class in the System namespace. In a given example it is very obvious - prints a given string to the console. WriteLine's complex usage can be found in the 7th lesson.

Before running this program, we must first save it in a file. Unlike Java is that C # does not require the class name and file names must be strict, but if you use the same file name as the class name to save the program, it is more convenient. Regarding the extension, you can freely choose any extension, C # default extension is '.cs'.

If you save the file as 'HelloWorld.cs', you can compile this program using the following command line: CSC HelloWorld.cs.

This command will generate an executable (PE format) HelloWorld.exe, then you can enter HelloWorld or HelloWorld.exe running program. Obviously, the results of this program are: Hello World! From Softsteel Solutions.

Translation: csc.exe is the command line compiler provided by Visual Studio. It does not create any object file (.Obj) using it to compile C # programs, but create an output file directly. About CSC You can view the .NET Framework SDK to get more information. If you want to use CSC directly in any directory, you must set the PATH value in the environment variable, enable the Path value to include the directory where the CSC is located.

转载请注明原文地址:https://www.9cbs.com/read-23446.html

New Post(0)