Next, we created a console project named "csharpbasic". First, let's take a look at the code editing area in vs. By default, program is enabled. in the CS file, vs automatically adds the following code for US (because I created it with vs2008, The namespace introduced is different from vs2005 ):
Using system; <br/> using system. collections. generic; <br/> using system. LINQ; <br/> using system. text; <br/> // The namespace introduced above <br/> namespace csharpbasic // namespace of the Program <br/>{< br/> class Program/Class Name <br/>{< br/> static void main (string [] ARGs) // main function of the program entry point <br/>{</P> <p >}< br/>
Next we will explain the most basic program code.
1. Using statement: at the top of the file, there are four lines of code headers starting with using. They represent the introduction of the namespace after "using, so what is a namespace. First, let's take a look at the explanations in msdn:
Namespace is an organizational unit C #
Different types of methods in the program. Namespaces are similar in concept to folders in computer file systems. Like folders, a namespace can have a unique fully qualified name for a class. A c #
A program contains one or more namespaces, each of which can be defined by a programmer or as part of a previously written class library.
What does it mean? Let's explain it in plain words. For example, there are two people in a house named "Li Ming" and "Zhang Ming". If we call it "Ming", they don't know who we are. This is our full name, for example, "Li Ming". In this way, Li Ming can know that we are called him, the name "Ming" is like a C # class. The name "Li" and "Zhang" are like different namespaces. When there is only one person called "Ming, we can only call "Ming", but if there are several "Ming" at the same time, we will call their full names. Some may say that, if there are several people named "Li Ming", what should they do? In the same way, at this time, "Li Ming" is a class. We need to add a namespace before this class, such as "Li Ming in the second class," Li Ming in the second class, and Li Ming in the four-year class, wait ...... If you are still in the same class, add other namespaces, such as the number of columns in the row, etc ...... Until their full names are unique. It must be appropriate to call the full name, but it is complicated and troublesome to write the full name for every one of them. Therefore, the using statement has helped us a lot, you can write the namespace name you want to use to the end of using. In this CS file, you can directly enter the class name when you want to use the class under this namespace, you don't need to write the full name. Of course, if there is a duplicate class name in the introduced namespace, you need to write all their names when using these classes. Otherwise, the compiler cannot know which class you are calling .. Some namespaces have been pre-defined in the. NET class library. For example, the first four rows in this example are pre-defined namespaces of the. NET class library. That is to say, we can directly write system, system. collections. generic, system. LINQ, system. the class in the four namespaces text, instead of the namespace name. So, can using be written only at the top of the CS file? Let's move them to the bottom:
Namespace csharpbasic // namespace of the Program <br/>{< br/> class program // class name <br/>{< br/> static void main (string [] ARGs) // main function of the program entry point <br/>{</P> <p >}< br/> using system; <br/> using system. collections. generic; <br/> using system. LINQ; <br/> using system. text; <br/> // The namespace introduced above <br/>
Run the program and check that the compilation fails. The error message is displayed: The using clause must be located before all other elements defined in the namespace (except for external alias declarations)
It doesn't seem to work. Let's write it down in front of namespace ~~
2. namespace of the program: after knowing the role of the namespace, let's take a look at the namespace csharpbasic code. This sentence defines a namespace for the following class program, the name is csharpbasic. This is automatically created by vs based on our project name. Of course, we can change it to our favorite name, generally, it is named by the company name or project type. Here I name it ojlovecd.
3. Class Name: What is a class? Let's take a look at the definition of classes in msdn: class is C #
The most powerful data type. Like a structure, classes also define data and behavior of data types. Then, programmers can create objects for such instances. Different from the structure, classes support inheritance, and inheritance is the basic part of object-oriented programming.
After the fixed population is removed, it becomes "the class is the data type". What is the data type? Let's take a look at msdn's definition of data types:
Data Type Overview
The type can be described as follows:
Built-in numeric type (such as Int or char), or
User-Defined types (such as class or interface ).
Anonymous type, which consists of a group of public attributes encapsulated in the non-Name Reference type.
Type can also be defined:
Value Type (C #
Reference) (used to store values ). These types include the primitive numeric type, enumeration, and structure, and the versions that can be null.
Reference Type (C #
Reference) (used to store references to actual data ). These types include classes, interfaces, arrays, and delegation.
C # is a strongly typed language. Therefore, each variable and object must have a declaration type. What do you mean? That is to say, in C #, any variables or objects you use must be of a certain data type, and no variables or objects without data types exist. This is like the birth of a new life in the world. It must belong to a specific species. Types include. net class library built-in numeric type, user-defined type and anonymous type (anonymous type is C #3.0 new feature), our class here belongs to user-defined type, for more information about classes, we will proceed later. Like a namespace, the class name can also be freely changed. It can be the same as the namespace name. Here we change the program to ojlovecd.
4. main method of the program entry point: the word "function" has been renamed as "Method" in C #, and the main method is the entry point of the C # program. When running the program, the system will call the main method to start our application. Can the main method be renamed? Let's try and change the main method to ojlovecd:
Using system; <br/> using system. collections. generic; <br/> using system. LINQ; <br/> using text = system. text; <br/> // The namespace introduced above <br/> namespace ojlovecd // namespace of the Program <br/>{< br/> class ojlovecd // Class Name <br/>{< br/> static void ojlovecd (string [] ARGs) // main function of the program entry point <br/>{</P> <p >}< br/>
Code running and program error. Error: "ojlovecd": Member names cannot be of the same type as their closed
The error message tells us that the member name cannot be the same as the class name. Let's change the name to csdn:
Using system; <br/> using system. collections. generic; <br/> using system. LINQ; <br/> using text = system. text; <br/> // The namespace introduced above <br/> namespace ojlovecd // namespace of the Program <br/>{< br/> class ojlovecd // Class Name <br/>{< br/> static void csdn (string [] ARGs) // main function of the program entry point <br/>{</P> <p >}< br/>
Run again, or an error occurs. The error is: Program "C: /Documents and Settings/administrative/My Documents/Visual Studio 2008/projects/csharpbasic/obj/debug/csharpbasic.exe "does not contain static" main "methods suitable for entry points
In other words, the program must not only include the main method, but also the main method must be static. So what is static? It is the Member modified by the keyword static. What is the function? Let's talk about it later.
Change csdn back to main. In the main method body (that is, the part contained in the ARC {}), enter the following code:
Using system; <br/> using system. collections. generic; <br/> using system. LINQ; <br/> using text = system. text; <br/> // The namespace introduced above <br/> namespace ojlovecd // namespace of the Program <br/>{< br/> class ojlovecd // Class Name <br/>{< br/> static void main (string [] ARGs) // main method of the program entry point <br/>{< br/> console. writeline ("Hello ojlovecd"); <br/>}< br/>
Press Ctrl + F5 to generate our first application, for example:
So far, our first application has been created and run successfully. In the next lecture, we will explain more useful information about this application.