C # 2.0 Specification (generic one)

zhaozj2021-02-16  43

This article is a Microsoft's technical article for translation. For reference for learning C #, please do not use for commercial purposes. http://msdn.microsoft.com/vcsharp/team/language/default.aspx

Because this chapter is very long, you may need a few :) 20. Pan-type 20.1 Pan-type declaration generic category is a statement that needs to provide type parameters to form an actual type.

Class declarations can be selected to define type parameters. class-declaration: (class declaration) attributesopt class-modifiersopt class identifieropt type-parameter-listopt class -baseopt type-parameter-constraints-clauseopt class-body; opt (optional characteristic class identifier class modifier optional optional type parameter List Optional Base Class Option Class Parameter Constraints Optional Categories; Optional)

Unless the type of parameter list is provided, the class declaration may not provide the type parameterized constraint statement. A class declaration that provides a list of type parameters is a generic class declaration. In addition, any class embedded in the generic class declaration or generic structure declaration, it is a generic category, as the type of type parameters containing types must be provided to create a constructed type; generic class by using construction types Reference (20.5). Give Pancase Declaration Class List {} This is some examples of constructive types, List , List and List >. The structure type can be used for one or more parameters, such as List is called open constructed type. The type of constructor is not used, for example, List is called closed constructed type.

The generic type cannot be "overloaded"; otherwise, in one scope in a scope, with a normal type, the generic type must be uniquely named.

Class C {} Class C {} // error, c defines two Class C {} // errors, C defined two times, however, in non-qualified type name (20.9.3 The type lookup rules and members access (§20.9.4) in the use (§20.9.4) have indeed taken into account the number of type parameters.

20.1.1 Type Parameter Type Parameters can be provided on a class declaration. Each type parameter is a simple identifier that indicates a placeholder for type parameters for creating a constructor. Type parameters are the form of placeholders in the type of type that will be provided later. Instead, type parameters 20.5.1) just an alternative to the actual type when the constructor is referenced.

Type-parameter-list :( Type Parameter list :) () Type-Parameters: (Type Parameters :) Type-Parameter (Type Parameters) Type-Parameters Type-Parameter (Type Parameters, Type Parameters) Type-Parameter: (Type Parameters :) AttributeSopt Identifier (Feature Optional Identifier) ​​Defines a name in each type of parameters in class declarations (§3.3) defined a name. Thus, it cannot be the same name with another type of parameters or members declared in the class. Type parameters cannot be the same name with the type itself. The scope of the type parameters in a class (§3.7), including base classes, type parameter constraint statements, and categories. Unlike a member of the class, it did not extend to derived class. Within its scope, the type parameters can be used as a type. TYPE: value-type (Value-Type) Reference-Type TYPE-Parameter (Type Parameters) Since the type parameters can be instantiated by many different actual types, the type parameters will be compared to other types Slightly different operations and restrictions. Includes the following.

Type parameters cannot be used directly to declare a base type or interface

For members lookup rules on the type parameters, if the constraint exists, depending on the constraint applied to the type of parameters. See §20.7.4 in more detail.

Feasible conversion of type parameters depends on the constraint (if any) applied to the type of parameters. See §20.7.4 in detail.

Field NULL cannot be converted to the type given by the type parameters, unless the type parameter is constrained by a class constraint (20.7.4). However, a default expression (§20.8.1) can be used instead. In addition, the values ​​of the type given by one type parameter can be compared using "==" and "! =" (§20.8.4) with NULL.

If the type parameter is constrained by a constructor-constraint (20.7), the New expression can only be used with a type parameter.

Type parameters cannot be used anywhere in the characteristics.

Type parameters cannot be used for members access, or represent a static member or a type name of the nested type (§20.9.1, 20.9.4).

In an insecure code, type parameters cannot be used as managed types (§18.2).

As a type, type parameters are purely just a compile time. At runtime, each type parameter is bound to the runtime type, which is specified by the type of the type provided by the generic type declaration. To do this, the variable type of the type parameter declared is a closed type (20.5.2) when running. CLOSED TYPE. All statements and expressions perform the type parameters used at runtime, are the actual types provided by that parameter as the type of party.

20.1.2 Example Types Each class declaration has a constructed type associated with it, ie instance type (Instance Type). For a generic class declaration, the instance type is formed by creating a constructor (20.4) from the type declaration (§20.4), which uses each type corresponding to the type parameters. Since the instantiation type uses the type parameters, it is only valid within the type parameter scope (within the class declaration). The instance type is this type of this declaration. For non-extensive classes, the instance type is just a statement. Several declaration classes are shown below, as well as their instance types. Class A // Instance Type: A {Class B {} // Instance Type: a .b Class C {} // Instance Type: a .c } Class D {} // Instance Type: D20.1.3 Base class specification The base class specified in the class declaration can be a constructor (§20.5). A base class itself cannot be a type parameter, but the type parameters can be included in its scope.

Class Extend : v {} // Error, type parameters are used as a base type generic class declaration that cannot be used as direct or indirect base classes using SYSTEM.ATTRIBUTE. The base interface specified in a class declaration can be a configuration interface type (§20.5). The basegain itself cannot be type parameters, but can include type parameters in its scope, and the following code demonstrates how to implement and extend the constructor. Class C {} interface i1 {} class d: c , i1 {} Class E : c , i1 { } The base interface of the generic type declaration must meet the uniqueness rules described in §20.3.1. A method of rewriting or implementing a method from a base class or an interface, a particular type of method must be provided. The following code demonstrates how the method is rewritten and implemented. This will be further explained in §20.1.10. Class C {public Virtual Void M1 (U X, List y) {...}} interface i1 {v M2 (v x);} Class D: c , I1 {public override void m1 (string x, list y) {...} public string m2 (string x) {...}}

20.1.4 All members of the general class of generic classes can use the type parameters from any closure class (Enclosing Class) directly or as part of the enclosing class. When a particular closed constructor is used, each use of the type parameter is replaced by the actual type of the constructive provided by the constructive type. E.g

Class C {public V f1; public c f2 = null;

Public c (v x) {this.f1 = x; this.f2 = this;}} class application {static void

Main

() {C x1 = new c (1); console.writeline (x1.f1); // Print 1 c x2 = new c (3.1415); console.writeline X2.f1); // Print 3.1415}} In the instance function member, the THIS type is the instance type of the declaration (20.1.2). In addition to using type parameters as types and members, the same rules are also followed in the generic class statement. Additional rules for specific species will be discussed in the following sections. 20.1.5 Static fields in generic classes in a generic class declaration, are shared in all instances of the same closed construction (§20.5.2), but in an instance of different enclosed constructors [1] It is not shared. These rules apply to the type of parameters regardless of the type of static variable. For example, Class C {static int count = 0; public c () {count ;} public static int count {get {return count;}}} class application {static voidmain

() {C x1 = new c (); console.writeline (c .count); // Print 1 c x2 = new c (); console.writeline (C .count); // Print 1 c x3 = new c (); console.writeLine (c .count); // Print 2}}

[1] This is easy to understand, because at runtime, different enclosed constructive types belong to different types, such as List and list The instance of both of this cannot share static variables.

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

New Post(0)