".NET Programming Pioneer C #" Chapter 3 First C # Application

zhaozj2021-02-17  49

".NET Programming Pioneer C" Chapter 3 First C # Application Christoph Wille | 2001-9-1

Chapter III First C # Application 3.0 Select an editor Even though I am a stubborn notepad mad, but this time I don't recommend editing the source code. The reason is that you are dealing with the real programming language, using the NotePad editing source code to compile the source code (C programmer know what I am talking about.) You have several options. You can reconfigure your trusted Visual C 6.0 to 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 rows, color coding, tool integration, and good search capabilities. CodeWright is one of the examples, as shown in Figure 3.1. Figure 3.1 CodeWright is one of you can use to create a number of possible editors in C # code files. Of course, in the editor mentioned, there is no need to create a C # program. Use notepad to be edited. However, if you consider going to write a bigger project, it is best to endure your love. 3.1 "Hello World" code discussion editor is a bit left question, let us turn the topic back to a very famous applet. This shortest C # version application see Listing 3.1. Store it, the file is called HelloWorld.cs so that you can complete the remaining steps such as compiling applications in accordance with the instructions. Listing 3.1 The easiest "Hello World" program 1: class helloworld2: {3: public static void main () 4: {5: system.console.writeline ("Hello World"); 6:} 7:} in C # The code block (statement group) is hosted by the large inclusion ({and}). So, 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 classes are enclosed in the defined bunary arc. The C # application (executable) entry point is the Static Main method, which must be included in a class. There is only one class to use this flag definition unless you tell which main method it should use (no side, it will generate a compilation error). Compared with C , the first letter of Main is uppercase M instead of lowercase letters you have used. In this method, your program starts and ends. Other methods can be called in the method - as in this example, used to output text - or create objects and activate the method. As you can see, the main method returns a void type. Public Static Void Main () Although the C programmers will feel like it is seen when seeing these statements, but other programmers are not the case. First, public access flag tells us that this method can be accessed by any program, which is the necessary condition it is called. Second, Static means that there is no instance of creating classes first, you can call the method - what you have to do is calling the method using the class name. HelloWorld.main (); however, I don't agree that this line code is executed in the main method, and recursive will cause the stack overflow. Another important aspect is the return type. For method main, you can select Void (meaning that there is no return value at all), or use int as an integer result (an error level returned by the application). Therefore, two possible main methods are: public static void main () public static int main () C programmer will also know the back I will mention - the array of command line parameters that can be passed.

Such as: public static void main (string [] args I don't want to explain how to access the parameters in detail, but I want to give a C programmer a warning: and C , the application path is not part of this array. Just those parameters are included in this array. After the MAIN method is not brief introduction, let us focus on the only real code row - this line code displays "Hello Wold" on the screen. System.console.Writeline ("Hello World"); if you don't have SYSTEM, everyone will guess WriteLine is a static method of console objects. So what is SYSTEM? It is a namespace containing the console object (range), in fact, not every time the console object is prefixed before the name space, you can like the appraisal shown in Listing 3.2, in the application Introduce namespace. Listing 3.2 Introducing namespaces in the application 1: use system; 2: 3: Class HelloWorld4: {5: public static void main () 6: {7: console.writeline ("Hello World"); 8:} 9: } All you have to do is add a USING instruction for the System name space. After this, you can no longer need to specify the namespace, you can use their methods and properties. There are many namespaces in the NGWS framework system, and I only discuss a few objects in the huge namespace pool. But in Chapter VII "Write Components" with C # Write "will introduce your object to create your own namespace. 3.2 Compiling applications Because NGWS Runtime supports all compilers (VB, C and C #), you don't 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 the application (only to compile the name, no familiarity), it is your primary choice. Open the command prompt and switch to the directory of the HelloWorld.cs. Try the following command: CSC HelloWorld.cs HelloWorld.cs is compiled and linked into hellworld.exe. Because the source code has no error (that's of course!), The C # compiler does not have an error prompt, and there is no stopping over the entire compilation process. As shown in Figure 3.2. Figure 3.2 Using the command line compiler CSC.exe Compile App Now you are ready to run the first application written in C # written. Simply knock in the command line, the result is "Hello World". Before you continue to introduce, I want to think a little about the first application and a compiler switch: csc /out:helo.exe helloWorld.cs This switch tells the compiler output file to name Hello.exe. Although this is not a trick, it is the basic skill of the future compiler used in this book. 3.3 Input and output to so far, I only demonstrate the output of simple constant strings to the screen. Although this book only introduces the concept of C # programming without introducing user interface programming, I need to quickly learn simple screen input and output method - corresponding to CN and Printf, or C CIN and COUT. I can't provide VB corresponding functions because screen access is not part of the core language. You only need to read the user's input and prompt some information to the user. Listing 3.3 shows how to read the name input of a user request and display a custom "Hello" message.

Listing 3.3 Enter information from console 1: use system; 2: 3: Class Inputoutput4: {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:} Chapter 7 A new method using Console objects is used to prompt text information to users, it is Write method. It is different from WriteLine that does not wrap when it outputs. I use this method so that the user can enter the name in the same line of the information prompt. After the user enters his name (and press Enter), the readline method reads a string variable. The name string is connected to the constant string "Hello" and is displayed with the WriteLine method we have already familiar with (see Figure 3.2). Figure 3.3 Compiling and run custom Hello applications you have completed the necessary inputs and output features 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: use system; 2: 3: Class Inputput4: {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 9th line contains the console.writeline statement using the format string. The format string example is as follows: "Hello {0}" {0} instead of the first variable after the format string is followed 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, is not limited to only using a string variable. You can use any type, which is discussed in the following Chapter 4 "C # Type". 3.4 When adding comments When writing code, you should write an annotation of the code, explain the implementation, and change history. Although the information provided in your comment (if any) is written to you, but you still have to follow the method of writing C # annotations. Listing 3.5 shows two different ways. Listing 3.5 Add a note 1: use system; 2: 3: Class HelloWorld4: {5: public static void main () 6: {7: // This is a single line notes 8: / * This note 9: Cross Multi-line * / 10: console.writeline (/ * "Hello World" * /); 11:} 12:} "//" symbol is used for a single line comment. You can use the "//" to comment, or follow the back of a code statement: int nmyvar = 10; // 胡 胡 八 All in "//" is considered a note; so, you can Use them to comment on the part of a row or a source code. This annotation method is similar to that described in C . If your comment spans more lines, you must use the "/ * * /" character combination. This approach is effective in C. In addition to the single-row comment, this approach is equally effective in C and C #. This multi-line annotation is used because of C / C and C #, so they also use the same terminator.

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

New Post(0)