c#2.0 specification (Generic one)

Source: Internet
Author: User
Tags constructor count expression extend interface variables reference valid
This article is a technical article of the translation of Microsoft. For the reference of Friends of C #, please do not use for commercial purposes. Http://msdn.microsoft.com/vcsharp/team/language/default.aspx
Because this chapter is very long may need to be divided into several:)
20. Generic type
20.1 generic class declarations
A generic class declaration is a declaration that needs to provide a type parameter to form a class of 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)

A class declaration may not provide a type parameterized constraint statement unless a list of type parameters 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 structure declaration itself is a generic class declaration, because the type parameter of the containing type must be supplied to create the constructed type (constructed type);

A generic class is referenced by using a constructed type (§20.5). Given generic class declaration

Class list<t>{}

This is 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 construct type (the open constructed type). The constructed type of the type parameter is not used, for example, List<int> is called a closed constructed type (closed constructed type).



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





Class c{}

Class c<v>{}//error, C defines twice

Class c<u,v>{}//error, C defines twice

However, the type lookup rules and member access (§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 parameters
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. A type parameter is a form placeholder for the type that will be supplied later. Instead, the type parameter §20.5.1 is only 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 (optional identifier for attributes)



Each type parameter in a class declaration defines a name in the declaration space (§3.3) of the class. Thus, it cannot have the same name as another type parameter or a member declared in the class. Type parameters cannot have the same name as the type itself.

The scope (§3.7) of a type parameter in a class, including the base class, the type parameter constraint statement, and the class body. Unlike a member of a class, it does not extend to a derived class. Within its scope, a 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, type parameters will have slightly different operations and limitations than other types. Includes the following content.

Type parameters cannot be used to declare a base type or interface directly
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 in more detail.




A valid conversion of a type parameter depends on the constraint applied to the type parameter, if any. Refer to §20.7.4 in detail.
Literal null cannot be converted to a type given by a type parameter, unless the type parameter is constrained by a class constraint (§20.7.4). However, a default value expression (§20.8.1) can be used instead. In addition, the value of the 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.
The type parameter cannot be used for member access, or it represents a static member or the type name of a nested type (§20.9.1, §20.9.4).
In unsafe code, type parameters cannot be used as managed types (§18.2).
As a type, the type parameter is purely a compile-time widget. At run time, each type parameter is bound to the 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). The type arguments used by all statements and expressions at run time are the actual types that are provided by that parameter as a type argument.

20.1.2 instance Type
Each class declaration has a constructed type that is associated with it, that is, the instance type (instance type). For a generic class declaration, an instance type is formed by creating a constructed type (§20.4) from a type declaration that uses each type argument that corresponds to a type parameter. Because the instantiated type uses the type parameter, it is only valid within the scope of the type argument (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 declaration type. Several declaration classes are shown below, along with their instance types.

Class A<t>//instance type:a<t>

{

Class b{}//instance type: A&LT;T&GT; B

Class c<u>{}//instance type: A&LT;T&GT; 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 type parameters within its scope.





Class Extend<v>: v{}//error, type parameter 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 shows how to implement and extend the 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 for a generic type declaration must satisfy the uniqueness rule described in §20.3.1.

Methods that override or implement a method's class from a base class or interface must provide an appropriate method for a specific type. The following code shows how the method is overridden and implemented. This will be explained further 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 a member of a generic class
All members of a generic class can use type parameters directly or as part of a constructed type from any enclosing class (enclosing class). When a particular closed 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 1

c<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, rules that are identical to Non-generic class members are also followed in generic class declarations. Additional rules that apply to a particular kind of member are discussed in later sections.

20.1.5 static fields in a generic class
Static variables in a generic class declaration are shared in all instances of the same enclosing constructed type (§20.5.2), but are not shared in instances of different enclosing constructed types [1]. 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&LT;INT&GT; Count);//print 1

c<double> x2 = new c<double> ();

Console.WriteLine (C&LT;INT&GT; Count);//print 1

C<int> x3 = new c<int> ();

Console.WriteLine (C&LT;INT&GT; Count);//Print 2

}

}



--------------------------------------------------------------------------------

[1] This is easy to understand, because at runtime, different closed constructed types are of different types, such as list<int> and list<string>, where the instance of the two is not able to share static variables.


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.