Chapter 3 first C # Application
3.0 select an editor
Although I am a stubborn Notepad, I do not recommend using it to edit the source code this time. The reason is that you are dealing with real programming languages. Using Notepad to edit source code compilation may produce a large number of error lines (C ++ programmers know what I'm talking about .)
You have several options. You can reconfigure the trusted legacy Visual C ++ 6.0 so that it can work with the C # source file. The second option is to use the new Visual Studio 7. Third, you can use any third-party program editor. It is best to support the number of lines, color encoding, tool integration, and good search functions. CodeWright is one of the examples, as shown in 3.1.
Figure 3.1 CodeWright is one of the many possible editors you can use to create a C # code file.
Of course, none of the mentioned Editors is necessary to create a C # program. You can edit it with Notepad. However, if you want to write a bigger project, you 'd better cut your foot.
3.1 "Hello World" code
The discussion editor is a little digress. Let's turn the topic back to a very famous small application. For the shortest C # application, see list 3.1. Save it as a file named helloworld. cs so that you can follow the instructions to complete other additional steps, such as compiling an application.
Listing 3.1 The simplest "Hello World" program
1: class HelloWorld
2 :{
3: public static void Main ()
4 :{
5: System. Console. WriteLine ("Hello World ");
6 :}
7 :}
In C #, a code block (statement group) is enclosed by a large arc ({And. Therefore, even if you have no experience in C ++ before, you can also say that the Main () method is part of the HelloWorld class statement, because the class is included in the defined Large arc.
C # The entry point of an application (executable) is the static Main method, which must be included in a class. Only one class can use this flag, unless you tell the Compiler which Main method should be used (no side will produce a compilation error ).
Compared with C ++, the first letter of Main is uppercase M, rather than the lowercase letters you have used. In this method, your program starts and ends. You can call other methods in the method-for example, output text-or create an object and activate the method.
As you can see, the Main method returns a void type.
Public static void Main ()
Even though C ++ programmers may feel familiar with these statements, they are not the same for other programmers. First, the public access flag tells us that this method can be accessed by any program, which is a necessary condition for its call. Second, static means that you can call a method without creating an instance of the class first-all you need to do is call the method with the class name.
HelloWorld. Main ();
However, I do not agree to execute this line of code in the Main method. recursion will cause stack overflow.
Another important aspect is the return type. For method Main, you can select void (meaning there is no return value at all), or use int as the integer result (error level returned by the application ). Therefore, the two possible Main methods are:
Public static void Main ()
Public static int Main ()
C ++ programmers will also know what I will mention later-an array of command line parameters that can be passed to the application. For example:
Public static void Main (string [] args)
I don't want to explain in detail how to access the parameters, but I want to give the C ++ programmer a warning: Compared with C ++, the application path is not part of this array. Only those parameters are included in this array.
After a brief introduction to the Main method, let's focus on the only real line of code-this line of code displays "Hello Wold" on the screen ".
System. Console. WriteLine ("Hello World ");
If it wasn't because of System, you would immediately guess that WriteLine is a static method of the Console object. So what does System mean? It is the namespace (range) that contains the Console object. In fact, it is not always prefixed with the namespace before the Console object. You can, as demonstrated in listing 3.2, introduce the namespace in the application.
Listing 3.2 introduce namespace in an application
1: using System;
2:
3: class HelloWorld
4 :{
5: public static void Main ()
6 :{
7: Console. WriteLine ("Hello World ");
8 :}
9 :}
All you need to do is add a using command to the System namespace. After that, you can use their methods and attributes without specifying namespaces. The NGWS framework has a lot of namespaces. I only discuss a few objects in the huge namespace pool. However, in chapter 8 "use C # To write components", we will introduce how to create your own namespace for your object.
3.2 compile the application
Because NGWS Runtime supports all compilers (VB, C ++, and C #), you do not have to buy a separate development tool to compile the application into IL (intermediate language ). However, if you have never used the command line compiler to compile an application (only know the compilation name, but do not note it), it is still your first choice.
Open the command prompt and switch to the directory where helloworld. cs is stored. Run the following command:
Csc helloworld. cs
Helloworld. cs is compiled and linked to hellworld.exe. Because the source code is correct (that's of course !), C # the compiler has no error prompts and does not pause throughout the compilation process. 3.2.
Figure 3.2 use the command line compiler csc.exe to compile an application
Now you are ready to run the first application written in C. Simply input helloworld in the command line and the output is "Hello World ".
Before proceeding, I would like to imagine the use of the first application and a compiler switch:
Csc/out: hello.exe helloworld. cs
The output file of the switch compiler is hello.exe. Although this is not a trick, it is the basic skill of future compilers used in this book.
3.3 Input and Output
So far, I have only demonstrated outputting simple constant strings to the screen. Although this book only introduces the concept of C # programming, but I need you to quickly learn simple screen input and output methods-corresponding to C's scanf and printf, or C ++'s cin and cout. I cannot provide VB functions because screen access is not part of the core language.
You only need to be able to read user input and prompt some information to the user. Listing 3.3 describes how to read the name input of a user request and display a customized "Hello" message.
Listing 3.3 read input information from the console
1: using System;
2:
3: class InputOutput
4 :{
5: public static void Main ()
6 :{
7: Console. Write ("Please enter your name :");
8: string strName = Console. ReadLine ();
9: Console. WriteLine ("Hello" + strName );
10 :}
11 :}
Row 7th uses a new method of the Console object to prompt the text information to the user, which is the Write method. It is different from WriteLine in that it does not provide line breaks during output. I use this method so that you can enter a name in the same row as the information prompt.
After the user enters his name (and press the Enter key), The ReadLine method reads a string variable. The name string is connected to the constant string "Hello" and displayed in the WriteLine method we are already familiar with (see Figure 3.2 ).
Figure 3.3 compile and run a customized Hello Application
You have almost completed the necessary input and output functions of the NGWS framework. However, you also need to display multiple values for the user. Write a format string for the user. Listing 3.4 shows an example.
Listing 3.4 uses different output methods
1: using System;
2:
3: class InputOutput
4 :{
5: public static void Main ()
6 :{
7: Console. Write ("Please enter your name :");
8: string strName = Console. ReadLine ();
9: Console. WriteLine ("Hello {0}", strName );
10 :}
11 :}
The second line contains the Console. WriteLine statement using the format string. The format string example is as follows:
"Hello {0 }"
{0} replaces the first variable following the format string in the parameter table of the WriteLine method. You can use this technology to format more than three variables.
Console. WriteLine ("Hello {0} {1}, from {2 }",
StrFirstname, strLastname, strCity );
Of course, it is not limited to only using string variables. You can use any type. These types are discussed in Chapter 4 "C # type.
3.4 add comments
When writing code, you should write comments for the code to explain the implementation content and change history. Although the information provided in your comments (if any) is provided to you, you must follow the C # comments writing method. In listing 3.5, two different methods are used.
Listing 3.5 adds comments to your code
1: using System;
2:
3: class HelloWorld
4 :{
5: public static void Main ()
6 :{
7: // This is a single line comment
8:/* This annotation
9: crossing multiple lines */
10: Console. WriteLine (/* "Hello World "*/);
11 :}
12 :}
The "//" symbol is used for single-line comments. You can use "//" to comment out the current row or follow a code statement:
Int nMyVar = 10; // nonsense
All comments after "//" are considered as comments. Therefore, you can use them to annotate the entire line or line of source code. This annotation method is similar to that described in C ++.
If your comment spans multiple lines, you must use a character combination. This method is valid in C. In addition to single-line comments, this method is also effective in C ++ and C. Because both C/C ++ and C # use this multi-line annotation method, they also use the same Terminator. See the following code lines:
/* Console. WriteLine ("Hello World ");*/
I used "/**/" to comment out a whole line. Now I assume that this line is part of a long code and I decided to temporarily disable a program block:
/*
...
/* Console. WriteLine ("Hello World ");*/
...
*/
The problem with this structure is that "*/" after the line "Hello World" terminates the comment "/*" that begins with the first line, the remaining code is valid for the compiler and you will see some interesting error messages. At least the last "*/" is marked as a attribution error. I just want to remind you of this error.
Conclusion 3.5
In this chapter, you create, compile, and execute the first C # application: the famous "Hello World" program. I will use this short application to introduce you to the Main method, which is the entry point and exit point of an application. This method can return no return value or an integer error level. If your application is called with parameters, you can read and use them (but not necessary.
After compiling and testing the application, you have learned more about the input and output methods provided by the Console object. For C #, they are sufficient to create meaningful console examples, but most of the user interfaces will be WFC, WinForms, or ASP +.