- •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
1A property access or indexer access is always reclassified as a value by performing an invocation of the get-
2accessor or the set-accessor. The particular accessor is determined by the context of the property or indexer
3access: If the access is the target of an assignment, the set-accessor is invoked to assign a new value
4(§14.13.1). Otherwise, the get-accessor is invoked to obtain the current value (§14.1.1).
514.1.1 Values of expressions
6Most of the constructs that involve an expression ultimately require the expression to denote a value. In such
7cases, if the actual expression denotes a namespace, a type, or nothing, a compile-time error occurs.
8However, if the expression denotes a property access, an indexer access, or a variable, the value of the
9property, indexer, or variable is implicitly substituted:
10• The value of a variable is simply the value currently stored in the storage location identified by the
11variable. A variable shall be considered definitely assigned (§12.3) before its value can be obtained, or
12otherwise a compile-time error occurs.
13• The value of a property access expression is obtained by invoking the get-accessor of the property. If the
14property has no get-accessor, a compile-time error occurs. Otherwise, a function member invocation
15(§14.4.3) is performed, and the result of the invocation becomes the value of the property access
16expression.
17• The value of an indexer access expression is obtained by invoking the get-accessor of the indexer. If the
18indexer has no get-accessor, a compile-time error occurs. Otherwise, a function member invocation
19(§14.4.3) is performed with the argument list associated with the indexer access expression, and the
20result of the invocation becomes the value of the indexer access expression.
2114.2 Operators
22Expressions are constructed from operands and operators. The operators of an expression indicate which
23operations to apply to the operands. [Example: Examples of operators include +, -, *, /, and new. Examples
24of operands include literals, fields, local variables, and expressions. end example]
25There are three kinds of operators:
26• Unary operators. The unary operators take one operand and use either prefix notation (such as –x) or
27postfix notation (such as x++).
28• Binary operators. The binary operators take two operands and all use infix notation (such as x + y).
29• Ternary operator. Only one ternary operator, ?:, exists; it takes three operands and uses infix notation
30(c ? x : y).
31The order of evaluation of operators in an expression is determined by the precedence and associativity of
32the operators (§14.2.1).
33The order in which operands in an expression are evaluated, is left to right. [Example: In
34 F(i) + G(i++) * H(i), method F is called using the old value of i, then method G is called with the old
35value of i, and, finally, method H is called with the new value of i. This is separate from and unrelated to
36operator precedence. end example]
37Certain operators can be overloaded. Operator overloading permits user-defined operator implementations to
38be specified for operations where one or both of the operands are of a user-defined class or struct type
39(§14.2.2).
4014.2.1 Operator precedence and associativity
41When an expression contains multiple operators, the precedence of the operators controls the order in which
42the individual operators are evaluated. [Note: For example, the expression x
43x + (y * z) because the * operator has higher precedence than the binary
+y * z is evaluated as
+operator. end note] The
44precedence of an operator is established by the definition of its associated grammar production. [Note: For
146
|
Chapter 14 Expressions |
1 |
example, an additive-expression consists of a sequence of multiplicative-expressions separated by + or - |
2 |
operators, thus giving the + and - operators lower precedence than the *, /, and % operators. end note] |
3 The following table summarizes all operators in order of precedence from highest to lowest:
4
Subclause |
Category |
Operators |
|
|
|
|
||
|
|
|
||||||
14.5 |
Primary |
x.y f(x) a[x] x++ x-- new |
||||||
|
|
typeof |
checked |
unchecked |
||||
|
|
|
|
|
|
|
|
|
14.6 |
Unary |
+ |
- |
! |
~ ++x |
--x |
(T)x |
|
|
|
|
|
|
|
|
|
|
14.7.1 |
Multiplicative |
* |
/ |
% |
|
|
|
|
|
|
|
|
|
|
|
|
|
14.7 |
Additive |
+ |
- |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14.8 |
Shift |
<< |
>> |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14.9 |
Relational and |
< |
> |
<= |
>= |
is |
as |
|
|
type-testing |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14.9 |
Equality |
== |
!= |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14.10 |
Logical AND |
& |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14.10 |
Logical XOR |
^ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14.10 |
Logical OR |
| |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14.11 |
Conditional |
&& |
|
|
|
|
|
|
|
AND |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14.11 |
Conditional OR |
|| |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14.12 |
Conditional |
?: |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14.13 |
Assignment |
= |
*= |
/= |
%= |
+= |
-= |
<<= >>= &= ^= |= |
|
|
|
|
|
|
|
|
|
5
6When an operand occurs between two operators with the same precedence, the associativity of the operators
7controls the order in which the operations are performed:
8• Except for the assignment operators, all binary operators are left-associative, meaning that operations
9 |
are performed from left to right. [Example: x + y + z is evaluated as (x + y) + z. end example] |
10 |
• The assignment operators and the conditional operator (?:) are right-associative, meaning that |
11 |
operations are performed from right to left. [Example: x = y = z is evaluated as x = (y = z). end |
12 |
example] |
13 |
Precedence and associativity can be controlled using parentheses. [Example: x + y * z first multiplies y |
14 |
by z and then adds the result to x, but (x + y) * z first adds x and y and then multiplies the result by z. |
15end example]
1614.2.2 Operator overloading
17All unary and binary operators have predefined implementations that are automatically available in any
18expression. In addition to the predefined implementations, user-defined implementations can be introduced
19by including operator declarations in classes and structs (§17.9). User-defined operator implementations
20always take precedence over predefined operator implementations: Only when no applicable user-defined
21operator implementations exist will the predefined operator implementations be considered, as described in
22§14.2.3 and §14.2.4.
23The overloadable unary operators are:
24 |
+ - ! ~ ++ -- true false |
147
C# LANGUAGE SPECIFICATION
1[Note: Although true and false are not used explicitly in expressions (and therefore are not included in
2the precedence table in §14.2.1), they are considered operators because they are invoked in several
3expression contexts: boolean expressions (§14.16) and expressions involving the conditional (§14.12), and
4conditional logical operators (§14.11). end note]
5The overloadable binary operators are:
6 |
+ - * / % & | ^ << >> == != > < >= <= |
7Only the operators listed above can be overloaded. In particular, it is not possible to overload member
8access, method invocation, or the =, &&, ||, ?:, checked, unchecked, new, typeof, as, and
9is operators.
10When a binary operator is overloaded, the corresponding assignment operator, if any, is also implicitly
11overloaded. [Example: An overload of operator * is also an overload of operator *=. This is described
12further in §14.13. end example] The assignment operator itself (=) cannot be overloaded. An assignment
13always performs a simple bit-wise copy of a value into a variable.
14Cast operations, such as (T)x, are overloaded by providing user-defined conversion operators (§13.4).
15Element access, such as a[x], is not considered an overloadable operator. Instead, user-defined indexing is
16supported through indexers (§17.8).
17In expressions, operators are referenced using operator notation, and in declarations, operators are referenced
18using functional notation. The following table shows the relationship between operator and functional
19notations for unary and binary operators. In the first entry, op denotes any overloadable unary prefix
20operator. In the second entry, op denotes the unary postfix ++ and -- operators. In the third entry, op
21denotes any overloadable binary operator. [Note: For an example of overloading the ++ and -- operators see
22§17.9.1. end note]
23
Operator notation |
Functional notation |
|
|
op x |
operator op(x) |
|
|
x op |
operator op(x) |
|
|
x op y |
operator op(x, y) |
|
|
24
25User-defined operator declarations always require at least one of the parameters to be of the class or struct
26type that contains the operator declaration. [Note: Thus, it is not possible for a user-defined operator to have
27the same signature as a predefined operator. end note]
28User-defined operator declarations cannot modify the syntax, precedence, or associativity of an operator.
29[Example: The / operator is always a binary operator, always has the precedence level specified in §14.2.1,
30and is always left-associative. end example]
31[Note: While it is possible for a user-defined operator to perform any computation it pleases,
32implementations that produce results other than those that are intuitively expected are strongly discouraged.
33For example, an implementation of operator == should compare the two operands for equality and return
34an appropriate bool result. end note]
35The descriptions of individual operators in §14.5 through §14.13 specify the predefined implementations of
36the operators and any additional rules that apply to each operator. The descriptions make use of the terms
37unary operator overload resolution, binary operator overload resolution, and numeric promotion,
38definitions of which are found in the following subclauses.
3914.2.3 Unary operator overload resolution
40An operation of the form op x or x op, where op is an overloadable unary operator, and x is an expression of
41type X, is processed as follows:
148
