
- •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.1.8 User-defined implicit conversions
2A user-defined implicit conversion consists of an optional standard implicit conversion, followed by
3execution of a user-defined implicit conversion operator, followed by another optional standard implicit
4conversion. The exact rules for evaluating user-defined conversions are described in §13.4.3.
513.2 Explicit conversions
6The following conversions are classified as explicit conversions:
7• All implicit conversions.
8• Explicit numeric conversions.
9• Explicit enumeration conversions.
10• Explicit reference conversions.
11• Explicit interface conversions.
12• Unboxing conversions.
13• Explicit type parameter conversions.
14• User-defined explicit conversions.
15Explicit conversions can occur in cast expressions (§14.6.6).
16The set of explicit conversions includes all implicit conversions. [Note: This means that redundant cast
17expressions are allowed. end note]
18The explicit conversions that are not implicit conversions are conversions that cannot be proven to always
19succeed, conversions that are known to possibly lose information, and conversions across domains of types
20sufficiently different to merit explicit notation.
2113.2.1 Explicit numeric conversions
22The explicit numeric conversions are the conversions from a numeric-type to another numeric-type for
23which an implicit numeric conversion (§13.1.2) does not already exist:
24• From sbyte to byte, ushort, uint, ulong, or char.
25• From byte to sbyte or char.
26• From short to sbyte, byte, ushort, uint, ulong, or char.
27• From ushort to sbyte, byte, short, or char.
28• From int to sbyte, byte, short, ushort, uint, ulong, or char.
29• From uint to sbyte, byte, short, ushort, int, or char.
30• From long to sbyte, byte, short, ushort, int, uint, ulong, or char.
31• From ulong to sbyte, byte, short, ushort, int, uint, long, or char.
32• From char to sbyte, byte, or short.
33• From float to sbyte, byte, short, ushort, int, uint, long, ulong, char, or decimal.
34• From double to sbyte, byte, short, ushort, int, uint, long, ulong, char, float, or decimal.
35• From decimal to sbyte, byte, short, ushort, int, uint, long, ulong, char, float, or double.
36Because the explicit conversions include all implicit and explicit numeric conversions, it is always possible
37to convert from any numeric-type to any other numeric-type using a cast expression (§14.6.6).
134
Chapter 13 Conversions
1The explicit numeric conversions possibly lose information or possibly cause exceptions to be thrown. An
2explicit numeric conversion is processed as follows:
3• For a conversion from an integral type to another integral type, the processing depends on the overflow
4checking context (§14.5.12) in which the conversion takes place:
5o In a checked context, the conversion succeeds if the value of the source operand is within the range
6of the destination type, but throws a System.OverflowException if the value of the source
7operand is outside the range of the destination type.
8o In an unchecked context, the conversion always succeeds, and proceeds as follows.
9• If the source type is larger than the destination type, then the source value is truncated by
10
11
12
13
14
15
16
17
discarding its “extra” most significant bits. The result is then treated as a value of the destination type.
•If the source type is smaller than the destination type, then the source value is either signextended or zero-extended so that it is the same size as the destination type. Sign-extension is used if the source type is signed; zero-extension is used if the source type is unsigned. The result is then treated as a value of the destination type.
•If the source type is the same size as the destination type, then the source value is treated as a value of the destination type
18• For a conversion from decimal to an integral type, the source value is rounded towards zero to the
19nearest integral value, and this integral value becomes the result of the conversion. If the resulting
20integral value is outside the range of the destination type, a System.OverflowException is thrown.
21• For a conversion from float or double to an integral type, the processing depends on the overflow-
22checking context (§14.5.12) in which the conversion takes place:
23o In a checked context, the conversion proceeds as follows:
24• The value is rounded towards zero to the nearest integral value. If this integral value is within
25 |
the range of the destination type, then this value is the result of the conversion. |
26• Otherwise, a System.OverflowException is thrown.
27o In an unchecked context, the conversion always succeeds, and proceeds as follows.
28• The value is rounded towards zero to the nearest integral value. If this integral value is within
29 |
the range of the destination type, then this value is the result of the conversion. |
30• Otherwise, the result of the conversion is an unspecified value of the destination type.
31• For a conversion from double to float, the double value is rounded to the nearest float value. This
32rounding may cause a non-zero value to be rounded to a zero value of the same sign. If the magnitude of
33the double value is too large to represent as a float, the result becomes positive infinity or negative
34infinity. If the double value is NaN, the result is also NaN.
35• For a conversion from float or double to decimal, the source value is converted to decimal
36representation and rounded to the nearest number after the 28th decimal place if required (§11.1.7). This
37rounding may cause a non-zero value to be rounded to zero. If the source value is NaN, infinite, or its
38magnitude is too large to represent as a decimal, a System.OverflowException is thrown.
39• For a conversion from decimal to float or double, the decimal value is rounded to the nearest
40double or float value. However, if the value being converted is not within the range of the destination
41type, a System.OverflowException is thrown.
4213.2.2 Explicit enumeration conversions
43The explicit enumeration conversions are:
135
C# LANGUAGE SPECIFICATION
1• From sbyte, byte, short, ushort, int, uint, long, ulong, char, float, double, or decimal to
2any enum-type.
3• From any enum-type to sbyte, byte, short, ushort, int, uint, long, ulong, char, float,
4double, or decimal.
5• From any enum-type to any other enum-type.
6An explicit enumeration conversion between two types is processed by treating any participating enum-type
7as the underlying type of that enum-type, and then performing an implicit or explicit numeric conversion
8between the resulting types. [Example: Given an enum-type E with and underlying type of int, a conversion
9from E to byte is processed as an explicit numeric conversion (§13.2.1) from int to byte, and a
10conversion from byte to E is processed as an implicit numeric conversion (§13.1.2) from byte to int. end
11example]
1213.2.3 Explicit reference conversions
13The explicit reference conversions are:
14• From object to any reference-type.
15• From any class-type S to any class-type T, provided S is a base class of T.
16• From any class-type S to any interface-type T, provided S is not sealed and provided S does not
17implement T.
18• From any interface-type S to any class-type T, provided T is not sealed or provided T implements S.
19• `From any interface-type S to any interface-type T, provided S is not derived from T.
20• From an array-type S with an element type SE to an array-type T with an element type TE, provided all
21of the following are true:
22o S and T differ only in element type. (In other words, S and T have the same number of dimensions.)
23o An explicit reference conversion exists from SE to TE.
24• From System.Array and the interfaces it implements, to any array-type.
25• From System.Delegate and the interfaces it implements, to any delegate-type.
26• From a one-dimensional array-type S[] to System.Collections.Generic.IList<T> and its base
27interfaces, provided there is an explicit reference conversion from S to T.
28• From System.Collections.Generic.IList<T> and its base interfaces to a one-dimensional
29array-type S[], provided there is an implicit or explicit reference conversion from S[] to
30System.Collections.Generic.IList<T>. This is precisely when either S and T are the same type
31or there is an implicit or explicit reference conversion from S to T.
32For a type-parameter T that is known to be a reference type (§26.7), the following explicit reference
33conversions exist:
34• From the effective base class C of T to T and from any base class of C to T.
35• From any interface-type to T.
36• From T to any interface-type I provided there isn’t already an implicit reference conversion from T to I.
37• From a type-parameter U to T provided that T depends on U (§26.7). [Note: Since T is known to be a
38reference type, within the scope of T, the run-time type of U will always be a reference type, even if U is
39not known to be a reference type at compile-time. end note]
40The explicit reference conversions are those conversions between reference-types that require run-time
41checks to ensure they are correct.
136
Chapter 13 Conversions
1For an explicit reference conversion to succeed at run-time, the value of the source operand shall be null,
2or the actual type of the object referenced by the source operand shall be a type that can be converted to the
3destination type by an implicit reference conversion (§13.1.4). If an explicit reference conversion fails, a
4System.InvalidCastException is thrown.
5Reference conversions, implicit or explicit, never change the referential identity of the object being
6converted. [Note: In other words, while a reference conversion can change the type of the reference, it never
7changes the type or value of the object being referred to. end note]
813.2.4 Unboxing conversions
9An unboxing conversion permits an explicit conversion from type object or System.ValueType to any
10value-type, or from any interface-type to any value-type that implements the interface-type, and from the
11type System.Enum to any enumeration type. An unboxing operation consists of first checking that the
12object instance is a boxed value of the given value-type or enumeration type, and then copying the value out
13of the instance. An enum can be unboxed from the type System.Enum, since that is the direct base class for
14all enum types (§21.4). A struct or enum can be unboxed from the type System.ValueType, since that is
15the direct base class for all structs (§18.3.2) and a base class for all enums.
16For a type-parameter T that is not known to be a reference type (§26.7), the following conversions involving
17T are considered to be unboxing conversions at compile-time. At run-time, if T is a value type, the
18conversion is executed as an unboxing conversion. At run-time, if T is a reference type, the conversion is
19executed as an explicit reference conversion or identity conversion.
20• From the effective base class C of T to T and from any base class of C to T. [Note: C will be one of the
21types System.Object, System.ValueType, or System.Enum (otherwise T would be known to be a
22reference type and §13.2.3 would apply instead of this clause). end note]
23• From any interface-type to T.
2413.2.5 Unboxing conversions are described further in §11.3.2.Explicit type parameter
25conversions
26This clause details explicit conversions involving type parameters that are not classified as explicit reference
27conversions or explicit unboxing conversions.
28For a type-parameter T that is not known to be a reference type (§26.7), the following explicit conversions
29exist:
30• From T to any interface-type I provided there is not already an implicit conversion from T to I. This
31conversion consists of an implicit boxing conversion (§13.1.5) from T to object followed by an
32explicit reference conversion from object to I. At run-time, if T is a value type, the conversion is
33executed as a boxing conversion followed by an explicit reference conversion. At run-time, if T is a
34reference type, the conversion is executed as an explicit reference conversion.
35• From a type parameter U to T provided that T depends on U (§26.7). At run-time, if T is a value type and
36U is a reference type, the conversion is executed as an unboxing conversion. At run-time, if both T and U
37are value types, then T and U are necessarily the same type and no conversion is performed. At run-time,
38if T is a reference type, then U is necessarily also a reference type and the conversion is executed as an
39explicit reference conversion or identity conversion.
4013.2.6 User-defined explicit conversions
41A user-defined explicit conversion consists of an optional standard explicit conversion, followed by
42execution of a user-defined implicit or explicit conversion operator, followed by another optional standard
43explicit conversion. The exact rules for evaluating user-defined conversions are described in §13.4.4.
137