- •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
1try try-block finally finally-block
2• The definite assignment state of v at the beginning of try-block is the same as the definite assignment
3state of v at the beginning of stmt.
4• The definite assignment state of v at the beginning of finally-block is the same as the definite assignment
5state of v at the beginning of stmt.
6• The definite assignment state of v at the end-point of stmt is definitely assigned if (and only if) either:
7o v is definitely assigned at the end-point of try-block
8o v is definitely assigned at the end-point of finally-block
9If a control flow transfer (such as a goto statement) is made that begins within try-block, and ends outside
10of try-block, then v is also considered definitely assigned on that control flow transfer if v is definitely
11assigned at the end-point of finally-block. (This is not an only if—if v is definitely assigned for another
12reason on this control flow transfer, then it is still considered definitely assigned.)
1312.3.3.15 Try-catch-finally statements
14Definite assignment analysis for a try-catch-finally statement of the form:
15try try-block
16catch ( … ) catch-block-1
17…
18catch ( … ) catch-block-n
19finally finally-block
20is done as if the statement were a try-finally statement enclosing a try-catch statement:
21try {
22 |
try try-block |
23 |
catch ( … ) catch-block-1 |
24 |
… |
25catch ( … ) catch-block-n
26}
27finally finally-block
28[Example: The following example demonstrates how the different blocks of a try statement (§15.10) affect
29definite assignment.
30class A
31{
32 |
static void F() { |
33 |
int i, j; |
34 |
try { |
35 |
goto LABEL; |
36 |
// neither i nor j definitely assigned |
37 |
i = 1; |
38 |
// i definitely assigned |
39 |
} |
40 |
catch { |
41 |
// neither i nor j definitely assigned |
42 |
i = 3; |
43 |
// i definitely assigned |
44 |
} |
123
|
C# LANGUAGE SPECIFICATION |
1 |
finally { |
2 |
// neither i nor j definitely assigned |
3 |
j = 5; |
4 |
// j definitely assigned |
5 |
} |
6 |
// i and j definitely assigned |
7 |
LABEL:; |
8 |
// j definitely assigned |
9 |
|
10}
11}
12end example]
1312.3.3.16 Foreach statements
14For a foreach statement stmt of the form:
15foreach ( type identifier in expr ) embedded-statement
16• The definite assignment state of v at the beginning of expr is the same as the state of v at the beginning
17of stmt.
18• The definite assignment state of v on the control flow transfer to embedded-statement or to the end point
19of stmt is the same as the state of v at the end of expr.
2012.3.3.17 Using statements
21For a using statement stmt of the form:
22using ( resource-acquisition ) embedded-statement
23• The definite assignment state of v at the beginning of resource-acquisition is the same as the state of v at
24the beginning of stmt.
25• The definite assignment state of v on the control flow transfer to embedded-statement is the same as the
26state of v at the end of resource-acquisition.
2712.3.3.18 Lock statements
28For a lock statement stmt of the form:
29lock ( expr ) embedded-statement
30• The definite assignment state of v at the beginning of expr is the same as the state of v at the beginning
31of stmt.
32• The definite assignment state of v on the control flow transfer to embedded-statement is the same as the
33state of v at the end of expr.
3412.3.3.19 General rules for simple expressions
35The following rule applies to these kinds of expressions: literals (§14.5.1), simple names (§14.5.2), member
36access expressions (§14.5.4), non-indexed base access expressions (§14.5.8), and typeof expressions
37(§14.5.11).
38• The definite assignment state of v at the end of such an expression is the same as the definite assignment
39state of v at the beginning of the expression.
4012.3.3.20 General rules for expressions with embedded expressions
41The following rules apply to these kinds of expressions: parenthesized expressions (§14.5.3), element access
42expressions (§14.5.6), base access expressions with indexing (§14.5.8), increment and decrement
43expressions (§14.5.9, §14.6.5), cast expressions (§14.6.6), unary +, -, ~, * expressions, binary +, -, *, /, %,
44<<, >>, <, <=, >, >=, ==, !=, is, as, &, |, ^ expressions (§14.7, §14.8, §14.9, §14.10), compound
124
Chapter 12 Variables
1assignment expressions (§14.13.2), checked and unchecked expressions (§14.5.12), array and delegate
2creation expressions (§14.5.10).
3Each of these expressions has one or more sub-expressions that are unconditionally evaluated in a fixed
4order. [Example: The binary % operator evaluates the left hand side of the operator, then the right hand side.
5An indexing operation evaluates the indexed expression, and then evaluates each of the index expressions, in
6order from left to right. end example] For an expression expr, which has sub-expressions expr1, expr2, ...,
7exprn, evaluated in that order:
8• The definite assignment state of v at the beginning of expr1 is the same as the definite assignment state at
9the beginning of expr.
10• The definite assignment state of v at the beginning of expri (i greater than one) is the same as the definite
11assignment state at the end of expri-1.
12• The definite assignment state of v at the end of expr is the same as the definite assignment state at the
13end of exprn.
1412.3.3.21 Invocation expressions and object creation expressions
15For an invocation expression expr of the form:
16primary-expression ( arg1, arg2, … , argn )
17or an object creation expression expr of the form:
18new type ( arg1, arg2, … , argn )
19• For an invocation expression, the definite assignment state of v before primary-expression is the same as
20the state of v before expr.
21• For an invocation expression, the definite assignment state of v before arg1 is the same as the state of v
22after primary-expression.
23• For an object creation expression, the definite assignment state of v before arg1 is the same as the state
24of v before expr.
25• For each argument argi, the definite assignment state of v after argi is determined by the normal
26expression rules, ignoring any ref or out modifiers.
27• For each argument argi for any i greater than one, the definite assignment state of v before argi is the
28same as the state of v after argi-1.
29• If the variable v is passed as an out argument (i.e., an argument of the form “out v”) in any of the
30arguments, then the state of v after expr is definitely assigned. Otherwise, the state of v after expr is the
31same as the state of v after argn.
3212.3.3.22 Simple assignment expressions
33For an expression expr of the form w = expr-rhs:
34• The definite assignment state of v before w is the same as the definite assignment state of v before expr.
35• The definite assignment state of v before expr-rhs is the same as the definite assignment state of v after
36w.
37• If w is the same variable as v, then the definite assignment state of v after expr is definitely assigned.
38Otherwise, the definite assignment state of v after expr is the same as the definite assignment state of v
39after expr-rhs.
40[Example: In the following code
125
C# LANGUAGE SPECIFICATION
1class A
2{
3 |
static void F(int[] arr) { |
4 |
int x; |
5 |
arr[x = 1] = x; // ok |
6}
7}
8the variable x is considered definitely assigned after arr[x = 1] is evaluated as the left hand side of the
9second simple assignment. end example]
1012.3.3.23 && expressions
11For an expression expr of the form expr-first && expr-second:
12• The definite assignment state of v before expr-first is the same as the definite assignment state of v
13before expr.
14• The definite assignment state of v before expr-second is definitely assigned if the state of v after expr-
15first is either definitely assigned or “definitely assigned after true expression”. Otherwise, it is not
16definitely assigned.
17• The definite assignment state of v after expr is determined by:
18o If the state of v after expr-first is definitely assigned, then the state of v after expr is definitely
19assigned.
20o Otherwise, if the state of v after expr-second is definitely assigned, and the state of v after expr-first
21is “definitely assigned after false expression”, then the state of v after expr is definitely assigned.
22o Otherwise, if the state of v after expr-second is definitely assigned or “definitely assigned after true
23expression”, then the state of v after expr is “definitely assigned after true expression”.
24o Otherwise, if the state of v after expr-first is “definitely assigned after false expression”, and the
25state of v after expr-second is “definitely assigned after false expression”, then the state of v after
26expr is “definitely assigned after false expression”.
27o Otherwise, the state of v after expr is not definitely assigned.
28[Example: In the following code
29class A
30{
31 |
static void F(int x, int y) { |
32 |
int i; |
33 |
if (x >= 0 && (i = y) >= 0) { |
34 |
// i definitely assigned |
35 |
} |
36 |
else { |
37 |
// i not definitely assigned |
38 |
} |
39 |
// i not definitely assigned |
40}
41}
42the variable i is considered definitely assigned in one of the embedded statements of an if statement but not
43in the other. In the if statement in method F, the variable i is definitely assigned in the first embedded
44statement because execution of the expression (i = y) always precedes execution of this embedded
45statement. In contrast, the variable i is not definitely assigned in the second embedded statement, since
46x >= 0 might have tested false, resulting in the variable i’s being unassigned. end example]
4712.3.3.24 || expressions
48For an expression expr of the form expr-first || expr-second:
126
