Program | Tutorial This lesson gives you an introduction to C # By introducing a few simple programs. This section is intended to achieve the following objectives:
1. Understand the basic structure of a C # program.
2. Preliminary understanding of the concept of "namespaces".
3. 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 console input/output (I/O) statements.
1. Listing 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 includes four basic elements: namespace declarations, classes, "Main" methods, and statements.
2. In this case, a namespace declaration indicates that the namespace is being used.
The namespace contains a set of code that can be invoked by a C # program. With the "Using System;" This declaration indicates that the program can reference code within the "System" namespace without having to precede each reference with "system". In this regard, I will explain in detail in a later course that specifically covers namespaces.
3. Class "Class Welcomecss" contains the data to be used by the program, and the definition of the method to be executed.
Like elements such as interfaces and structs, classes are used to describe objects in a program, and these elements are described in detail in subsequent courses. The class in this example does not contain data and contains only one method. This method defines the behavior of the class (or what it can do).
4. When the program is running, the method in the Welcomecss class indicates what the class is going to accomplish.
The method name "Main" is used as a reserved word as the starting point for the program. "Main" is preceded by a modifier named "Static." The "static" modifier indicates that the method works only in that particular class, not in instances of that class. This is required because once the program is started, there is no instance of the object. class, the specific usage of objects and instances will be covered in a later course. Each method must have a return value type. In this case, 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 that contains 0 or more parameters and is enclosed in parentheses. For simplicity, no parameters are added after "Main". In the following lesson, you will describe the parameter types that the "Main" method allows.
5. The "Main" method indicates its behavior through the "Console.WriteLine (...)" statement.
"Console" is a class in the "System" namespace. "WriteLine (...)" Is the method in the "Console" class. We use the "." This period operator marks the subordinate element in the program. Note that we can also write like this: "System.Console.WriteLine (...)", such writing format is very interesting, it is based on the "Namespace.class.method" format to write. If the "using System" declaration is not used at the beginning of the program, then "System.Console.WriteLine (...)" must be strictly adhered to. Such a writing format. The execution result of the statement is the output string "Welcome to the C # station tutorial!" on the console console.
6. Comments are marked by "//".
These comments in the example are single-line comments, indicating that from the beginning of the annotation symbol to the end of the line, it is a comment section. If you have a comment that spans several lines, that is, a multiline comment, you can start with the symbol "/*" and end with the symbol "* *", which contains all the comments. You can also include single-line comments in multiple-line comment symbols. However, you cannot place multiple line annotation symbols behind a single-line annotation symbol. When the program compiles, the annotation section is ignored. The purpose of the annotation is to annotate the work to be done by the program in Simple English.
7. All statements are separated by semicolons ";" End.
Classes and methods begin with "{" and End With "}". Any statement between "{" and "}" is defined as a block. The blocks define the range of activities (or lifetimes and visibility) of the program elements, which are described in a later course.
8. You can write programs that can accept command line input information.
The collection of command line input information is handled in the "Main" method. The program in Listing 1-2 allows you to accept the input of a name from the command line and then display it on the console.
2. Listing 1-2. The program that reads the command line input information 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" on the command line. If you don't, the program crashes, and in the course that follows, it shows you how to detect this and how to avoid it.
2. In Listing 1-2, there is an entry in the parameter table of the "Main" method.
The name of the parameter is "args". This parameter is referenced in the later part of the program. "string[]" is the type of the parameter "args". The string type is used for storing characters. These characters can be either a word or multiple words. The square brackets "[]" represent the array, and the "args" argument consists of several words on the command line.
3. In the statement "Main" method, One More "Console.WriteLine (...)" Statement.
The parameter table in this statement is different from the previous one, which has a format string "{0}" parameter. The first argument in the format string starts with the number 0, the second argument starts with the number 1, and so on. The ' {0} ' parameter means that the value of the parameter following the quotation mark will be output to that location. Now let's look at the arguments behind the quotes.
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" on the command line, the value of "args[0]" is "Joe."
Let's go back to the "{0}" argument embedded in the format string, because "args[0]" is the first argument after the format string, and once the command is executed, the value "Joe" for "args[0" will replace "{0}" in the format string. Once the command is executed: "Namedwelcome Joe", the output will be:
>hello, joe!
>welcome to the C # station tutorial!
The input information can also be supplied to the program through the console. Listing 1-3 shows a way to interactively process information entered by a user.
3. Listing 1-3. An interactive process for processing input information 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 arguments, but now there are three statements in the program, and the preceding two statements are different from the third statement: "Console.Write (...)" Rather than "Console.WriteLine (...)". The difference is: "Console.Write (...)" The statement prints the information to the console, then the cursor stays on the same line, and the "Console.WriteLine (...)" Output the information and then wrap it.
The first statement only outputs "What is your name?:" to the console.
The second statement will not output information until its parameters are properly processed. The first argument after the format string is: "Console.ReadLine ()". This allows the program to wait for the user to enter information in the console, enter information to return or wrap end. The return value of the 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 described earlier. Once the program "Interactivewelcome" has been run, its output results are:
>what is your Name?
>hello,! Welcome to the C # station tutorial!
Summary
So far, you've learned the basic structure of C # programs, namespaces, and classes. You also learned that the "Main" method is the entrance to the C # program, and learned how to capture the input information of the command line, and how to perform interactive I/O operations.
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.