". NET programming Pioneer C #" chapter III first C # application (Turn)

Source: Internet
Author: User
Tags command line comments constant integer readline valid visual studio
Programming | Procedure Chapter III FIRST C # application
3.0 Select an editor
Although I am a stubborn notepad, but this time I do not recommend using it to edit the source code. The reason is that you are dealing with a real programming language, and using Notepad to edit the source code can generate a lot of error message lines (c + + programmers know what I'm talking about.) )
You have several options. You can reconfigure your trusted legacy Visual C + + 6.0 to enable it to work with C # source files. The second option is to use the new visual Studio 7. Third, you can use any Third-party program editor, preferably to support line number, color coding, tool integration and good search capabilities. CodeWright is one example, as shown in Figure 3.1.

Figure 3.1 CodeWright is one of the many possible editors you can use to create C # code files.

Of course, none of the editors mentioned is necessary to create a C # program. You can definitely edit it with Notepad. But if you think about writing bigger projects, it's best to reluctantly.

3.1 "Hello World" code
The discussion editor is a bit off track, so let's turn the topic back to a very famous small application. This shortest C # version of the application is shown in Listing 3.1. Save it, the file name is HelloWorld.cs, so that you can follow the instructions to complete such other steps as compiling the 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 #, code blocks (statement groups) are enclosed by large brackets ({and}). So, even if you have no experience in C + +, you can say that the main () method is part of the HelloWorld class statement because the class is enclosed in the defined braces.
The entry point for a C # application (executable) is the static Main method, which must be contained in a class. Only one class can use the flag definition unless you tell the compiler which Main method it should use (no side, resulting in a compilation error).
In contrast to C + +, Main's first letter is uppercase M, not the lowercase letter you used. In this method, your program starts and ends. Method can call other methods--for example, to output text--or to create an object and activate the method.
As you can see, the main method returns a void type.
public static void Main ()
Although the C + + programmer will certainly feel familiar with these statements, other programmers are not. First, the public access flag tells us that this method can be accessed by any program, which is necessary for it to be invoked. Second, static means that you can call a method without creating an instance of the class first-all you have to do is call the method with the class name.
Helloworld.main ();
However, I am not in favor of executing this line of code in the Main method, which can cause a stack overflow.
Another important aspect is the return type. For method Main, you can choose void (meaning there is no return value at all), or an int is an integer result (the 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'm going to mention later--an array of command-line arguments that can be passed to the application. Such as:
public static void Main (string[] args)

I don't want to elaborate on how to access the parameters at this time, but I'd like to give a warning to C + + programmers beforehand: The application path is not part of this array compared to C + +. 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-the line of code that displays "Hello wold" on the screen.
System.Console.WriteLine ("Hello World");
If it weren't for system, people would immediately guess that WriteLine is a static method of the console object. So what does system represent? It is the namespace (scope) that contains the console object, not actually the prefix of the namespace in front of the console object each time, and you can introduce namespaces in your application as shown in Listing 3.2.

Listing 3.2 Introducing namespaces in your application
1:using System;
2:
3:class HelloWorld
4: {
5:public static void Main ()
6: {
7:console.writeline ("Hello World");
8:}
9:}

All you have to do is add a using directive to the System namespace. After that, you no longer need to specify namespaces, you can use their methods and properties. There are a lot of namespaces in the NGWS framework system, and I only discuss a few objects in the huge pool of namespaces. But in the eighth chapter "Writing components in C #" will introduce you to create your own namespaces for your objects.

3.2 Compiling the application
Since 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've never compiled an application from a command-line compiler (just compile the name and not memorize it), it's still your first choice.
Open a command prompt and switch to the directory where the HelloWorld.cs is stored. Typing the following command:
CSC HelloWorld.cs

HelloWorld.cs is compiled and linked into Hellworld.exe. Because the source code is not wrong (of course!) , the C # compiler does not have an error, and there is no pause in 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 application that is actually written in C #. Simply typing HelloWorld on the command line, the output is "Hello world".
Before proceeding, I would like to imagine the first application and the use of a compiler switch:
Csc/out:hello.exe HelloWorld.cs
This switch tells the compiler that the output file is named Hello.exe. Although this is not a trick, it is the basic skill of the future compilers used in this book.
3.3 Input and output
So far, I've only demonstrated 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 let you quickly learn simple screen input and output methods--corresponding to scanf and printf in C, or Cin and cout in C + +. I can't provide the corresponding function for VB, 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 shows how to read a user-requested name input 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:}

Line 7th uses a new method of the console object to prompt the text message to the user, which is the Write method. It differs from the WriteLine in that it does not change lines when it is exported. I use this method so that the user can enter a name on the same line as the message prompt.
After the user enters his name (and presses 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 that we already know (see Figure 3.2).

Figure 3.3 Compiling and running a custom hello application

You've almost finished learning 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 a different output method

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:}

Line 9th contains a Console.WriteLine statement that uses a format string. The format string example is as follows:
"Hello {0}"
{0} replaces the first variable followed by a format string in the parameter table of the WriteLine method. You can use this technique to format more than three variables.
Console.WriteLine ("Hello {0} {1}, from {2}",
strFirstName, strLastName, strcity);

Of course, it is not limited to using only string variables. You can use any type, and these types are discussed in the fourth "C # Type" later in this chapter.

3.4 Adding comments
When writing code, you should write annotation clauses for code, explain the content of implementation, change history, etc. Although the information provided in your comments (if any) is written to you, you must follow the method of writing a C # annotation. Listing 3.5 shows the two different ways in which they are used.

Listing 3.5 Adds a comment 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: Across multiple lines * *
10:console.writeline (* * "Hello World" * *);
11:}
12:}

The '//' symbol is used for single-line comments. You can use the "//" comment on the current line, or after a code statement:
int nmyvar = 10; Nonsense
All is considered a comment after "//", so you can also use them to annotate a whole line or a line of source code. This annotation is similar to what is described in C + +.
If your annotation spans more than one line, you must use a "/*/" character combination. This method works in C. This approach is equally valid in C + + and C # In addition to Single-line annotations. Because both C + + and C # Use this multiline annotation method, they also use the same terminator. Take a look at the following line of code:
/* Console.WriteLine ("Hello World"); */

I use "/*/*" To simply annotate an entire line. Now I assume that this line is part of a long code, and I've decided to temporarily disable a block:
/*
...
/* Console.WriteLine ("Hello World"); */
...
*/

The problem with this structure is that the "* *" Behind the "Hello World" line terminates the "/*" comment that started 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 attribution error. I just want to remind you that you know this mistake.

3.5 Summary
In this chapter, you create, compile, and execute the first C # application: The famous "Hello World" program. I use this short application to introduce you to the main method, which is an entry point for an application and an exit point. This method can have no return value or return an integer error level. If your application is invoked with parameters, you can (but not necessarily) read and use them.
After compiling and testing the application, you learned more about the input and output provided by the console object. They are enough to create meaningful console examples for Learning C #, but most of the user interface will be WFC, WinForms, or asp+.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.