- •Foreword
- •Introduction
- •Scope
- •Conformance
- •Normative references
- •Definitions
- •Notational conventions
- •Acronyms and abbreviations
- •General description
- •Language overview
- •Getting started
- •Types
- •Predefined types
- •Conversions
- •Array types
- •Type system unification
- •Variables and parameters
- •Automatic memory management
- •Expressions
- •Statements
- •Classes
- •Constants
- •Fields
- •Methods
- •Properties
- •Events
- •Operators
- •Indexers
- •Instance constructors
- •Destructors
- •Static constructors
- •Inheritance
- •Static classes
- •Partial type declarations
- •Structs
- •Interfaces
- •Delegates
- •Enums
- •Namespaces and assemblies
- •Versioning
- •Extern Aliases
- •Attributes
- •Generics
- •Why generics?
- •Creating and consuming generics
- •Multiple type parameters
- •Constraints
- •Generic methods
- •Anonymous methods
- •Iterators
- •Lexical structure
- •Programs
- •Grammars
- •Lexical grammar
- •Syntactic grammar
- •Grammar ambiguities
- •Lexical analysis
- •Line terminators
- •Comments
- •White space
- •Tokens
- •Unicode escape sequences
- •Identifiers
- •Keywords
- •Literals
- •Boolean literals
- •Integer literals
- •Real literals
- •Character literals
- •String literals
- •The null literal
- •Operators and punctuators
- •Pre-processing directives
- •Conditional compilation symbols
- •Pre-processing expressions
- •Declaration directives
- •Conditional compilation directives
- •Diagnostic directives
- •Region control
- •Line directives
- •Pragma directives
- •Basic concepts
- •Application startup
- •Application termination
- •Declarations
- •Members
- •Namespace members
- •Struct members
- •Enumeration members
- •Class members
- •Interface members
- •Array members
- •Delegate members
- •Member access
- •Declared accessibility
- •Accessibility domains
- •Protected access for instance members
- •Accessibility constraints
- •Signatures and overloading
- •Scopes
- •Name hiding
- •Hiding through nesting
- •Hiding through inheritance
- •Namespace and type names
- •Unqualified name
- •Fully qualified names
- •Automatic memory management
- •Execution order
- •Types
- •Value types
- •The System.ValueType type
- •Default constructors
- •Struct types
- •Simple types
- •Integral types
- •Floating point types
- •The decimal type
- •The bool type
- •Enumeration types
- •Reference types
- •Class types
- •The object type
- •The string type
- •Interface types
- •Array types
- •Delegate types
- •Boxing and unboxing
- •Boxing conversions
- •Unboxing conversions
- •Variables
- •Variable categories
- •Static variables
- •Instance variables
- •Instance variables in classes
- •Instance variables in structs
- •Array elements
- •Value parameters
- •Reference parameters
- •Output parameters
- •Local variables
- •Default values
- •Definite assignment
- •Initially assigned variables
- •Initially unassigned variables
- •Precise rules for determining definite assignment
- •General rules for statements
- •Block statements, checked, and unchecked statements
- •Expression statements
- •Declaration statements
- •If statements
- •Switch statements
- •While statements
- •Do statements
- •For statements
- •Break, continue, and goto statements
- •Throw statements
- •Return statements
- •Try-catch statements
- •Try-finally statements
- •Try-catch-finally statements
- •Foreach statements
- •Using statements
- •Lock statements
- •General rules for simple expressions
- •General rules for expressions with embedded expressions
- •Invocation expressions and object creation expressions
- •Simple assignment expressions
- •&& expressions
- •|| expressions
- •! expressions
- •?: expressions
- •Anonymous method expressions
- •Yield statements
- •Variable references
- •Atomicity of variable references
- •Conversions
- •Implicit conversions
- •Identity conversion
- •Implicit numeric conversions
- •Implicit enumeration conversions
- •Implicit reference conversions
- •Boxing conversions
- •Implicit type parameter conversions
- •Implicit constant expression conversions
- •User-defined implicit conversions
- •Explicit conversions
- •Explicit numeric conversions
- •Explicit enumeration conversions
- •Explicit reference conversions
- •Unboxing conversions
- •User-defined explicit conversions
- •Standard conversions
- •Standard implicit conversions
- •Standard explicit conversions
- •User-defined conversions
- •Permitted user-defined conversions
- •Evaluation of user-defined conversions
- •User-defined implicit conversions
- •User-defined explicit conversions
- •Anonymous method conversions
- •Method group conversions
- •Expressions
- •Expression classifications
- •Values of expressions
- •Operators
- •Operator precedence and associativity
- •Operator overloading
- •Unary operator overload resolution
- •Binary operator overload resolution
- •Candidate user-defined operators
- •Numeric promotions
- •Unary numeric promotions
- •Binary numeric promotions
- •Member lookup
- •Base types
- •Function members
- •Argument lists
- •Overload resolution
- •Applicable function member
- •Better function member
- •Better conversion
- •Function member invocation
- •Invocations on boxed instances
- •Primary expressions
- •Literals
- •Simple names
- •Invariant meaning in blocks
- •Parenthesized expressions
- •Member access
- •Identical simple names and type names
- •Invocation expressions
- •Method invocations
- •Delegate invocations
- •Element access
- •Array access
- •Indexer access
- •This access
- •Base access
- •Postfix increment and decrement operators
- •The new operator
- •Object creation expressions
- •Array creation expressions
- •Delegate creation expressions
- •The typeof operator
- •The checked and unchecked operators
- •Default value expression
- •Anonymous methods
- •Anonymous method signatures
- •Anonymous method blocks
- •Outer variables
- •Captured outer variables
- •Instantiation of local variables
- •Anonymous method evaluation
- •Implementation example
- •Unary expressions
- •Unary plus operator
- •Unary minus operator
- •Logical negation operator
- •Bitwise complement operator
- •Prefix increment and decrement operators
- •Cast expressions
- •Arithmetic operators
- •Multiplication operator
- •Division operator
- •Remainder operator
- •Addition operator
- •Subtraction operator
- •Shift operators
- •Relational and type-testing operators
- •Integer comparison operators
- •Floating-point comparison operators
- •Decimal comparison operators
- •Boolean equality operators
- •Enumeration comparison operators
- •Reference type equality operators
- •String equality operators
- •Delegate equality operators
- •The is operator
- •The as operator
- •Logical operators
- •Integer logical operators
- •Enumeration logical operators
- •Boolean logical operators
- •Conditional logical operators
- •Boolean conditional logical operators
- •User-defined conditional logical operators
- •Conditional operator
- •Assignment operators
- •Simple assignment
- •Compound assignment
- •Event assignment
- •Expression
- •Constant expressions
- •Boolean expressions
- •Statements
- •End points and reachability
- •Blocks
- •Statement lists
- •The empty statement
- •Labeled statements
- •Declaration statements
- •Local variable declarations
- •Local constant declarations
- •Expression statements
- •Selection statements
- •The if statement
- •The switch statement
- •Iteration statements
- •The while statement
- •The do statement
- •The for statement
- •The foreach statement
- •Jump statements
- •The break statement
- •The continue statement
- •The goto statement
- •The return statement
- •The throw statement
- •The try statement
- •The checked and unchecked statements
- •The lock statement
- •The using statement
- •The yield statement
- •Namespaces
- •Compilation units
- •Namespace declarations
- •Extern alias directives
- •Using directives
- •Using alias directives
- •Using namespace directives
- •Namespace members
- •Type declarations
- •Qualified alias member
- •Classes
- •Class declarations
- •Class modifiers
- •Abstract classes
- •Sealed classes
- •Static classes
- •Class base specification
- •Base classes
- •Interface implementations
- •Class body
- •Partial declarations
- •Class members
- •Inheritance
- •The new modifier
- •Access modifiers
- •Constituent types
- •Static and instance members
- •Nested types
- •Fully qualified name
- •Declared accessibility
- •Hiding
- •this access
- •Reserved member names
- •Member names reserved for properties
- •Member names reserved for events
- •Member names reserved for indexers
- •Member names reserved for destructors
- •Constants
- •Fields
- •Static and instance fields
- •Readonly fields
- •Using static readonly fields for constants
- •Versioning of constants and static readonly fields
- •Volatile fields
- •Field initialization
- •Variable initializers
- •Static field initialization
- •Instance field initialization
- •Methods
- •Method parameters
- •Value parameters
- •Reference parameters
- •Output parameters
- •Parameter arrays
- •Static and instance methods
- •Virtual methods
- •Override methods
- •Sealed methods
- •Abstract methods
- •External methods
- •Method body
- •Method overloading
- •Properties
- •Static and instance properties
- •Accessors
- •Virtual, sealed, override, and abstract accessors
- •Events
- •Field-like events
- •Event accessors
- •Static and instance events
- •Virtual, sealed, override, and abstract accessors
- •Indexers
- •Indexer overloading
- •Operators
- •Unary operators
- •Binary operators
- •Conversion operators
- •Instance constructors
- •Constructor initializers
- •Instance variable initializers
- •Constructor execution
- •Default constructors
- •Private constructors
- •Optional instance constructor parameters
- •Static constructors
- •Destructors
- •Structs
- •Struct declarations
- •Struct modifiers
- •Struct interfaces
- •Struct body
- •Struct members
- •Class and struct differences
- •Value semantics
- •Inheritance
- •Assignment
- •Default values
- •Boxing and unboxing
- •Meaning of this
- •Field initializers
- •Constructors
- •Destructors
- •Static constructors
- •Struct examples
- •Database integer type
- •Database boolean type
- •Arrays
- •Array types
- •The System.Array type
- •Array creation
- •Array element access
- •Array members
- •Array covariance
- •Arrays and the generic IList interface
- •Array initializers
- •Interfaces
- •Interface declarations
- •Interface modifiers
- •Base interfaces
- •Interface body
- •Interface members
- •Interface methods
- •Interface properties
- •Interface events
- •Interface indexers
- •Interface member access
- •Fully qualified interface member names
- •Interface implementations
- •Explicit interface member implementations
- •Interface mapping
- •Interface implementation inheritance
- •Interface re-implementation
- •Abstract classes and interfaces
- •Enums
- •Enum declarations
- •Enum modifiers
- •Enum members
- •The System.Enum type
- •Enum values and operations
- •Delegates
- •Delegate declarations
- •Delegate instantiation
- •Delegate invocation
- •Exceptions
- •Causes of exceptions
- •The System.Exception class
- •How exceptions are handled
- •Common Exception Classes
- •Attributes
- •Attribute classes
- •Attribute usage
- •Positional and named parameters
- •Attribute parameter types
- •Attribute specification
- •Attribute instances
- •Compilation of an attribute
- •Run-time retrieval of an attribute instance
- •Reserved attributes
- •The AttributeUsage attribute
- •The Conditional attribute
- •Conditional Methods
- •Conditional Attribute Classes
- •The Obsolete attribute
- •Unsafe code
- •Unsafe contexts
- •Pointer types
- •Fixed and moveable variables
- •Pointer conversions
- •Pointers in expressions
- •Pointer indirection
- •Pointer member access
- •Pointer element access
- •The address-of operator
- •Pointer increment and decrement
- •Pointer arithmetic
- •Pointer comparison
- •The sizeof operator
- •The fixed statement
- •Stack allocation
- •Dynamic memory allocation
- •Generics
- •Generic class declarations
- •Type parameters
- •The instance type
- •Members of generic classes
- •Static fields in generic classes
- •Static constructors in generic classes
- •Accessing protected members
- •Overloading in generic classes
- •Parameter array methods and type parameters
- •Overriding and generic classes
- •Operators in generic classes
- •Nested types in generic classes
- •Generic struct declarations
- •Generic interface declarations
- •Uniqueness of implemented interfaces
- •Explicit interface member implementations
- •Generic delegate declarations
- •Constructed types
- •Type arguments
- •Open and closed types
- •Base classes and interfaces of a constructed type
- •Members of a constructed type
- •Accessibility of a constructed type
- •Conversions
- •Using alias directives
- •Generic methods
- •Generic method signatures
- •Virtual generic methods
- •Calling generic methods
- •Inference of type arguments
- •Using a generic method with a delegate
- •Constraints
- •Satisfying constraints
- •Member lookup on type parameters
- •Type parameters and boxing
- •Conversions involving type parameters
- •Iterators
- •Iterator blocks
- •Enumerator interfaces
- •Enumerable interfaces
- •Yield type
- •This access
- •Enumerator objects
- •The MoveNext method
- •The Current property
- •The Dispose method
- •Enumerable objects
- •The GetEnumerator method
- •Implementation example
- •Lexical grammar
- •Line terminators
- •White space
- •Comments
- •Unicode character escape sequences
- •Identifiers
- •Keywords
- •Literals
- •Operators and punctuators
- •Pre-processing directives
- •Syntactic grammar
- •Basic concepts
- •Types
- •Expressions
- •Statements
- •Classes
- •Structs
- •Arrays
- •Interfaces
- •Enums
- •Delegates
- •Attributes
- •Generics
- •Grammar extensions for unsafe code
- •Undefined behavior
- •Implementation-defined behavior
- •Unspecified behavior
- •Other Issues
- •Capitalization styles
- •Pascal casing
- •Camel casing
- •All uppercase
- •Capitalization summary
- •Word choice
- •Namespaces
- •Classes
- •Interfaces
- •Enums
- •Static fields
- •Parameters
- •Methods
- •Properties
- •Events
- •Case sensitivity
- •Avoiding type name confusion
- •Documentation Comments
- •Introduction
- •Recommended tags
- •<code>
- •<example>
- •<exception>
- •<list>
- •<para>
- •<param>
- •<paramref>
- •<permission>
- •<remarks>
- •<returns>
- •<seealso>
- •<summary>
- •<value>
- •Processing the documentation file
- •ID string format
- •ID string examples
- •An example
- •C# source code
- •Resulting XML
Chapter 26 Generics
1class C<T>
2{
3protected T x;
4}
5class D<T>: C<T>
6{
7 |
static void F() { |
8 |
D<T> dt = new D<T>(); |
9 |
D<int> di = new D<int>(); |
10 |
D<string> ds = new D<string>(); |
11 |
dt.x = default(T); |
12 |
di.x = 123; |
13 |
ds.x = "test"; |
14}
15}
16the three assignments to x are permitted because they all take place through instances of class types
17constructed from the generic type. end example]
1826.1.7 Overloading in generic classes
19Methods, constructors, indexers, and operators within a generic class declaration can be overloaded;
20however, overloading is constrained so that ambiguities cannot occur within constructed classes. Two
21function members declared with the same names in the same generic class declaration shall have parameter
22types such that no closed constructed type could have two members with the same name and signature.
23When considering all possible closed constructed types, this rule includes type arguments that do not
24currently exist in the current program, but could be written. Type constraints (§26.7) on the type parameter
25are ignored for the purpose of this rule.
26[Example: The following examples show overloads that are valid and invalid according to this rule:
27interface I1<T> {…}
28interface I2<T> {…}
29class G1<U>
30{
31 |
long F1(U u) {…} |
// |
Invalid overload, G<int> would have two |
32 |
int F1(int i) {…} |
// |
members with the same signature |
33 |
void F2(U u1, U u2) {…} // Valid overload, no type argument for U |
||
34 |
void F2(int i, string s) {…} |
||
35 |
|
// could be int and string simultaneously |
|
36 |
void F3(I1<U> a) {…} |
// Valid overload |
|
37 |
void F3(I2<U> a) {…} |
|
|
38 |
void F4(U a) {…} |
// Valid overload |
|
39void F4(U[] a) {…}
40}
41class G2<U, V>
42{
43 |
void F5(U u, V v) {…} |
// Invalid overload, G2<int, int> would |
44 |
void F5(V v, U u) {…} |
// have two members with the same |
45 |
|
// signature |
46 |
void F6(U u, I1<V> v) {…} |
// Invalid overload, G2<I1<int>, int> |
47 |
|
// would have two members with the |
48 |
void F6(I1<V> v, U u) {…} |
// same signature |
49 |
void F7(U u1, I1<V> v2) {…} |
// Valid overload, U cannot be V <V> |
50 |
void F7(V v1, U u2) {…} |
// and I1 simultaneously |
51 |
void F8(ref U u) {…} |
// Invalid overload |
52void F8(out V v) {…}
53}
54class C1 {…}
55class C2 {…}
401
C# LANGUAGE SPECIFICATION
1class G3<U, V> where U: C1 where V: C2
2{
3 |
void |
F9(U |
u) |
{…} |
// |
Invalid overload, constraints on U and V |
4 |
void |
F9(V |
v) |
{…} |
// |
are ignored when checking overloads |
5}
6end example]
726.1.8 Parameter array methods and type parameters
8Type parameters can be used in the type of a parameter array. [Example: Given the declaration
9class C<V>
10{
11static void F(int x, int y, params V[] args) { … }
12}
13the following invocations of the expanded form of the method:
14C<int>.F(10, 20);
15C<object>.F(10, 20, 30, 40);
16C<string>.F(10, 20, "hello", "goodbye");
17correspond exactly to:
18C<int>.F(10, 20, new int[] {});
19C<object>.F(10, 20, new object[] {30, 40});
20C<string>.F(10, 20, new string[] {"hello", "goodbye"} );
21end example]
2226.1.9 Overriding and generic classes
23Function members in generic classes can override function members in base classes, as usual. If the base
24class is a non-generic type or a closed constructed type, then any overriding function member cannot have
25constituent types that involve type parameters. When determining the overridden base member, the members
26of the base classes shall be determined by substituting type arguments, as described in §26.5.4. Once the
27members of the base classes are determined, the rules for overriding are the same as for non-generic classes.
28[Example: The following example demonstrates how the overriding rules work in the presence of generics:
29abstract class C<T>
30{
31 |
public |
virtual |
T F() {…} |
32 |
public |
virtual |
C<T> G() {…} |
33public virtual void H(C<T> x) {…}
34}
35class D: C<string>
36{
37 |
public override string F() {…} |
// Ok |
38 |
public override C<string> G() {…} |
// Ok |
39 |
public override void H(C<int> x) {…} |
// Error, should be C<string> |
40}
41class E<T, U>: C<U>
42{
43 |
public override U F() {…} |
// Ok |
44 |
public override C<U> G() {…} |
// Ok |
45 |
public override void H(C<T> x) {…} |
// Error, should be C<U> |
46}
47end example]
4826.1.10 Operators in generic classes
49Generic class declarations can define operators, following the same rules as non-generic class declarations.
50The instance type (§26.1.2) of the class declaration shall be used in the declaration of operators in a manner
51analogous to the normal rules for operators, as follows:
402
Chapter 26 Generics
1• A unary operator shall take a single parameter of the instance type. The unary ++ and -- operators shall
2return the instance type or a type derived from the instance type.
3• At least one of the parameters of a binary operator shall be of the instance type.
4• Either the parameter type or the return type of a conversion operator shall be of the instance type.
5[Example: The following shows some examples of valid operator declarations in a generic class:
6class X<T>
7{
8 |
public |
static |
X<T> operator ++(X<T> operand) {…} |
9 |
public |
static |
int operator *(X<T> op1, int op2) {…} |
10public static explicit operator X<T>(T value) {…}
11}
12end example]
13For a conversion operator that converts from a source type S to a target type T, when the rules specified in
14§17.9.3 are applied, any type parameters associated with S or T are considered to be unique types that have
15no inheritance relationship with other types, and any constraints on those type parameters are ignored.
16[Example: In the following code
17class C<T> {…}
18class D<T>: C<T>
19{
20 |
public |
static |
implicit |
operator |
C<int>(D<T> value) {…} |
// Ok |
21 |
public |
static |
implicit |
operator |
C<T>(D<T> value) {…} |
// Error |
22}
23the first operator declaration is permitted because, for the purposes of §17.9.3, T and int are considered
24unique types with no relationship. However, the second operator is an error because C<T> is the base class
25of D<T>. end example]
26It is possible to declare operators that, for some type arguments, specify conversions that already exist as
27pre-defined conversions. [Example: In the following code
28struct Convertible<T>
29{
30 |
public static implicit operator Convertible<T>(T value) {…} |
31public static explicit operator T(Convertible<T> value) {…}
32}
33when type object is specified as a type argument for T, the second operator declares a conversion that
34already exists (an implicit, and therefore also an explicit, conversion exists from any type to type object).
35end example]
36In cases where a pre-defined conversion exists between two types, any user-defined conversions between
37those types are ignored. Specifically:
38• If a pre-defined implicit conversion (§13.1) exists from type S to type T, all user-defined conversions
39(implicit or explicit) from S to T are ignored.
40• If a pre-defined explicit conversion (§13.2) exists from type S to type T, any user-defined explicit
41conversions from S to T are ignored. However, user-defined implicit conversions from S to T are still
42considered.
43[Example: For all types but object, the operators declared by the Convertible<T> type above do not
44conflict with pre-defined conversions. For example:
45void F(int i, Convertible<int> n) {
46 |
i = n; |
// Error |
47 |
i = (int)n; |
// User-defined explicit conversion |
48 |
n = i; |
// User-defined implicit conversion |
49 |
n = (Convertible<int>)i; |
// User-defined implicit conversion |
50 |
} |
|
403
