- •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
C# LANGUAGE SPECIFICATION
113.3 Standard conversions
2The standard conversions are those pre-defined conversions that can occur as part of a user-defined
3conversion.
413.3.1 Standard implicit conversions
5The following implicit conversions are classified as standard implicit conversions:
6• Identity conversions (§13.1.1)
7• Implicit numeric conversions (§13.1.2)
8• Implicit reference conversions (§13.1.4)
9• Boxing conversions (§13.1.5)
10• Implicit type parameter conversions (§13.1.6)
11• Implicit constant expression conversions (§13.1.7)
12The standard implicit conversions specifically exclude user-defined implicit conversions.
1313.3.2 Standard explicit conversions
14The standard explicit conversions are all standard implicit conversions plus the subset of the explicit
15conversions for which an opposite standard implicit conversion exists. [Note: In other words, if a standard
16implicit conversion exists from a type A to a type B, then a standard explicit conversion exists from type A to
17type B and from type B to type A. end note]
1813.4 User-defined conversions
19C# allows the pre-defined implicit and explicit conversions to be augmented by user-defined conversions.
20User-defined conversions are introduced by declaring conversion operators (§17.9.3) in class and struct
21types.
2213.4.1 Permitted user-defined conversions
23C# permits only certain user-defined conversions to be declared. In particular, it is not possible to redefine
24an already existing implicit or explicit conversion. A class or struct is permitted to declare a conversion
25operator from a source type S to a target type T only if all of the following are true:
26• S and T are different types.
27• Either S or T is the class or struct type in which the operator declaration takes place.
28• Neither S nor T is object or an interface-type.
29• T is not a base class of S, and S is not a base class of T.
30The restrictions that apply to user-defined conversions are discussed further in §17.9.3.
3113.4.2 Evaluation of user-defined conversions
32A user-defined conversion converts a value from its type, called the source type, to another type, called the
33target type. Evaluation of a user-defined conversion centers on finding the most specific user-defined
34conversion operator for the particular source and target types. This determination is broken into several
35steps:
36• Finding the set of classes and structs from which user-defined conversion operators will be considered.
37This set consists of the source type and its base classes and the target type and its base classes (with the
38implicit assumptions that only classes and structs can declare user-defined operators, and that non-class
39types have no base classes).
138
Chapter 13 Conversions
1• From that set of types, determining which user-defined conversion operators are applicable. For a
2conversion operator to be applicable, it shall be possible to perform a standard conversion (§13.3) from
3the source type to the operand type of the operator, and it shall be possible to perform a standard
4conversion from the result type of the operator to the target type. If the set of applicable user-defined
5conversion operators is empty then there is no user-defined conversion from the source type to the target
6type.
7• From the set of applicable user-defined operators, determining which operator is unambiguously the
8most specific. In general terms, the most specific operator is the operator whose operand type is
9“closest” to the source type and whose result type is “closest” to the target type. The exact rules for
10establishing the most specific user-defined conversion operator are defined in the following subclauses.
11For the purposes of overload resolution, a user-defined conversion from the source type to the target type
12exists if and only if the set of applicable user-defined conversion operators is non-empty. If the set of
13applicable operators is non-empty but does not contain a unique most specific operator, the user-defined
14conversion is deemed to exist even though application of the conversion will always produce a compile-time
15error.
16Once a most specific user-defined conversion operator has been identified, the actual execution of the user-
17defined conversion involves up to three steps:
18• First, if required, performing a standard conversion from the source type to the operand type of the user-
19defined conversion operator.
20• Next, invoking the user-defined conversion operator to perform the conversion.
21• Finally, if required, performing a standard conversion from the result type of the user-defined
22conversion operator to the target type.
23Evaluation of a user-defined conversion never involves more than one user-defined conversion operator. In
24other words, a conversion from type S to type T will never first execute a user-defined conversion from S to
25X and then execute a user-defined conversion from X to T.
26Exact definitions of evaluation of user-defined implicit or explicit conversions are given in the following
27subclauses. The definitions make use of the following terms:
28• If a standard implicit conversion (§13.3.1) exists from a type A to a type B, and if neither A nor B are
29interface-types, then A is said to be encompassed by B, and B is said to encompass A.
30• The most encompassing type in a set of types is the one type that encompasses all other types in the set.
31If no single type encompasses all other types, then the set has no most encompassing type. In more
32intuitive terms, the most encompassing type is the “largest” type in the set—the one type to which each
33of the other types can be implicitly converted.
34• The most encompassed type in a set of types is the one type that is encompassed by all other types in the
35set. If no single type is encompassed by all other types, then the set has no most encompassed type. In
36more intuitive terms, the most encompassed type is the “smallest” type in the set—the one type that can
37be implicitly converted to each of the other types.
3813.4.3 User-defined implicit conversions
39Processing of a user-defined implicit conversion from type S to type T takes one of two forms:
40• Testing whether a user defined conversion exists from S to T. This is used by overload resolution to
41determine whether a function member is applicable.
42• Applying the user defined conversion from S to T. This requires that the existence of the user defined
43conversion has been verified.
44To test whether a user-defined implicit conversion from S to T exists:
139
C# LANGUAGE SPECIFICATION
1• Find the set of types, D, from which user-defined conversion operators will be considered. This set
2consists of S (if S is a class or struct), the base classes of S (if S is a class), T (if T is a class or struct),
3and the base classes of T (if T is a class).
4• Find the set of applicable user-defined conversion operators, U. This set consists of the user-defined
5implicit conversion operators declared by the classes or structs in D that convert from a type
6encompassing S to a type encompassed by T.
7The user-defined conversion from S to T exists if and only if U is not empty.
8To apply the user-defined conversion from S to T:
9• Construct the set of applicalbe user-defined conversion operators U as above.
10• Find the most specific source type, SX, of the operators in U:
11o If any of the operators in U convert from S, then SX is S.
12o Otherwise, SX is the most encompassed type in the combined set of source types of the operators
13in U. If no most encompassed type can be found, then the conversion is ambiguous and a compile-
14time error occurs.
15• Find the most specific target type, TX, of the operators in U:
16o If any of the operators in U convert to T, then TX is T.
17o Otherwise, TX is the most encompassing type in the combined set of target types of the operators
18in U. If no most encompassing type can be found, then the conversion is ambiguous and a compile-
19time error occurs.
20• If U contains exactly one user-defined conversion operator that converts from SX to TX, then this is the
21most specific conversion operator. If no such operator exists, or if more than one such operator exists,
22then the conversion is ambiguous and a compile-time error occurs. Otherwise, the user-defined
23conversion is applied:
24o If S is not SX, then a standard implicit conversion from S to SX is performed.
25o The most specific user-defined conversion operator is invoked to convert from SX to TX.
26o If TX is not T, then a standard implicit conversion from TX to T is performed.
2713.4.4 User-defined explicit conversions
28A user-defined explicit conversion from type S to type T is processed as follows:
29• Find the set of types, D, from which user-defined conversion operators will be considered. This set
30consists of S (if S is a class or struct), the base classes of S (if S is a class), T (if T is a class or struct),
31and the base classes of T (if T is a class).
32• Find the set of applicable user-defined conversion operators, U. This set consists of the user-defined
33implicit or explicit conversion operators declared by the classes or structs in D that convert from a type
34encompassing or encompassed by S to a type encompassing or encompassed by T. If U is empty, the
35conversion is undefined and a compile-time error occurs.
36• Find the most specific source type, SX, of the operators in U:
37o If any of the operators in U convert from S, then SX is S.
38o Otherwise, if any of the operators in U convert from types that encompass S, then SX is the most
39encompassed type in the combined set of source types of those operators. If no most encompassed
40type can be found, then the conversion is ambiguous and a compile-time error occurs.
41o Otherwise, SX is the most encompassing type in the combined set of source types of the operators
42in U. If no most encompassing type can be found, then the conversion is ambiguous and a compile-
43time error occurs.
140
