Routine structure of C # class code in the C # tutorial

Source: Internet
Author: User

The main form of console applications is a CMD window. After a common console application is created, there is only one window and one Program. cs file, which is the main scenario for code writing.

  

Xiaotian: What are the functions of the automatically generated codes in the newly created console applications, Winform and WebForm applications? Can we modify it at will?

  

Laotian: this problem involves many things that we have learned and cannot be fully explained yet. However, we will first use the console application to briefly introduce the C # class code framework, so that you will not be confused.

  

The main form of console applications is a CMD window. After a common console application is created, there is only one window and one Program. cs file, which is the main scenario for code writing. If not specified, all the code is written in this file. For details, refer to the following picture:

 

  

Since each item cannot be described in detail in the figure, we provide numbers for them. Note that the green font on the way is my comments. For the namespace, we will introduce it in detail in later sections. We will not describe it too much here. The following sections describe:

  

1. Use the using keyword to introduce other types of namespaces to the current class. Remember the previous chapter, we said that any application written in C # depends on. NET Framework, because. NET Framework provides the data type, character processing, operators, and other classes, methods, and attributes that we use in programming, such as the Console class. Therefore, in any case, the System namespace must be referenced. You can also delete the following items. However, if you need to use classes, methods, and attributes in a namespace, you must first reference this namespace. For example, if I want to borrow a car from your house, you must first contact your house.

  

2. namespace of the current class. In other words, all the methods and attributes of the current class and the class belong to this namespace. If the classes under other namespaces need to use the content of this class, they must be referenced using the using Keyword first.

  

3. class (discussed in later sections), usually in Program. the default class Program exists in the cs file, and all the operations we perform are finally displayed in the Main function of the class. If we still need to add another simple class at this time, we can add one at the level of the class (all at the same level under the namespace, I added a new MyClass class in 2-1.

  

4. Methods and functions in the class. Note that the method in C # is not equal to the function. This is discussed in Chapter 4 in detail. In the Program class, there must be an entry function Main. What is an entry function? Of course, it is not a food. Instead, it means that our application starts to enter the Program from here every time. Therefore, most of the Instance code in this chapter is written in this method. Next we have added the GetName () method. This means that we can build multiple custom methods in the class. But do not try to delete the Main function.

  

Xiaotian: What is level? What do you mean by the level of the class in the namespace and the level of the method in the class?

  

Lao Tian: note that the namespace, class, and method annotation boxes in Figure 2-1 all have vertical downward lines in this range. For example, in the above example, the MyClass class and Program are of the same level. The Main function and the GetName method are of the same level.

  

Basically, all classes follow this result (very few special cases will be discussed later ). In addition to exercises, it is not recommended to write multiple classes in one file. This is not conducive to code reading.

  

Next, let's take a look at the Winform application practices. The first thing to note is that the Program of the Winfrom application. the cs class is better not to be moved easily. In our current learning stage, the only thing we need to modify is the startup window, that is, to modify the last line of code in the Main function:

  

Application. Run (newForm1 (); // change the startup form

  

For more detailed operations, more detailed steps will be taken in the instances later in this chapter. I will not go into details here.

  

This article is a new one. For more information, see the source and author!

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.