From today on, we are going to learn about Microsoft. on the NET Platform, a Language C # That is widely promoted and developing rapidly. The pronunciation of "#" is English sharp, which means sharp. Microsoft's idea is: I hope to develop this language into the sharpest language related to Microsoft. net and framework introduction, I will put it in the next course to teach, because according to many teaching experiences, it is found that when you are new to this language, if you talk too much about it theoretically, it is difficult to accept and understand, so the course schedule of this lesson starts with a simple C # program.
Before starting this course, I need to learn how to use VS to create a console application. The following code is automatically generated by the compiler after it is created, here we start to open the C # door:
Simple structure and code comments
1 using System;
2 // use the using keyword to introduce the namespace. For example, using System; introduces the System namespace
3 using System. Collections. Generic;
4 using System. Linq;
5 using System. Text;
6
7/** // * multi-line comments. For the sake of beauty, most of them will write the following results */
8
9 /**//***********************
10 * Code content in this space :*
11 * understand simple program structures and *
12 * Three annotation Methods *
13 ***********************/
14 // use the namespace keyword to define a namespace named "console exercise"
15 namespace console exercises
16 {
17 // use the class keyword to define a class named "Program"
18 class Program
19 {
20 // Main method of the program entry point
21 static void Main (string [] args)
22 {
23}
24}
25}
Please take a look at the text after "//". These texts are the explanation statements for the code annotation function. The annotated statements will not be executed in the code, in addition to commenting on the meaning or functions of the Code to improve the readability of the program code, we can also comment out the code that is not used for the time being but does not want to be deleted first, this code will not be executed because it has been commented out. If you want to use it in the future, you only need to release the comments to avoid the useless effort of rewriting this code.
NoteThere are a total of three methods. For which method to use, it mainly depends on which effect you want to use annotations for practice. The three annotations are:
1. Single line Comment Method "//": it is the comment method adopted by line 2nd of the code above. It is generally used only for interpreting a line of code.
2. Multi-line comments: Start comments with "/*" and end comments with "*/". This method is generally used to explain the entire code segment, for example, 7th lines.
3. In the XML annotation method, enter three "/" before the code to be annotated, And the compiler will automatically display the following code with "<>" </> ":
1/** // <summary>
2 // method annotation of XML:
3 // generally, the peer method and class annotation are used.
4 /// </summary>
5 static void MethodName ()
6 {
7
8}
The code above defines a method named "MethodName,The method is written in "{}" in C # code that will implement a certain function.,If your program needs to implement this function, find the method in which the code of this function is written, find the name of this method, and at the entry point of the Program (Main method) to find the function code written in this method, and then execute it. Now I have briefly described the functions and usage of the method. If you don't understand it, don't worry. As the number of writing code increases and you learn it, soon we will understand what I am talking about in the vernacular theory.
You don't need to master the 3rd usage items, as long as you are familiar with single-line comments, and we need to develop the habit of using comments to increase the readability of the Code, so that you can benefit yourself!
Next let's learn a simpleC # BASIC program structure:
In the first code, I marked a simple C # program result:
Using introduces the namespace; // The first to fifth rows
Define a namespace // 15th rows
{
Define a class // 18th rows
{
Define the Main method () // 21st rows
{
Function Code Statement (determines the number of lines of Function Code statements based on the functions you implement );
/* Each code statement represents a specific functional meaning,
* Statements can also be combined to complete one or more functions,
* When each code sentence uses ";", it indicates the end, which is equivalent to telling the compiler to encounter,
* It is regarded as the end of defining a function statement */
}
}
}
In the c # language, we use "{" to start the scope, and use "}" to end the scope, for example, line 15th of the first code defines a namespace named "console exercise", and the code in line 16 "{" to 25, we think it is the code used in the namespace of Program. We can also say that the "Program" class defined in row 18th is a member of the namespace of "console exercises, the method defined in line 21 as "Main" is a type member of the "Program" class. That is to say, the method should be defined in the class and the class should be defined in the namespace, I won't teach you in the first class.NamespaceAndClassNow we can learn who is included in the package.
Next, I will give you a question about defining the C # syntax structure. You can refer to the answers and comments I have given to compare your code.
1. In the namespace of "console exercise", define another class named "LeiName.
2. In the "LeiName" class, define a method named "Jianfa" (subtraction.
3. In the "Program" class, define a method named "Jiafa" (addition.
4. interpret the definition of "jiafa" using XML annotations.
Before writing code, note that C # is a case-sensitive language. At the same time, the compiler used for writing code provides the smart prompt function, and the keywords in the language are displayed in blue lower case.
C # structure answer
1 using System;
2 using System. Collections. Generic;
3 using System. Linq;
4 using System. Text;
5 namespace console exercises
6 {
7 // use the class keyword to define a class named "Program"
8 class Program
9 {
10 // Main method of the program entry point
11 static void Main (string [] args)
12 {
13 // if you want to run the addition function, you should be called in the Main method of the program's entry point. Be sure to use ";" to indicate the end of the call, the call method is the method name and the "()" after the method. Remember to add () when calling a method (), at the same time, we need to instill in everyone the idea that if we want to complete one (or a series of) actions, we should define a method, when calling an action, you must add "()" after the method name "()".
14 Jiafa ();
15}
16 /// <summary>
17 // defines a method named Jiafa: 3rd and 4
18 /// </summary>
19 static void Jiafa ()
20 {
21 // code for implementing Addition
22}
23}
24 // In The namespace of "console exercise", define a Leiname class: 1st question
25 class Leiname
26 {
27 // defines a method named Jianfa: Question 2nd
28 static void Jianfa ()
29 {
30 // code for Subtraction
31}
32}
33}
Based on the above Code, you should note that:
Question 1: The Leiname class should have a level-1 Relationship with the Program class, which should be behind the end Of the 23-line Program class, or write it in the namespace "console exercise" {after the start, and before the start of the Program class defined in the row 7th, it indicates the level-level relationship between the class and the class, at the same time, the class must be defined in} at the end of the namespace of 33 rows.
Question 2: define a method named "jianfa". According to the definition method of the second code, the name of MethodName is replaced by jianfa, which should be within {} of the Leiname class.
Question 3: when defining a method in the Program class, it must maintain the same level as the Main method, because they are all Class Members of the Program class, but the Program is executed from the Main method, because the Main method calls jiafa (); after the program is executed, the 21st lines of code in the jiafa method will be executed, but the jianfa will not be executed, the reason is very simple, because the Main method does not call him, the 30th lines of code in jianfa will not be executed. For details about the Main method, I will introduce it in the next chapter.
Question 4: I added a comment for this method in row 17th. Please try the following: when you place the cursor in the Main method, call the 14th line code of the Jiafa method, the compiler automatically displays the prompt "defines a method named Jiafa: 3rd and 4.
Can you understand the unique advantages of the third annotation? The answer is that when you call this method that has been annotated using XML, the Comment statement that you have defined for this method appears, such as the role of this method, the points of attention for usage, and other prompt statements. Imagine that when we become a member of a project team who has compiled large programs, there will be a lot of functions to be implemented in the project, so we need to define a lot of methods to implement these functions, for example, when logging on, you must determine that the password must be a number, so that a small method is defined much more. When the program development cycle is prolonged, special use of many methods will be forgotten, the call may cause unnecessary trouble and affect the overall progress. Maybe one of your team's colleagues writes this project when users register new users, the password must also be a number, so you can directly call the method you wrote without having to re-write it yourself, which improves the work efficiency, if the method you wrote uses the XML annotation method, it is also a friendly reminder for him. This is what I said previously to benefit myself!
This lesson is about C #, program running methods, and other substantive issues. We will teach them in the future, however, in the next section, you must be familiar with writing simple definition code about the C # structure, because I found that, the students wrote a lot of code, but they should be confused about where the method is defined. So I have adjusted the Teaching Sequence very early, the course that defines a simple structure is used as the start chapter to learn, to instill the idea of a general situation into the students and to understand the process model.
In the next lesson, we will learn the Main method and use the console application to learn the C # syntax.