
- •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
1chosen. For instance, if the index expression is of type short then an implicit conversion to int is
2performed, since implicit conversions from short to int and from short to long are possible. If
3evaluation of an index expression or the subsequent implicit conversion causes an exception, then no
4further index expressions are evaluated and no further steps are executed.
5• The value of P is checked to be valid. If the value of P is null, a
6System.NullReferenceException is thrown and no further steps are executed.
7• The value of each expression in the expression-list is checked against the actual bounds of each
8dimension of the array instance referenced by P. If one or more values are out of range, a
9System.IndexOutOfRangeException is thrown and no further steps are executed.
10• The location of the array element given by the index expression(s) is computed, and this location
11becomes the result of the array access.
1214.5.6.2 Indexer access
13For an indexer access, the primary-no-array-creation-expression of the element-access shall be a variable
14or value of a class, struct, or interface type, and this type shall implement one or more indexers that are
15applicable with respect to the expression-list of the element-access.
16The compile-time processing of an indexer access of the form P[A], where P is a primary-no-array-
17creation-expression of a class, struct, or interface type T, and A is an expression-list, consists of the
18following steps:
19• The set of indexers provided by T is constructed. The set consists of all indexers declared in T or a base
20type of T that are not override declarations and are accessible in the current context (§10.5).
21• The set is reduced to those indexers that are applicable and not hidden by other indexers. The following
22rules are applied to each indexer S.I in the set, where S is the type in which the indexer I is declared:
23o If I is not applicable with respect to A (§14.4.2.1), then I is removed from the set.
24o If I is applicable with respect to A (§14.4.2.1), then all indexers declared in a base type of S are
25removed from the set.
26• If the resulting set of candidate indexers is empty, then no applicable indexers exist, and a compile-time
27error occurs. If the candidate indexers are not all declared in the same type, the indexer access is
28ambiguous, and a compile-time error occurs (this latter situation can only occur for an indexer access on
29an instance of an interface that has multiple direct base interfaces, or for an invocation of an indexer on a
30type parameter).
31• The best indexer of the set of candidate indexers is identified using the overload resolution rules of
32§14.4.2. If a single best indexer cannot be identified, the indexer access is ambiguous, and a compile-
33time error occurs.
34• The index expressions of the expression-list are evaluated in order, from left to right. The result of
35processing the indexer access is an expression classified as an indexer access. The indexer access
36expression references the indexer determined in the step above, and has an associated instance
37expression of P and an associated argument list of A.
38Depending on the context in which it is used, an indexer access causes invocation of either the get-accessor
39or the set-accessor of the indexer. If the indexer access is the target of an assignment, the set-accessor is
40invoked to assign a new value (§14.13.1). In all other cases, the get-accessor is invoked to obtain the current
41value (§14.1.1).
4214.5.7 This access
43A this-access consists of the reserved word this.
44this-access:
45 |
this |
168
Chapter 14 Expressions
1A this-access is permitted only in the block of an instance constructor, an instance method, or an instance
2accessor. It has one of the following meanings:
3• When this is used in a primary-expression within an instance constructor of a class, it is classified as a
4value. The type of the value is the class within which the usage occurs, and the value is a reference to the
5object being constructed.
6• When this is used in a primary-expression within an instance method or instance accessor of a class, it
7is classified as a value. The type of the value is the class within which the usage occurs, and the value is
8a reference to the object for which the method or accessor was invoked.
9• When this is used in a primary-expression within an instance constructor of a struct, it is classified as a
10variable. The type of the variable is the struct within which the usage occurs, and the variable represents
11the struct being constructed. The this variable of an instance constructor of a struct behaves exactly the
12same as:
13o an out parameter of the struct type if the constructor declaration has no constructor initializer. In
14particular, this means that the variable shall be definitely assigned in every execution path of the
15instance constructor.
16o a ref parameter of the struct type if the constructor declaration has a constructor initializer. In
17particular, this means that the variable is considered initially assigned.
18• When this is used in a primary-expression within an instance method or instance accessor of a struct, it
19is classified as a variable. The type of the variable is the struct within which the usage occurs, and the
20variable represents the struct for which the method or accessor was invoked. The this variable of an
21instance method of a struct behaves exactly the same as a ref parameter of the struct type.
22Use of this in a primary-expression in a context other than the ones listed above is a compile-time error. In
23particular, it is not possible to refer to this in a static method, a static property accessor, or in a variable-
24initializer of a field declaration.
2514.5.8 Base access
26A base-access consists of the reserved word base followed by either a “.” token and an identifier and
27optional type-argument-list or an expression-list enclosed in square brackets:
28base-access:
29 |
base . |
identifier type-argument-listopt |
30 |
base [ |
expression-list ] |
31A base-access is used to access base class members that are hidden by similarly named members in the
32current class or struct. A base-access is permitted only in the block of an instance constructor, an instance
33method, or an instance accessor. When base.I occurs in a class or struct, I shall denote a member of the
34base class of that class or struct. Likewise, when base[E] occurs in a class, an applicable indexer shall exist
35in the base class.
36At compile-time, base-access expressions of the form base.I and base[E] are evaluated exactly as if they
37were written ((B)this).I and ((B)this)[E], where B is the base class of the class or struct in which
38the construct occurs. Thus, base.I and base[E] correspond to this.I and this[E], except this is
39viewed as an instance of the base class.
40When a base-access references a virtual function member (a method, property, or indexer), the
41determination of which function member to invoke at run-time (§14.4.3) is changed. The function member
42that is invoked is determined by finding the most derived implementation (§17.5.3) of the function member
43with respect to B (instead of with respect to the run-time type of this, as would be usual in a non-base
44access). Thus, within an override of a virtual function member, a base-access can be used to invoke the
45inherited implementation of the function member. If the function member referenced by a base-access is
46abstract, a compile-time error occurs.
169
C# LANGUAGE SPECIFICATION
114.5.9 Postfix increment and decrement operators
2post-increment-expression:
3 |
primary-expression |
++ |
4 |
post-decrement-expression: |
|
5 |
primary-expression |
-- |
6The operand of a postfix increment or decrement operation shall be an expression classified as a variable, a
7property access, or an indexer access. The result of the operation is a value of the same type as the operand.
8If the operand of a postfix increment or decrement operation is a property or indexer access, the property or
9indexer shall have both a get and a set accessor. If this is not the case, a compile-time error occurs.
10Unary operator overload resolution (§14.2.3) is applied to select a specific operator implementation.
11Predefined ++ and -- operators exist for the following operand types: sbyte, byte, short, ushort, int,
12uint, long, ulong, char, float, double, decimal, and any enum type. The result type of each of these
13predefined operators is the same as the operand type. The predefined ++ operators return the value produced
14by adding 1 to the operand, and the predefined -- operators return the value produced by subtracting 1 from
15the operand. In a checked context, if the result of this addition or subtraction is outside the range of the
16result type, a System.OverflowException is thrown.
17There shall be an implicit conversion from the return type of the selected unary operator to the type of the
18primary-expression, otherwise a compile-time error occurs.
19The run-time processing of a postfix increment or decrement operation of the form x++ or x-- consists of
20the following steps:
21• If x is classified as a variable:
22o x is evaluated to produce the variable.
23o The value of x is saved.
24o The saved value of x is converted to the operand type of the selected operator and the operator is
25invoked with this value as its argument.
26o The value returned by the operator is converted to the type of x and stored in the location given by
27the evaluation of x.
28o The saved value of x becomes the result of the operation.
29• If x is classified as a property or indexer access:
30o The instance expression (if x is not static) and the argument list (if x is an indexer access)
31associated with x are evaluated, and the results are used in the subsequent get and set accessor
32invocations.
33o The get accessor of x is invoked and the returned value is saved.
34o The saved value of x is converted to the operand type of the selected operator and the operator is
35invoked with this value as its argument.
36o The value returned by the operator is converted to the type of x and the set accessor of x is invoked
37with this value as its value argument.
38o The saved value of x becomes the result of the operation.
39The ++ and -- operators also support prefix notation (§14.6.5). The result of x++ or x-- is the value of
40x before the operation, whereas the result of ++x or --x is the value of x after the operation. In either
41case, x itself has the same value after the operation.
42An operator ++ or operator -- implementation can be invoked using either postfix or prefix notation.
43It is not possible to have separate operator implementations for the two notations.
170