C # study notes (1) reprint

xiaoxiao2021-03-06  63

http://74.com.cn/Article/2521.shtml

1. Structure (STRUCT) [attributes] [MODIFIERS] Struct Identifier [: interfaces] body [;] structure is similar, which represents the data structure that can contain data members and function members. Different from the class, the structure is a value type and does not require a heap allocation. The variable of the struct type directly contains the structure of the structure, and the class type variable contains references to the data (this variable is called object). The STRUCT type is suitable for a lightweight object such as a point, rectangle, and colors. Although a point may be represented as classes, the structure is more effective in some scenarios. In some cases, the cost of the structure is low. For example, if an array containing 1000 points object is declared, additional memory is assigned to each object. The structure can declare the constructor, but they must have parameters. The default (non-parameter) constructor of the declaration structure is wrong. Always provide default constructor to initialize structural members to their default values. The initialization instance field in the structure is wrong. In the class, instance objects must be initialized. When you create a structural object with the New operator, the structure object is created, and the appropriate constructor is called. Different from the class, instantiation of the structure may not use the New operator. If NEW is not used, the field will remain unfaised and the object is not available before initializing all fields. For structures, there is no inheritance like classes. A structure cannot inherit from another structure or class, and cannot be used as a class of a class. However, the structure is inherited from the base class Object. The structure can realize the interface, which is the same as the same type. [C ] Unlike C , the Struct keyword declaration cannot be used. In C #, classes and structures are semantically semantically. The structure is a value type, and the class is a reference type. 2, packing and unpacking (unpacking) packing is an implicit conversion of any interface type that value type to the Object type or to the value type. Assign a value of the value of the value to an object instance and copy the value into the new object. The keyword object. Cancel box is an explicit conversion from the Object type to value type or from the interface type to the value type that implements the interface. Cancel packing operations include: Check the object instance to make sure it is a packing value for a given value type. Copy this value from an instance to a value type variable. Example: INT i = 123; // a value typeObject Box = I; // boxingint j = (int) box; // unboXING box conversion unpack 3, implicit and explicit following converted belonging to implicit conversion: example : Object O = I; Identification conversion. Implicit numerical conversion. Implicit enumeration conversion. Implicit reference conversion. Packing conversion. Implicit constant expression conversion. User-defined implicit conversions. The following conversion belongs to explicit conversion: Object 0 = (Object) i; all implicit conversions. Explicit numerical conversion. Explicit enumeration conversion. Explicit reference conversion. Explicit interface conversion. Cancel the box conversion. User-defined explicit conversion 4. Delegate delegate Void D (int X); Class C {public static void m1 (int i) {/ * ... * /} public static void M2 (INT i) {/ * ... * /}} Class {....d CD1 = New D (C.M1); ............} The delegation is a data structure that references a static method or references an object instance and An example method of this object. In C or C , the client is closest to the function pointer, but the function pointer can only reference the static function, and the commission can simultaneously reference the static method and instance method. In the latter case, the delegate not only stores references to method entry points, but also stores references to objects that call their methods.

Unlike the C function pointer, the delegate is completely facing object; unlike the C pointer to the member function, entrust the simultaneous package object instance and method. The delegation declares defines the class derived from class system.delegate. The entrusted instance encapsulates one or more methods, each method being called an adjustable entity. For example, the modifier consists of an instance and the method on the instance. For static methods, the calling entity is constructed only by one method. For a given entrustment instance and an appropriate parameter set, you can use this parameter set to call all the methods of this delegate instance. An interesting and useful attribute of the entrusted instance is that it does not understand or does not care about the way it is packaged; the truly important is just that the method is compatible with the type of delegate, which makes the entrustment very suitable for "anonymous" calls. An optional shape specifies the parameters of the delegate, and the return type indicates the returning type of the delegate. If the following two conditions are true, the method and the delegate type are compatible: (compatible concept is to delegate the method of this declaration) .1 They have the same number of parameters, and the type, the same order, Parameter modifiers are also the same. 2 They have the same type. The delegate type in C # is the name equivalent, not the structure is equivalent. (However, please note: the instance of two different but structurally equivalent delegate types may be equal to the equal), accurately, two different entrustments with the same parameter list, signature, and return types are considered different. Entrusted type. The method set of the entrusted instance is called a call list. 5, Interface [Attributes] [MODIFIERS] Interface Identifier [: base-list] {interface-body} [;] an interface defines an agreement. The class or structure that implements the interface must comply with its agreement. The interface can be inherited from multiple base interfaces, and the class or structure can implement multiple interfaces. The interface can contain methods, attributes, events, and indexers. The interface itself does not provide the implementation of the members it defined. The interface only specifies the membership that implements the interface or interface that the interface must be provided. The interface can be a member of the namespace or class, and can contain the signature of the following members: Method Properties Index. An interface can be inherited from one or more base interfaces. The interface can be implemented by the class. The identifier of the implemented interface appears in the base list of the class. The inherited interface is called the explicit base reference for the interface. When the interface has one or more explicit base portions, in the interface declaration, the interface identifier follows a colon and a list of colons and the base interface identifier separated by commas. The base interface of the interface is an explicit base interface and its base reference. In other words, the base interface set is an explicit base reference, and their explicit base reference (according to this type) is completely transmitted. The interface inherits all members of its base. Interface members are accessed by I.M and I [A] forms access and index access expressions, where i is an instance of the interface type, M is the method, attribute, or event of the interface type, and a is the list of indexer parameters. The interface can be implemented by classes and structures. In order to indicate classes or structures, an interface identifier is included in the base class list of such or structures. The process of the implementation of the positioning interface member in the implementation class or structure is called an interface mapping. 6, the ObjectObject class type is all other types of final base classes. Each type in C # is derived directly or indirectly from the Object class type. An instance of any type of value can be used to the Object type. 7, an instance of the String Type String class represents the Unicode string. Although string is a reference type, the equal operator (== and! =) Is defined as a "value" (7.9.7 string equal) of the String object (not reference). This makes it more intuitive for the test of the character string. The string is the string type and writable in two forms, that is, use quotation marks to cause and use @. The character caused by quotation marks in double quotation marks ("), and can include any character string that includes @ @ @ including the excipt sequence, and use a double quotation mark.

The string that is caused by @ begins with @, and uses two quotes. To include a dual quotation number in a string that uses @, use two pairs of dual quotes: another usage of @ symbols to use the referenced (/ reference) identifier that happens to C # keywords. 8. Modified Relivery effect Access Demolecular Private Internal Protected Type and accessibility of type members. Access Unrestricted Classs that only include this class can access only the current engineering can access only class and inheritance of the class and inheritance of the class can access the Abstract Indicates that a class can only be other class. Const specifies the value that cannot be modified by the field or partial variable. Event declares an event. Extern Indicates that this method is implemented. Override provides a new implementation of virtual members inherited from base classes. Readonly declares a field that can only assign a part of the declaration or in the same class constructor. Sealed specified classes cannot be inherited. Static declaration belongs to a type itself instead of a member belonging to a specific object. Unsafe declares unsafe context. Virtual declares its implementation of the method or accessor that can be rewritten in the derived class. The volatile indicator field can be modified in the program by operating system, hardware, or concurrent thread or the like. 9, statement statement is program instruction. Unless otherwise stated, statements are executed in order. C # has the statement of the following category. Category CACKED, TRYDCH, UncheckedFixed statement fixedlock statement LOCK (1) Foreach statement repeats an embedded statement group for each element in an array or an object collection. The Foreach statement is used to loop access sets to get the required information, but not to change the collection content to avoid unpredictable side effects. The form of this statement is as follows: Foreach (Type Identifier In Expression) Statement To cycle access set, the collection must meet a specific requirement. Collection type: must be Interface, Class or Struct. Must include an instance method called GetEnumerator, such as ENUMERATOR (see below). Enumerator type (class or structure) must include: a property called Current, which returns ItemType or converts to this type of type. Attribute Accessor returns the current element of the collection. · A BOOL method called MoveNext, which is incremented by the counter and returns True when there is more items in the collection. There are three ways to use a collection: create a collection using the above guidance. This collection can only be used for C # programs. 1. Create a general collection using the above guidance and other Ienumerable interfaces. This collection can be used in other languages ​​(such as Visual Basic). 2. Use a predefined collection in the collection class. (2) The throw statement is used to issue a signal that appears abnormal (abnormal) during program execution. The form of the throw statement is: throw [expression]; Expression: exception object. It is omitted when the current exception object is raised again in the CatCH clause. (3) Try -catch statement try-catch statement consists of a try block and one or more CATCH clauses (for different exception specified processing procedures) thereafter.

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

New Post(0)