c#2.0 specification (Generic one)

Source: Internet
Author: User

Since this chapter is very long it may take a few points:)

20. Generic type

20.1 generic class declaration

A generic class declaration is a declaration of a class that needs to provide a type parameter to form the actual type.



A class declaration can optionally define a type parameter.

Class-declaration: (class declaration)
attributesopt class-modifiersopt class identifieropt type-parameter-listopt class–baseopt type-parameter-constraints-clauseopt class-body;opt (attribute optional class modifier Optional class identifier optional type parameter list optional base class optional type parameter constraint statement optional class body; optional)
Class declarations can provide no type parameterized constraint statements unless a type argument list is provided.

A class declaration that provides a list of type parameters is a generic class declaration. In addition, any class that is embedded in a generic class declaration or a generic struct declaration is itself a generic class declaration, because a type parameter of the containing type must be supplied to create the constructed type (constructed type);

Generic classes are referenced by using constructed types (§20.5). Given generic class declaration

Class list<t>{}
Here are some examples of constructed types,list<t>,list<int> and list<list<string>>. A constructed type can use one or more parameters, such as list<t>, which is called an open constructed type. Constructed types that do not use type parameters, such as list<int>, are called closed constructed types (closed constructed type).

Generic types cannot be "overloaded"; that is, the generic type must be uniquely named as a normal type within a scope.


Class c{}
Class c<v>{}//error, C defined twice
Class c<u,v>{}//error, C defined twice
However, the type lookup rules and member accesses (§20.9.4) used in unqualified type name lookup (§20.9.3) do take 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 the type parameter that is used to create a constructed type. The type parameter is a placeholder for the form of the type that will be provided later. Instead, the type parameter §20.5.1) is simply an alternative to the actual type when the constructed type is referenced.

Type-parameter-list: (type parameter list:)
<type-parameters> (< type parameters >)
Type-parameters: (type parameter:)
Type-parameter (type parameter)
Type-parameters type-parameter (type parameter, type parameter)
Type-parameter: (type parameter:)
attributesopt identifier (attribute optional identifier)

Each type parameter in a class declaration defines a name in the class's declaration space (§3.3). As a result, it cannot have the same name as another type parameter or a member declared in a class. The type parameter cannot have the same name as the type itself.

The scope of the type parameter in a class (§3.7), including the base class, the type parameter constraint statement, and the class body. Unlike a member of a class, it does not extend to derived classes. Within its scope, the type parameter can be used as a type.

Type (types):

Value-type (value type)
Reference-type (Reference type)
Type-parameter (type parameter)
Because type parameters can be instantiated by many different actual type arguments, the type parameter has slightly different operations and restrictions than other types. Includes the following content.

Type parameters cannot be used to directly declare a base type or interface
For a member lookup rule on a type parameter, if the constraint exists, it depends on the constraint applied to the type parameter. Refer to §20.7.4 for more details.



A valid conversion of a type parameter depends on the constraint (if any) applied to that type parameter. Refer to §20.7.4 for details.
Literal null cannot be converted to a type given by the type parameter, unless the type parameter is constrained by a class constraint (§20.7.4). However, you can use a default value expression (§20.8.1) instead. In addition, a value of a type given by a type parameter can be compared with null by using "= =" and "! =" (§20.8.4).
If a type parameter is constrained by a constructor constraint (Constructor-constraint) (§20.7), the new expression can only be used with one type parameter.
Type parameters cannot be used anywhere within an attribute.
A type parameter cannot be used for member access, or a static member or a type name for a nested type (§20.9.1, §20.9.4).
In unsafe code, the type parameter cannot be used as a managed type (§18.2).

As a type, a type parameter is purely a compile-time artifact. At run time, each type parameter is bound to a run-time type, which is specified by the type arguments provided by the generic type declaration. To do this, at run time, the variable type declared with the type parameter is a closed type (closed type) (§20.5.2). All statements and expressions are executed at run time using the type parameter, which is the actual type provided by that parameter as a type argument.

20.1.2 instance Type

Each class declaration has a constructed type associated with it, that is, the instance type (instance type). For a generic class declaration, the instance type is formed by creating a constructed type (§20.4) from the type declaration, which uses each type argument corresponding to the type parameter. Because the instantiated type uses a type parameter, it is only valid within the scope of the type parameter (within the class declaration). The instance type is the type of this in the class declaration. For non-generic classes, the instance type is just a claim type. The following shows several declarative classes, along with their instance types.

Class A<t>//instance type: A<t>{class b{}//instance type: A<t>. Bclass c<u>{}//instance type: A<t>. C<u>}class d{}//instance type: D

20.1.3 base class Specification

The base class specified in the class declaration can be a constructed type (§20.5). A base class cannot itself be a type parameter, but it can contain a type parameter within its scope.


Class Extend<v>: v{}//error, type parameter is used as base class
Generic class declarations cannot use System.Attribute as a direct or indirect base class.

The base interface specified in a class declaration can be a constructed interface type (§20.5). The base interface itself cannot be a type parameter, but it can contain type parameters within its scope, and the following code demonstrates how to implement and extend a constructed type.

Class C<u,v>{}interface I1<v>{}class d:c<string, Int>,i1<string>{}class E<T>:C<int ,t>, i1<t>{}

The base interface of a generic type declaration must satisfy the uniqueness rules described in §20.3.1.

To override or implement methods of a method from a base class or interface, you must provide an appropriate method for a particular type. The following code shows how the method is overridden and implemented. This will be further explained in the §20.1.10.

Class c<u,v>{public virtual void M1 (U x, list<v> y) {...}} Interface I1<v>{v M2 (V x);} Class d:c<string, int>,i1<string>{public override void M1 (string x, list<int> y) {...} public string M2 (String x) {...}}

20.1.4 members of generic classes

All members of a generic class can use the type parameter from any enclosing class (enclosing class), either directly or as part of a constructed type. When a particular enclosing constructed type is used at run time, each use of the type parameter is replaced by the actual type argument provided by the constructed type. For example


Class c<v>{public V F1, public c<v> f2=null;public C (V x) {this.f1 = X;this.f2 = this;}} Class application{static void Main () {c<int> x1= new C<int > (1); Console.WriteLine (X1.F1); Print 1c<double> x2 = new c<double> (3.1415); Console.WriteLine (X2.F1); Print 3.1415}}

Within an instance function member, the type of this is the declared instance type (§20.1.2).

In addition to using type parameters as types and members, the same rules are followed in generic class declarations as non-generic class members. Additional rules that apply to specific kinds of members are discussed in the following sections.

20.1.5 static fields in a generic class

Static variables in a generic class declaration are shared across all instances of the same enclosing constructed type (§20.5.2), but in instances of different enclosing constructed types [1], are not shared. These rules apply regardless of whether the type of the static variable contains that type parameter.

For example

Class C<v>{static int count = 0;public C () {count++;} public static int Count{get{return Count;}}} Class application{static void Main () {c<int> x1 = new c<int> (); Console.WriteLine (C<INT>. Count);//print 1c<double> x2 = new c<double> (); Console.WriteLine (C<INT>. Count);//print 1c<int> x3 = new c<int> (); Console.WriteLine (C<INT>. Count);//Print 2}}

[1] This is very easy to understand, because at runtime, different types of closed construction, are different types, such as list<int> and list<string> the two instances are not shared static variables.

The above is c#2.0 specification (generic one) content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.