C # tutorial first lesson: simple welcome program

zhaozj2021-02-16  98

When you start writing in this article, although the commercial C # compiler has not been launched, you can download Microsoft's .NET Frameworks SDK Beta 1. This section introduces several simple programs so that you are entitled to C #. This section is to achieve the following purposes: 1. Understand the basic structure of a C # program.

2. Preliminary understanding of the concept of "Name Space".

3. Preliminary understanding of the concept of "class".

4. Understand the work made by the "main" method.

5. Learn how to read the command line input information.

6. Learn to use the console input / output (I / O) statement.

1. List 1-1. A simple welcome program Welcome.cs

// Namespace Declarationusing System; // Program start classclass WelcomeCSS {// Main begins program execution.public static void Main () {// Write to consoleConsole.WriteLine ( "Welcome to the C # Station Tutorial!");}}

Description

1. The programs in Listing 1-1 include four basic elements: declarations, classes, "main" methods, and statements of namespaces.

2. Declaration of namespace in this example, indicating that "System" namespace is being used.

The namespace contain a set of code that can be called by the C # program. With the "Using System;" declaration, it indicates that the program can reference the code in the "System" namespace without having to add "system" in front of each reference. About this, I will introduce a detailed introduction in the course of the namespace.

3. Class "Class WelcomeCSS" contains the data to be used by the program, and the definition of the method to be performed.

Similar to elements such as interfaces and structures, classes are used to describe objects in the program, which will be described in detail in subsequent courses. The class in this example does not contain data, and only one method is included. This method defines the behavior of this class (or what you can do).

4. When the program is running, the method in the WelcomeCSS class indicates what you want to do.

Method "Main" as a reserved word as the starting point of the program. "Main" is a modifier named "static". The "Static" modifier indicates that this method works only in this particular class instead of working in the instance of this class. This is required, because once the program is started, there is no instance of an object. Specific uses of class, objects, and instances will be overwritten in later courses. Each method must have a return value type. In this example, the return value type is "void", which indicates that the "main" function does not return the value. Along each method name is also followed by a parameter table, the parameter table contains zero or more parameters and encloses with parentheses. For the sake of simplicity, the parameters are not added behind "main". In the following courses, the parameter type allowed by the "main" method will be described.

5. The "main" method indicates its behavior through the "console.writeline (...) statement.

"Console" is a class in the "System" name. "WriteLine (..." is a method in the "console" class. We use "." This period operator is used to mark the dependent elements in the program. Note that we can also write this: "System.console.writeline (...", this writing format is very interesting, it is written in the format of "Namespace.class.Method". If you don't use the "Using System" declaration in the beginning of the program, you must strictly abide by "System.Console.writeLine (...)". The execution result of this statement is to output a string "Welcome to the C # station tutorial!" On the console console. 6. The comment is marked by "//".

These comments in the example are separate comments, indicating that the beginning of the line is at the end of the end from the beginning of the comment symbol. If your comment, you can start with a multi-line annotation, you can start with symbol "/ *", with symbol "* /", which is included in the comment. You can also include a single line comment in multi-line comment symbols. However, the multi-line annotation symbol cannot be placed behind a single-line comment symbol. When the program is compiled, the comment portion will be ignored. The purpose of the comment is to add annotations to the work to be completed by simple English.

7. All statements are ended in semicolons.

The classes and methods start with "{" to end with "}". Any statement between "{" and "}" is defined as a block. Block defines the range of activity (or life and visibility), which will be introduced in later courses.

8. You can write a program that can accept the command line input information.

The collection of command line input information is processed in the "main" method. The procedure in Listing 1-2 can accept a name from the command line, which is then displayed on the console.

2. Listing 1-2. Read the program for the command line Enter information Namedwelcome.cs

// Namespace Declarationusing System; // Program start classclass NamedWelcome {// Main begins program execution.public static void Main (string [] args) {// Write to consoleConsole.WriteLine ( "Hello, {0}!", Args [ 0]); console.writeline ("Welcome to the c # station tutorial!");}}

Description

1. Remember, add your name to the command line.

For example, it is entered into "NamedwelCome Joe" in the command line. If you don't do it, the program will crash. In the following courses, how to detect this situation and how to avoid this situation.

2. In Listing 1-2, there is an entry in the parameter table of the "main" method.

The parameter name is "args". This parameter is referenced later in the rear section of the program. "String []" is the type of parameter "args". "String" type is used to store characters. These characters can be a word or multiple words. Square bracket "[]" means an array, the "args" parameter consists of several words on the command line.

3. In the "main" method, there is a "console.writeLine (...) statement. The parameters in the statement are different from the previous way, with a format string "{0}" parameter. The first parameter in the format string starts from the number 0, and the second parameter starts from the number 1, and so on. The "{0}" parameter means that the parameter value behind the quotation will be output to this location. Let us now look at the parameters behind the quotation marks.

4. "ARGS [0]" parameter, which points to the first string in the "Args" array.

The first element in the array is Args [0], and the second element is Args [1], which is pushed. For example, if I write "Namedwelcome Joe" on the command line, "ARGS [0]" is "Joe".

Let's go back to the "{0}" parameter embedded in the format string, because "args [0]" is the first parameter behind the format string, "ARGS [0]" value is executed when the command is executed "Joe" will replace "{0}" in the format string. Once the command is executed: "Namedwelcome Joe", the output result will be:

> Hello, Joe!> Welcome to the C # station tutorial!

The input information can also be provided to the program through the console. Listing 1-3 demonstrates how to interactively handle user input information.

3. Listing 1-3. Program for interactive processing input information InteractiveWelcome.cs

// Namespace Declarationusing System; // Program start classclass NamedWelcome {// Main begins program execution.public static void Main () {// Write to console / get inputConsole.Write ( "What is your name ?:"); Console. Write ("Hello, {0}!", Console.readline ()); console.writeline ("Welcome to the c # station tutorial!");}}

Description

This time, the "main" method is not used by any parameters, but now there are three statements in the program. The two statements are different from the third statement, they are: "console.write (...) instead of" console " .Writeline (...) ". The difference is: "console.write (..." statement outputs the information to the console, then the cursor stays in the same line, and "console.writeLine (..." puts the information output, then wrap.

The first statement only outputs "what is your name ?:" to the console.

The second statement will wait until its parameters are properly handled properly. The first parameter behind the format string is: "Console.Readline ()". This allows the program to wait for the user to enter information in the console, enter the information to enter the carriageway or the end of the wrap. The return value of this method replaces the "{0}" parameter in the format string and outputs it to the console.

The last statement is also used to output information to the console, which we have already introduced in front. Once the program "InteractiveWelcome" is run, its output is:> What is your name?> Hello,! Welcome to the c # station tutorial!

Small knots so far, you have learned the basic structure, namespaces and classes of the C # program. You also learned that the "main" method is the entrance of the C # program and learns how to capture the input information of the command line, and how to interactively I / O operation.

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

New Post(0)