C # sharp experience second lecture C # language basic introduction

xiaoxiao2021-03-06  103

Nanjing University of Posts and Telecommunications Li Jianzhong (Cornyfield@263.net)

Prior to experience C #, the mastery of language is essential is an essential ring. Due to the C # Basic language, there are many sources from C / C , which is simply introduced here for those places and C / C , we will experience the key language basic knowledge that distinguishes between traditional C / C .

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. The CLR nature of the platform's garbage collection, we are placed in the "COM Interoperable Non-Manage Programming and Exception". Value type variable itself contains their data, and the reference type variable contains references to the memory block containing the data or called handles. From the following picture, you can clearly see the differences in both:

Possible issues brought by reference is to cause changes in any reference variable when multiple variables references the same memory blocks. 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. Enumeration Types and User Custom Structure Types We will explain in detail in the "ninth lecture structure, enumeration, array and string". 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 symbol integers sbyte val = 12; short ?????? 16-bit is symbol integer short val = 12; int ????????? ? 32-bit has a symbol integer int val = 12; long ??????? 64-bit has a symbol integer 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 var 2 = 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 Val1 = 1.23; double VAL2 = 4.56d; l Boolean BOOL VAL1 = true; BOOL VAL2 = FALSE; char ???? ??? Character type, Unicode encoded char Val = 'h'; decimal ?? 28 valid digital 128-bit decimal type decimal val = 1.23m; reference types are 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.

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 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. 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.

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.

Checked / unchecked statement is primarily used in the context of overflow checking in numerical operations. The LOCK statement is primarily used for the lock control of the thread semaphore. The USING statement is primarily used for fragment resource management. These we have specific involves in subsequent chapters.

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

New Post(0)