Lesson 1: simple welcome Program
Author: Joe Mayo
Compile: PINE
In this lesson, we need to study C # applets to achieve the following goals:
◎ Understand the basic structure of C # Program
◎ Familiar with the concept of NameSpace.
◎ Understand the meaning of a CLASS
◎ Functions of learning the "Main" Method
Understanding how to obtain command line Input
◎ Understanding of console input/output (I/O)
Section 1: 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 eSchool! ");
}
}
This applet has four Main elements: a namespace declaration, a class, a "Main" method, and some program annotations.
"Using System;" indicates that the program declares the introduction of the namespace "System. The namespace contains some code that can be called by the C # program. Through "using System;", you can tell the program to use its methods and attributes without adding "System" in the preceding statements. We will discuss namespaces in future courses.
"Class WelcomeCSS" declares the WelcomeCSS class, which contains program data and methods to be executed. It is one of the only types used to describe objects and will be discussed in future courses. In the WelcomeCSS class, there is only one method that does not contain data. This method defines the role of this class.
The name of this method is "Main", and Main is the reserved keyword of the program, representing the program entry point. Note that only one class can be defined using the Main flag. There is a modifier "static" before Main, indicating that this method only works in this class, rather than in the instance. This is necessary because no object instance exists at the beginning of the program execution. Classes, objects, and instances will be discussed in detail in future courses. Each method must have a return type. In this example, "void" indicates that the Main method does not return a value. In addition, each method also has a parameter list, which is represented by a number in braces, for example, "{0 }". However, we did not add parameters to the Main method. In future courses, we will discuss the types of parameters that the Main method should have.
The "Main" method specifies that the first line of code "Console. WriteLine (...)" is executed (...)". "Console" is a class of the namespace "System. "WriteLine (...)" is a method of the "Console" class, so they are separated by the operator. We can also write this sentence as "System. Console. WriteLine (...)". Its pattern is actually like this: "namespace. Class. method ". If we do not declare a namespace by using the "using System" statement at the beginning of the program, we must write it in such a complete format. This shows the convenience of the namespace. This line of code outputs "Welcome to the eSchool!" on the screen! ".
We use the "//" symbol to mark comments. "//" indicates that all rows are comments. If you want to use multi-line comments, use the "/*" and "*/" tags, which are considered comments between the two tags. Of course, you can only add a line of comment between the "/*" and "*/" tags. During compilation, annotations are not compiled into the program. Its function is to tell others or remind them of what the program has done.
Note: Each statement in the program is followed by a semicolon ";". Both classes and Methods start with "{" and end. "{" And "}" jointly define the program block. The function scope of the block-defined program element will be discussed in detail later.