C # Basic syntax
Course Introduction
For readersC #Programming LanguageThis chapter requires readers to have mastered a computer high-level programming language, suchVBOrC ++Understanding programming knowledge such as variables, arrays, condition judgments, and loops. It has reached the level 2 of the National Computer Rank Examination.
This chapter is intendedC #For beginners, we will only introduceC #Is not introduced.C #.
C # OfCodeModule logic framework.
C #The Code module is logically divided into namespaces, types, and members.
The following is a completeC #Source codeContent,
UsingSystem; UsingSystem. Collections. Generic; UsingSystem. componentmodel; UsingSystem. Data; UsingSystem. drawing; UsingSystem. LINQ; UsingSystem. text; UsingSystem. Windows. forms; NamespaceWindowsformsapplication1 { Public Partial Class Form1:Form { PublicForm1 () { Initializecomponent (); } Private VoidButton#click (ObjectSender,EventargsE) { MessageBox. Show ("Display a message"); } } } |
This code is used as an example to describeC #The structure of the Code module. [Yuan Yongfu copyright]
Namespace
The namespace isC #The largest syntax structure module, which is identified by name. Each type has a namespace. The type of the same namespace cannot be repeated, but different namespaces can have the same name type.
InC #Use the following syntax structure to use the namespace.
namespace namespace name { several types } |
In this example, use the code"Namespace windowsformsapplication1"Defines aWindowsformsapplication1", Which defines the namespace named"Form1.
For many programming languages, suchCLanguage, which does not have the namespace concept. Thousands of functions are arranged together.VBHundreds of classes are also mixed together, which will make it very inconvenient for developers to remember and call.
Namespace can be used to classify multiple types, facilitate modularity, and facilitate calls.
Namespaces can also be divided into different levels, such as the following code.
Namespace Namespace name { Several Types } Namespace Namespace name.Sub-namespace { Several Types } |
This Code defines a namespace and a sub-namespace. There is no limit on the number of layers.
InC #In the function code, to reference a type, it must be its type full name, that is, "namespace.Type name ", the code looks bloated at this time, and if the type of namespace changes in the future, you need to modify a lot of code. In this caseC #UseUsingKeyword to reference the namespace.
For example, at the beginning of the Demo code, there is [Yuan Yongfu copyright ownership] a line of code"UsingSystem. Windows. forms;", Then the namespace is referenced in the code"System. Windows. Forms", While the form type"Form"Under this namespace, you can directly use it in the code"FormTo obtain the type of the form.UsingCode, you must useSystem. Windows. Forms. FormTo obtain the form type, which makes the Code look bloated. From here,UsingKeyword functions are similar.VBInWithKeyword.
Note:UsingIn addition to referencing namespaces, the keyword can also form a syntax structure for automatically destroying objects.
Type
The following types are defined in the namespace, including the class type, struct type, and delegate type. Fields and functions cannot be directly followed under the namespace. ThereforeC #No function exclusive to the type.
Theoretically, you can jump out of the namespace and directly write the type, which can also be compiled, but this is not recommended in practice.
In the Demo code, the following code defines a type.
Public partial class form1 : form { } |
Here,PublicKeyword indicates that this type is public and can be used by anyProgramCall.
PartialKeyword indicates that this code is not all of this type of code,C #There are other code files in the project that contain this type of code. During program compilation, editor [Yuan Yongfu copyright ownership] will collect these scattered source code to form a complete source code for compilation.
ClassThe keyword indicates that this is defining a class type.
Form1Is the name of the new type.
FormIs followedForm1There is a quotation mark in the middle, which indicates the New TypeForm1Is the existing type of integrationForm. If there is no code"Using system. Windows. Forms. Form"Reference namespace, this line of code must be written as"Public partial class form1: system. Windows. Forms. Form".
FormFollowed by a pair of curly braces, we define its type member definition area.
Member
The members of the definition type, including fields, attributes, methods, and events.
In the Demo code, use the following code to defineForm1.
Public form1 () { initializecomponent (); } |
The method name is the same as the type name, and no return value is defined. Therefore, this method is the constructor of this type.
The following code defines a member method.
Private VoidButton#click (ObjectSender,EventargsE) { MessageBox. Show ("Display a message"); } |
In this Code,PrivateKeyword indicates that the method is private and can only be used within the current type.
VoidKeyword indicates that this method does not return any value.
Button#clickIs the name of the method.
"Object sender"Defines the first parameter of the method,ObjectYes parameter type,SenderIs the parameter name.Eventargs E" The second parameter is defined. The two parameters are separated by commas.
The function declaration is followed by a letter [Yuan Yongfu copyright ownership. A line of code" MessageBox . Show (" Display a message " ); ", This line of code calls the type MessageBoxOf Show The parameter is the string" " Display a message " ". This line of code is the call type System. Windows. Forms. MessageBox Of Show Static Method to display a text message box.
Note thatC #Remember to write the Semicolon";".