Learn C # (1)

xiaoxiao2021-03-06  73

Read http://www.microsoft.com/china/msdn/library/langtool/vcsharp/cornyfield.mspx

C # sharp experience

Release Date: 2/4/2002

| Update Date: 6/22/2004

Nanjing University of Posts and Telecommunications, Li Jianzhong (JZLi@china.com)

Little note:

The first lecture "Hello, World!" Program

//Helloworld.cs by Cornfield, 2001

// CSC HelloWorld.cs

Using system;

Class HelloWorld

{

Public static void main ()

{

Console.writeline ("Hello World!");

}

}

C # supports two annotations: single-line comments started with "//" and use "/ *", "* /"

Comment. The comments cannot be nested.

I think there should be a third type /// used to generate a document, similar to Java

About USING statement:

Of course, the USING indicator is not necessary, we can use the global name of the type to get the type. actual

Using the USING statement does not have any effect on the C # compilation output, it is just simple.

Types of a longer namespace.

C # uses a namespace to organize the program. Named space can be nested. USING indicator can be used to simplify references for named space types. There are two using indicators. "Using System;" statement allows us to replace the type "system.console" with a short type name "console". "Using output = system.console;" statement allows us to replace type "system.console" with alias "Output". The introduction of namespace greatly simplifies the Organizational mode of the C # program. We declare and achieve a HelloWorld class with a static main () function. C # All declarations and implementations are placed in the same file, unlike the two can be separated as C . The main () function is very special in C #, which is the entry point for all executable programs specified by the compiler. Due to its specialty, we have the following guidelines for main () functions:

The 1.main () function must be packaged in the class or structure to provide the entry point of the executable program. C # uses a completely object-oriented programming method, and there is no global function like C in C #. The 2.main () function must be a static function (Static). This allows C # to run the program without having to create an instance object. 3. MAIN () function protection level There is no special requirement, public, protected, private, etc., but usually we specify it as public. (Note: Of course, the first word line of public PUBLIC cannot be capitalized) 4. The first letter of the MAIN () function name should be capitalized (Note: Do not be Java), otherwise it will not have the semantics of the entry point. C # is case sensitive language. 5. MAIN () function The parameters are only two parameters: there is no parameter and the command line parameter represented by the String array, that is, static void main () or static void main (String [] args), the latter accepts command line parameters. There is only one main () function entry point in a C # program. Other forms of parameters do not have entry descriptions, and C # is not recommended to overload the main () function in other parameters, which can cause compilation warnings. 6.main () function return value can only be VOID (no type) or int (integer type). Other formal return values ​​do not have entry point semantics. HelloWorld.exe for compilation and output is a standard executable file header with a target platform added by the intermediate language (IL), metadata and an additional compiler (such as a Win32 platform plus a standard Win32 executable Head) The PE (Portable Executable, portable actuator) file, not a traditional binary executable - although they have the same extension. The intermediate language is a set of instruction sets independent of the CPU, which can be translated into local code for the target platform by instant compiler Jitter. The intermediate language code makes all the advanced languages ​​of all Microsoft.Net platforms C #, VB.NET, VC.NET, and the like to be independent, and interoperability between languages. Metadata is a collection of tables embedded in the PE file. Metadata describes some of the information of the data type in the code (Common Language Runtime) that needs to be learned when the code is executed. Metadata allows .NET application code to provide self-description features, providing type security, which requires additional type libraries or interface definition languages ​​(Idl). Type the command "ildasm /output:HelloWorld.il HelloWorld.exe", you can usually get two output files: helloworld.il and helloworld.res. Among them, the latter is extracted resource file, we don't care, let's see the helloWorld.il file. Similar to earlier assembly languages, it has the function of object definition and operation. We can see it definition and implements a HelloWorld class that inherits from System.Object: main () and .ctor ().

Second lecture C # language basic introduction

type of data

The data type of the C # language is mainly divided into two categories: value type and reference type. Another data type "pointer" is set for the Unsafe context programming, where the UNSAFE context refers to the Unsafe to meet the code to meet the use of C # unmanaged code that is directly required for memory, which will lose Microsoft .NET. Platform garbage collection and other CLR properties

NULL value indicates that the reference type does not reference any actual address.

The value type can be divided into structure types and enumeration types. The structural types include simple types and user-defined structural types. Simple types can be divided into Boolean and numeric types. C # Language is strictly divided by the numerical type, only two values ​​of true and false, there is no conversion between the C / C and other types of other types. The numerical type includes three types of integral, floating point and DECIMAL. Total value has Sbyte, Byte, Short, Ushort, int, uint, long, ulong, char total nine species. In addition to the CHAR type, the other eight two or two groups are both symbols and no symbols, respectively. The floating point value has two kinds of float and double. Decimal is mainly used in financial, currency, etc. The following table is a detailed description of these simple types: Simple Type Description Sample Sbyte 8-bit has symbolic integer sbyte val = 12; Short 16-bit has symbol integer short val = 12; int 32-bit has symbol integers Int Val = 12; long 64-bit has symbol integers long Val1 = 12; long val2 = 34L; Byte 8-bit unsigned integer Byte Val1 = 12; Byte Val2 = 34u; Ushort 16-bit unsigned integer ushort var1 = 12; ushort VAL2 = 34U; UINT 32-bit unsigned integer uint val1 = 12; uint val2 = 34U; Ulong 64-bit unsigned integer ulong var1 = 12; ulong val2 = 34u; ulong val3 = 56L; ulong val4 = 78ul; float 32 -BIT single-precision floating point number float val = 1.23F; Double 64-bit double precision floating point number double value = 1.23; double VAL2 = 4.56d; Bool Boolean BOOL VAL1 = true; Bool Val2 = false; char Character type, Unicode Code CHAR VAL = 'h'; Decimal 28 valid digital 128-bit decimal type decimal val = 1.23m;

The reference type is divided into four types: class, interface, array, delegation. In addition to we can define our own type, it includes two relatively special types of Object and String. Object is the root class in all types in C # (including all value types and reference types). The String type is a sealing type (cannot be inherited), and the instance represents the Unicode string, which we will be detailed in the "ninth lecture structure, enumeration, array and string". The interface type defines a method of contracts, we will tell the "Seventh Telegraph Interface Inheritance and Polymorphism". The delegation type is a signature to static or instance methods, similar to function pointers in C / C , will be described in the "Eighth Lecture Delegate and Event". In fact, we will see these types from the next topic are some form of packaging of classes.

Each data type has a corresponding default value. The value type of the value is 0 or 0.0, where char default is '/ x0000'. The default value of the Boolean type is false. The default value of the enumeration type is 0. The default value of the structural type is to set the domain of all the value types to the corresponding value type, set the domain of the reference type to NULL. The default value of all reference types is NULL.

Different types of data can be converted, C # type conversion has implicit conversion, clear conversion, standard conversion, and custom conversion. Including the conversion and C , data from "Small Type" to "Great Type" is implicit conversion, from "large type" to "small type" converted to clear conversion, clear conversion needs "(TYPE) DATA" General parentheses conversion operator. Standard conversion and custom conversion are for system built-in conversions and user-defined conversions, both of which are custom types such as class or structures. Variables and constants

Variables indicate storage locations, and variables must have a determined data type. One of the types of security of C # is to ensure that the storage position of the variable is accommodated. Variables in C # can be divided into static variables, instance variables, pass value parameters, reference parameters, output parameters, array parameters, and local variables. The local variable is a temporary variable in the method.

Static variables and example variables are mainly for data members (also known as domain) for classes or structures. Static variables are loaded after the class or structural type thereof is loaded, and if the initialization assignment is not initialized, the initial value of the static variable will be the default value held by its type. The instance variable is created after its class instance is created, and if there is no initialization assignment, its initial value is the same as the static variable. Both more detailed description We are placed in the "sixth lecture domain method attribute and indexer" topic.

The pass value parameters, reference parameters, output parameters, array parameters are mainly for the parameter type of the method. Simple talk value parameters are a passage of the value of the variable, and the change in variables does not work in vitro within the method. The transmission value parameter itself is a slightly different variable, and the change in the data member points to this reference (handle) variable, the change in the actual memory block will remain changes in the method, but for the reference (handle) itself Do not work. The reference parameter is a passage of the handle of the variable, and any change in the variable will be reserved in vitro to the method. The output parameter is C # specifically for a plurality of return values, it is tailored, which is similar to the reference variable, but can not initialize before entering the method body, while other parameters are required to enter the method in the process of entering the method. C # requires a clear initialization . Array parameters are specifically designed to deliver a large number of array elements. It is essentially a quotable parameter of reference variables. They explain more detailed in the topic of the "sixth lecture domain method attribute and indexer".

The local variable is rigorous in the block statement, the for statement, the switch statement, and the variable declared in the USING statement. Its lifecycle is strictly limited to these statement blocks.

Constants determines its value when compiling, and do not modify throughout the program. Constant statements must be assigned. Due to its characteristics of its compile time, the reference type may only be String and NULL (except String, the build-by-reference builder must determine the value of the reference type when running).

Operators and expressions

C # retains all of the C , where the pointer operator (* and ->) and the reference operator (&) require a context of unsafe. C # abandon the scope of analysis of the operator (: :), will be changed to a single operator (.). We no longer explain those reserved C operators, which mainly introduces several operators with special significance: AS, IS, New, TypeOf, Sizeof, Stackalloc.

The AS operator is used to perform conversion between compatible types. When the conversion fails, the AS operator result is NULL. The IS operator is used to check if the object's runtime is compatible with a given type. When the expression is non-NULL and can be converted to the specified type, the IS operator is true, otherwise false. The AS and IS operators are designed based on the same type identification and conversion, and both have similar applications. In fact, Expression As Type is equivalent to Expression IS TYPE (TYPE) Expression: (Type) NULL. (The following describes which variables are placed separately about the heap and stack)

The NEW as an operator is used to create objects and call construct functions on the heap. It is worth noting that the value type object (eg, structure) is created on the stack, and the reference type object (such as class) is created on the heap. NEW is also used for modifiers for hiding inheritance members of the base class member. To hide the inheritance, use the same name to declare the member in the derived class and modify it with the new modifier. The TypeOf operator is used to get a type of System.Type object, and we will explain it in connection with the type system of Microsoft.NET in "Tenth Species and Mappings". The SIZEOF operator is used to obtain a value type (not suitable for reference type) size (in bytes). Stackalloc is used to allocate memory blocks on the stack, which is only valid in the initial value setting item of the local variable, similar to the _alloca of the C / C language. SizeOf and Starckalloc require the Unsafe context due to direct operations involving memory.

Some operators in C # can be overloaded like C . Operator overload makes the custom type (class or structure) can be conveniently expressing some common operations with a simple operator.

A combination of a series of operators and operands for completing a calculation result is called expressions. Like C , the expression of C # can be divided into two types of assignment expressions and Boolean expressions. C # does not introduce new expressions, we will not repeat it.

Namespace and statement

C # uses a namespace to organize the program. Named space can be nested.

C # statements can be divided into label statements, declarative statements, block statements, empty sentences, express statements, selection statements, repetitive statements, jump statements, try statements, check, and unchecked statements, LOCK statements, and using statements.

The label statement is primarily a GOTO jump design, and the C # does not allow jump across the method, but allows a small-scale way to jump. The declaration statement can be initialized simultaneously, and the instantiation statement of the object requires the new keyword. The block statement uses "{" and "}" to definken the word block, mainly to define the scope of the local variable. The empty statement is in the C # ";" said no semantics. Expression statements are configured by expressions.

Select statements There are two types of IF statements and Switch statements, and there is no difference from C . In addition to While, Do, For three cyclic structures, the Foreach statement is introduced to traverse all elements in the collection, but this requires specific interface support, and we will explain in detail in the following chapters.

The jump statement has Break, Continue, Goto, Return, Throw, the top four, the same as the semantics in C , the THROW statement and the TRY statement we will be in "Eleventh, COM interoperability, non-hosting programming and abnormal Processing "elaborate.

转载请注明原文地址:https://www.9cbs.com/read-121341.html

New Post(0)