<< Exhibition C # >> Foreword (Supplement) and Chapter 3 First C # Program (Revised)

zhaozj2021-02-08  404

Preface 0.1 Please welcome to read "Presenting C #". This book is a shortcut you improve your business programming language. Such enterprise programming languages ​​have next-generation programming language service runtime (NGWS runtime): C # (pronounced "C Sharp"). NGWS Runtime is an dynamic environment that not only manages execution code, but also provides a dynamic environment that makes programming easier. The compiler produces managed code to point to this managed execution environment. You get cross-platform language integration, cross-platform language exception handling, enhance security, version control, arrangement support, and check-in service. The main language supporting NGWS Runtime is C #. Many programs supporting the NGWS framework are written in C #, so in some compilers with NGWS Runtime, its compiler can be considered very testing and optimized. C # language draws on C , but has a modern and new type of security - making C # becomes the preferred language of the enterprise solution. 0.2 Who should read this book If you are very strange to programming, this book is not suitable for you. This book is an attempt to let programmers will relieve and use C #, based on their already have knowledge. The goal of "displaying C #" is aimed at programmers already have experience in C / C , VB, Java or other programming. If you have a c background, it will be easy to turn to C #, but if you are well traveled in other different languages, this book will take over some days. If you have a point COM programming, this will be more interesting, but COM programming is no longer necessary. 0.3 How to organize the first chapter of the first chapter of the C # Introduction - this chapter takes you to C #, and answers the problem about you should consider learning C #. Chapter 2 Technical Basis of NGWS Runtime - Introducing NGWS Runtime to the low-level architecture provided by the C # code running. Chapter III First C # Application - Create a real first C # app (otherwise what is it?), It is a "Hello World" application. Chapter 4 C # Type - You can see various types used in the C # application. Explore the difference between value and reference type, how to fill boxes and boxes. Chapter 5 Class - C # function is really powerful, it is an object-oriented programming. You will learn a lot of things, such as constructor, destructuring functions, methods, attributes, indexes, and events. Chapter 6 Control Statement - Process Control is implemented. Explore the various options and statements provided by C #. Chapter VII Operation - Obtaining Application Skills Writing Explosive Abnormal Processing, having this skill, in the NGWS Runtime World, you are a good citizen Chapter 8 with C # write components - build components with C #, because support NGWS Runtime, it can be used by customers of different language platforms. Chapter 9 Configuration and Arrangement - Learn how to compile work with C #, and how to automatically create documents for your C # source code. In addition, this chapter will introduce the version technical issues of NGWS. Chapter 10 is interacting with non-managed code - finding how you use C # internal non-accomplished codes, and how non-managed code interacts with your C # components. Chapter 11 C # Code Fire - Get the skills of using the SDK error control tool, you can accurately find errors in your application and modify them. Chapter 12 Security - Explore the security concept of NGWS Runtime. Learn the code access security and role-based security. 0.4 Use this book What do you need? From this book's view, what you need is the next generation of Windows Services Software Development Caps (NGWS SDK). Although at least as long as there is an NGWS runtime and a C # compiler, it is an excellent manner when the instruction file and all SDK tools (including debugger) are installed on one machine. idea. This book does not need any tools for Visual Studio 7 on the machine.

I only suggest you have an excellent program editor to edit the C # source file. ... (omitted some of the relevant publishing processes and expressing paragraphs) 0.5 About Author Christoph Wille, Microsoft System Engineer (MCSE), Microsoft System Developer (MCSD), NetWare Network Administrator (CNA) and MCP-IT, as a Network consultants and programmers, specially proficient in Windows DNA. Microsoft believes that he is the most valuable person (MVP) asp, he is one of the few developers of the early C # versions of Microsoft. Christoph alone or participated in the writing of books, including SAMS Press, "self-study ADO2.5 21 days", "self-study ASP 24 hours", "self-study MCSE TCP / IP 14 days" and "MCSE Training Guide: SQL Server 7 management". Third, the first C # program

3.0 Select an editor

Although I am a stubborn notepad mad, I don't recommend editing the source code this time. 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 the vintage Visual C 6.0 you trusted 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 the possible editing of C # code files in the C # code file.

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

The above discussion is a bit left question, let us turn the topic back to a very famous applet. This shortest C # version program is shown in Listing 3.1. Store it (Note: The program cannot contain a line number), the file name is HelloWorld.cs so that you can complete other remaining steps such as compilers in accordance with the instructions.

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 #, the code block (statement group) is hosted by the tremble "{" and "}". So, even if you didn't use C experience, you can also point out that the main () method is part of the HelloWorld class statement because classes are enclosed in the defined oche.

The C # application (executable) entry point is the Static Main method, which must be included in a class. Only one class can 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. Your program begins in this method, but also ends in this method. 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 has a VOID return type.

Public static void main ()

Although when seeing these statements, C programmers will definitely feel at home, 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 (the error code returned by the program). Therefore, there are two possible MAIN methods:

Public static void main ()

Public static int main ()

C programmers will also understand that the following I have to mention-can be passed to the program command line parameter array. Such as:

Public static void main (string [] args)

I don't want to explain how to access parameters in detail, but I want to give C programmers a warning: and C , the program 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 it is not because of SYSTEM, everyone will immediately guess WriteLine is a static method for the Console object. So what is SYSTEM? It is the namespace containing the console object (range), in fact, not every time the Console object is prefixed before the name space, you can introduce it like the list 3.2, Name space.

Listing 3.2 introducing namespaces in the program

1: USING System;

2:

3: Class HelloWorld

4: {

5: public static void main ()

6: {

7: console.writeline ("Hello World");

8: }

9: }

What you have to do is add a USING instruction to 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 how to create your own namespace for your object.

3.2 Compiler

Since 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 never use the command line compiler compile process (only to compile the name, no familiarity), it is your primary choice.

Open the command prompt and switch to the directory where helloworld.cs is stored. Try the following command:

CSC HelloWorld.cs

HelloWorld.cs is compiled and linked into hellworld.exe. Because the source code is not wrong (then of course)! The C # compiler does not have an error prompt, and there is no stall over the entire compilation process. As shown in Figure 3.2.

Figure 3.2 Compiling the application using the command line compiler CSC.exe

Now you are ready to run the first programs that are really written in C #. Simply knock in the command line, the result is "Hello World".

Before you continue to introduce, I want to simply explain a usage of the compiler switch:

CSC /out:hexe 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 compiler used in this book.

3.3 Input and output

So far, I only demonstrate the display of simple constant strings to the screen. Although this book only introduces the concept of C # programming without introducing user interface programming, I want you to quickly grasp the simple console input and output method - corresponding to CNF and Printf, or C CIN and COUT . I can't provide VB corresponding functions because console 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 Entering 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:}

Chain 7 uses a new method of console object to prompt the text information to the user, it is the Write method. It is different from WriteLine that does not wrap when it outputs. 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 in the WriteLine method we have already familiar with (see Figure 3.3)

Figure 3.3 Compiling and run custom Hello applications

You have completed the necessary inputs 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 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 no more than three variables.

Console.writeline ("Hello {0} {1}, from {2}",

StrfirstName, StrlastName, StriTy;

Of course, it is not limited to only the string variable. You can use any type, which is discussed in the following Chapter 4 "C # Type".

3.3 Add a comment

When writing code, you should write an annotation, explain the content, change history, etc. Although the information provided in your comment (if any) is written to you, but you must follow the method of writing C # annotations. Listing 3.5 shows the two different ways used.

Listing 3.5 Add a comment to the code

1: USING System;

2:

3: Class HelloWorld

4: {

5: public static void main () 6: {

7: // This is a single line of comments

8: / * This comment

9: Cross multiple lines * /

10: console.writeline (/ * "Hello World" * /);

11:}

12:}

"//" symbol is used for a single line comment. You can use the "//" to comment the current, or follow the back of a code statement:

INT nmyvar = 10; // 胡 八

All characters after "//" are considered a comment; so you can use them to comment on a full line of code or partial code in a row. This annotation method is similar to that described in C .

If your comment spans more lines, you must use the "/ * * /" to combine symbols. This approach is effective in C. In addition to a single line of comments, this approach is also useful in C and C #. This multi-line annotation method is used due to C / C and C #, so they also use the same end. Please see the following code line:

/ * Console.WriteLine ("Hello World"); * /

I use "/ * * /" to simply comment a whole line. Now I assume that this line is part of a long code, and I decided to temporarily disable a block:

/ *

...

/ * Console.WriteLine ("Hello World"); * /

...

* /

The problem exists in this structure is: "* /" after the "Hello World", "* /" terminates the comment begins with "/ *" of the first line, and the remaining code is valid for the compiler, you will see some interesting Error information. At least the last "* /" is marked as a home error. I just want to remind you by the way, let you pay attention to this error.

3.4 small knot

In this chapter, you created, compiled and implemented the first C # program: the famous "Hello World" program. I use this short program to introduce you to the main method, it is an entry point of a program and an exit point. This method can have no return value or return an integer error code. If your program is called with parameter, you can (but do not need) read and use them.

After compiling and testing, you have learned more methods for input and output by console objects. For learning C #, they are sufficient to create meaningful console programs, and most of the user interface will be used for WFC, WinForms, or ASP .

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

New Post(0)