
- •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 12 Variables
112.3.1 Initially assigned variables
2The following categories of variables are classified as initially assigned:
3• Static variables.
4• Instance variables of class instances.
5• Instance variables of initially assigned struct variables.
6• Array elements.
7• Value parameters.
8• Reference parameters.
9• Variables declared by a catch clause, a foreach statement, or a using statement.
1012.3.2 Initially unassigned variables
11The following categories of variables are classified as initially unassigned:
12• Instance variables of initially unassigned struct variables.
13• Output parameters, including the this variable of struct instance constructors without a constructor
14initializer.
15• Local variables, except those declared in a catch clause, a foreach statement, or a using statement.
1612.3.3 Precise rules for determining definite assignment
17In order to determine that each used variable is definitely assigned, the compiler shall use a process that is
18equivalent to the one described in this subclause.
19The compiler processes the body of each function member that has one or more initially unassigned
20variables. For each initially unassigned variable v, the compiler determines a definite assignment state for v
21at each of the following points in the function member:
22• At the beginning of each statement
23• At the end point (§15.1) of each statement
24• On each arc which transfers control to another statement or to the end point of a statement
25• At the beginning of each expression
26• At the end of each expression
27The definite assignment state of v can be either:
28• Definitely assigned. This indicates that on all possible control flows to this point, v has been assigned a
29value.
30• Not definitely assigned. For the state of a variable at the end of an expression of type bool, the state of a
31variable that isn’t definitely assigned might (but doesn’t necessarily) fall into one of the following sub-
32states:
33o Definitely assigned after true expression. This state indicates that v is definitely assigned if the
34boolean expression evaluated as true, but is not necessarily assigned if the boolean expression
35evaluated as false.
36o Definitely assigned after false expression. This state indicates that v is definitely assigned if the
37boolean expression evaluated as false, but is not necessarily assigned if the boolean expression
38evaluated as true.
39The following rules govern how the state of a variable v is determined at each location.
119
C# LANGUAGE SPECIFICATION
112.3.3.1 General rules for statements
2• v is not definitely assigned at the beginning of a function member body.
3• v is definitely assigned at the beginning of any unreachable statement.
4• The definite assignment state of v at the beginning of any other statement is determined by checking the
5definite assignment state of v on all control flow transfers that target the beginning of that statement. If
6(and only if) v is definitely assigned on all such control flow transfers, then v is definitely assigned at the
7beginning of the statement. The set of possible control flow transfers is determined in the same way as
8for checking statement reachability (§15.1).
9• The definite assignment state of v at the end point of a block, checked, unchecked, if, while, do,
10for, foreach, lock, using, or switch statement is determined by checking the definite assignment
11state of v on all control flow transfers that target the end point of that statement. If v is definitely
12assigned on all such control flow transfers, then v is definitely assigned at the end point of the statement.
13Otherwise, v is not definitely assigned at the end point of the statement. The set of possible control flow
14transfers is determined in the same way as for checking statement reachability (§15.1).
1512.3.3.2 Block statements, checked, and unchecked statements
16The definite assignment state of v on the control transfer to the first statement of the statement list in the
17block (or to the end point of the block, if the statement list is empty) is the same as the definite assignment
18statement of v before the block, checked, or unchecked statement.
1912.3.3.3 Expression statements
20For an expression statement stmt that consists of the expression expr:
21• v has the same definite assignment state at the beginning of expr as at the beginning of stmt.
22• If v if definitely assigned at the end of expr, it is definitely assigned at the end point of stmt; otherwise, it
23is not definitely assigned at the end point of stmt.
2412.3.3.4 Declaration statements
25• If stmt is a declaration statement without initializers, then v has the same definite assignment state at the
26end point of stmt as at the beginning of stmt.
27• If stmt is a declaration statement with initializers, then the definite assignment state for v is determined
28as if stmt were a statement list, with one assignment statement for each declaration with an initializer (in
29the order of declaration).
3012.3.3.5 If statements
31For an if statement stmt of the form:
32if ( expr ) then-stmt else else-stmt
33• v has the same definite assignment state at the beginning of expr as at the beginning of stmt.
34• If v is definitely assigned at the end of expr, then it is definitely assigned on the control flow transfer to
35then-stmt and to either else-stmt or to the end-point of stmt if there is no else clause.
36• If v has the state “definitely assigned after true expression” at the end of expr, then it is definitely
37assigned on the control flow transfer to then-stmt, and not definitely assigned on the control flow
38transfer to either else-stmt or to the end-point of stmt if there is no else clause.
39• If v has the state “definitely assigned after false expression” at the end of expr, then it is definitely
40assigned on the control flow transfer to else-stmt, and not definitely assigned on the control flow transfer
41to then-stmt. It is definitely assigned at the end-point of stmt if and only if it is definitely assigned at the
42end-point of then-stmt.
120
Chapter 12 Variables
1• Otherwise, v is considered not definitely assigned on the control flow transfer to either the then-stmt or
2else-stmt, or to the end-point of stmt if there is no else clause.
312.3.3.6 Switch statements
4In a switch statement stmt with a controlling expression expr:
5• The definite assignment state of v at the beginning of expr is the same as the state of v at the beginning
6of stmt.
7• The definite assignment state of v on the control flow transfer to a reachable switch block statement list
8is the same as the definite assignment state of v at the end of expr.
912.3.3.7 While statements
10For a while statement stmt of the form:
11while ( expr ) while-body
12• v has the same definite assignment state at the beginning of expr as at the beginning of stmt.
13• If v is definitely assigned at the end of expr, then it is definitely assigned on the control flow transfer to
14while-body and to the end point of stmt.
15• If v has the state “definitely assigned after true expression” at the end of expr, then it is definitely
16assigned on the control flow transfer to while-body, but not definitely assigned at the end-point of stmt.
17• If v has the state “definitely assigned after false expression” at the end of expr, then it is definitely
18assigned on the control flow transfer to the end point of stmt, but not definitely assigned on the control
19flow transfer to while-body.
2012.3.3.8 Do statements
21For a do statement stmt of the form:
22do do-body while ( expr ) ;
23• v has the same definite assignment state on the control flow transfer from the beginning of stmt to do-
24body as at the beginning of stmt.
25• v has the same definite assignment state at the beginning of expr as at the end point of do-body.
26• If v is definitely assigned at the end of expr, then it is definitely assigned on the control flow transfer to
27the end point of stmt.
28• If v has the state “definitely assigned after false expression” at the end of expr, then it is definitely
29assigned on the control flow transfer to the end point of stmt, but not definitely assigned on the control
30flow transfer to do-body.
3112.3.3.9 For statements
32Definite assignment checking for a for statement of the form:
33 |
for ( for-initializer ; for-condition ; for-iterator ) embedded-statement |
34is done as if the statement were written:
35{
36 |
for-initializer ; |
37 |
while ( for-condition ) { |
38 |
embedded-statement ; |
39 |
LLoop: |
40 |
for-iterator ; |
41}
42}
121
C# LANGUAGE SPECIFICATION
1with continue statements that target the for statement being translated to goto statements targeting the
2label LLoop. If the for-condition is omitted from the for statement, then evaluation of definite assignment
3proceeds as if for-condition were replaced with true in the above expansion.
412.3.3.10 Break, continue, and goto statements
5The definite assignment state of v on the control flow transfer caused by a break, continue, or goto
6statement is the same as the definite assignment state of v at the beginning of the statement.
712.3.3.11 Throw statements
8For a statement stmt of the form
9throw expr ;
10the definite assignment state of v at the beginning of expr is the same as the definite assignment state of v at
11the beginning of stmt.
1212.3.3.12 Return statements
13For a statement stmt of the form
14return expr ;
15• The definite assignment state of v at the beginning of expr is the same as the definite assignment state of
16v at the beginning of stmt.
17• If v is an output parameter, then it shall be definitely assigned either:
18o after expr
19o or at the end of the finally block of a try-finally or try-catch-finally that encloses the
20return statement.
21For a statement stmt of the form:
22return ;
23• If v is an output parameter, then it shall be definitely assigned either:
24o before stmt
25o or at the end of the finally block of a try-finally or try-catch-finally that encloses the
26return statement.
2712.3.3.13 Try-catch statements
28For a statement stmt of the form:
29try try-block
30catch ( … ) catch-block-1
31…
32catch ( … ) catch-block-n
33• The definite assignment state of v at the beginning of try-block is the same as the definite assignment
34state of v at the beginning of stmt.
35• The definite assignment state of v at the beginning of catch-block-i (for any i) is the same as the definite
36assignment state of v at the beginning of stmt.
37• The definite assignment state of v at the end-point of stmt is definitely assigned if (and only if) v is
38definitely assigned at the end-point of try-block and every catch-block-i (for every i from 1 to n).
3912.3.3.14 Try-finally statements
40For a try statement stmt of the form:
122