C # tutorial Lesson 1: simple welcome Program

Source: Internet
Author: User
Tags first string

At the beginning of this article, although the commercial C # compiler has not yet been released, you can download microsoft's. NET Frameworks SDK Beta 1.
This lesson introduces several simple programs to help you get started with C. This section aims to achieve the following objectives:
1. Understand the basic structure of a C # program.

2. Have a preliminary understanding of the concept of "namespace.

3. Have a preliminary understanding of the concept of "class.

4. Understand the work done by the "Main" method.

5. Learn how to read 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 Declaration
Using System;
// Program start class
Class WelcomeCSS {
// Main begins program execution.
Public static void Main (){
// Write to console
Console. WriteLine ("Welcome to the C # Station Tutorial! ");
}
}

Description

1. The program in Listing 1-1 contains four basic elements: namespace declaration, class, "Main" method and statement.

2. The namespace declaration in this example indicates that the "System" namespace is being used.

The namespace contains a group of codes that can be called by C # programs. With the "using System;" declaration, the program can reference the code in the "System" namespace without adding "System" before each reference ". In this regard, I will introduce the namespace courses in detail later.

3. The class "class WelcomeCSS" contains the data used by the program and the definition of the method to be executed.

Similar to elements such as interfaces and structures, classes are used to describe objects in programs. These elements will be detailed in subsequent courses. The class in this example does not contain data and only contains one method. This method defines the behavior of the class (or what the class can do ).

4. When the program is running, this method in the WelcomeCSS class indicates what the class should do.

The method name "Main" is used as the reserved word and as the starting point of the program. "Main" is preceded by a modifier named "static. The "static" modifier indicates that the method only works in the specified class, rather than in the instance of the class. This is required because once the program is started, no instance of the object exists. The usage of classes, objects, and instances will be covered in the subsequent courses. Each method must have a return value type. In this example, the return value type is "void", which indicates that the "Main" function has no return value. Each method name is followed by a parameter table. The parameter table contains zero or multiple parameters and is enclosed in parentheses. For the sake of simplicity, no parameter is added after "Main. In subsequent courses, we will introduce the parameter types allowed by the "Main" method.

5. The "Main" method indicates its behavior through the "Console. WriteLine (...)" statement.

"Console" is a class in the "System" namespace. "WriteLine (...)" is a method in the "Console" class. We use the period operator "." To mark the subordinate elements in the program. Note that we can also write: "System. console. writeLine (...) ", this writing format is very interesting, it is based on" namespace. class. method. If the "using System" statement is not used at the beginning of the program, the writing format such as "System. Console. WriteLine (...)" must be strictly followed. The execution result of this statement is to output the string "Welcome to the C # Station Tutorial!" on the console! ".

6. Annotations are marked.

In this example, all the comments are single-line comments, indicating that the comments are comments from the beginning of the annotation symbol to the end of the row. If your comments span several lines, that is, multi-line comments, you can start with the symbol "/*" and end with the symbol "*/", all of which contain comments. You can also include a single line comment in a multi-line comment symbol. However, you cannot add multiple line comments to the end of a single line comments. During program compilation, the comments are ignored. The purpose of annotation is to add an annotation to the work done by the program in simple English.

7. All statements end with a semicolon.

Class and method start with "{" and end. Any statement between "{" and "}" is defined as a block. The block defines the activity scope (or lifetime and visibility) of the program element, which will be introduced in subsequent courses.

8. You can compile programs that can accept command line input information.

The set of command line input information is processed in the "Main" method. The program in Listing 1-2 can be input from the command line and displayed on the console.

2. Listing 1-2. NamedWelcome. cs

// Namespace Declaration
Using System;
// Program start class
Class NamedWelcome {
// Main begins program execution.
Public static void Main (string [] args ){
// Write to console
Console. WriteLine ("Hello, {0 }! ", Args [0]);
Console. WriteLine ("Welcome to the C # Station Tutorial! ");
}
}

Description

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

For example, enter "NamedWelcome Joe" in the command line ". If this is not done, the program will crash. In the subsequent courses, we will introduce 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 must be referenced later in the program. "String []" is the type of the parameter "args. The string type is used to store characters. These characters can be a word or multiple words. Square brackets "[]" indicate arrays. The "args" parameter consists of several words on the command line.

3. In the statement of the "Main" method, a "Console. WriteLine (...)" statement is added.

The parameter table in this statement is different from the previous writing method, where there is a format string "{0}" parameter. The first parameter in the format string starts from the number 0, the second parameter starts from the number 1, and so on. The "{0}" parameter indicates that the parameter value after the quotation marks is output to this position. Now let's take a look at the parameters following 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], the second element is args [1], and so on. For example, if I write "NamedWelcome Joe" in the command line and "args [0]", the value is "Joe ".

 

Let's go back to the "{0}" parameter embedded in the format string, because "args [0]" is the first parameter after the format string. Once this command is executed, the value "Joe" of "args [0]" replaces "{0}" in the format string }". Once the command: "NamedWelcome Joe" is executed, the output result will be:

> Hello, Joe!
> Welcome to the C # Station Tutorial!

You can also provide the input information to the program through the console. Listing 1-3 demonstrates how to interactively process user input information.

3. Listing 1-3. InteractiveWelcome. cs

// Namespace Declaration
Using System;
// Program start class
Class NamedWelcome {
// Main begins program execution.
Public static void Main (){
// Write to console/get input
Console. 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 does not use any parameters, but now the program has three statements, the first two statements are different from the third statement, they are: "Console. write (...) "instead of" Console. writeLine (...) ". The difference is: "Console. write (...) "statement to output information to the Console, and then the cursor stays in the same line, and" Console. writeLine (...) "output the information, and then wrap the line.

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

The second statement outputs information only after its parameters are properly processed. The first parameter after the format string is "Console. ReadLine ()". This causes the program to wait for the user to enter information in the console, and enter the information to end with a carriage return or line feed. 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 has been described earlier. Once the program "InteractiveWelcome" is run, the output result is:

> What is your Name?
> Hello, ! Welcome to the C # Station Tutorial!

Summary
So far, you have learned about the basic structure, namespace, and class of the C # program. You also learned that the "Main" method is the entry to the C # program, how to capture command line input information, and how to perform interactive I/O operations. Let's continue learning. The tutorial will be released. I will watch the movie 2009 first ....

 

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.