
- •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
19.2.3 Grammar ambiguities
2The productions for simple-name (§14.5.2) and member-access (§14.5.4) can give rise to ambiguities in the
3grammar for expressions. [Example: The statement:
4F(G<A, B>(7));
5could be interpreted as a call to F with two arguments, G < A and B > (7). Alternatively, it could be
6interpreted as a call to F with one argument, which is a call to a generic method G with two type arguments
7and one regular argument. end example]
8If a sequence of tokens can be parsed (in context) as a simple-name (§14.5.2), member-access (§14.5.4), or
9pointer-member-access (§25.5.2) ending with a type-argument-list (§26.5.1), the token immediately
10following the closing > token is examined. If it is one of
11 ( ) ] : ; , . ? == !=
12then the type-argument-list is retained as part of the simple-name, member-access or pointer-member-access
13and any other possible parse of the sequence of tokens is discarded. Otherwise, the type-argument-list is not
14considered to be part of the simple-name, member-access or pointer-member-access, even if there is no other
15possible parse of the sequence of tokens. [Note: These rules are not applied when parsing a type-argument-
16list in a namespace-or-type-name (§10.8). end note] [Example: The statement:
17F(G<A, B>(7));
18will, according to this rule, be interpreted as a call to F with one argument, which is a call to a generic
19method G with two type arguments and one regular argument. The statements
20F(G<A, B>7);
21F(G<A, B>>7);
22will each be interpreted as a call to F with two arguments. The statement
23x = F<A> + y;
24will be interpreted as a less-than operater, greater-than operator and unary-plus operator, as if the statement
25 had been written x = (F < A) > (+y), instead of as a simple-name with a type-argument-list followed
26by a binary-plus operator. In the statement
27x = y is C<T> + z;
28the tokens C<T> are interpreted as a namespace-or-type-name with a type-argument-list. end example]
299.3 Lexical analysis
30The input production defines the lexical structure of a C# source file. Each source file in a C# program shall
31conform to this lexical grammar production.
32input::
33 |
input-sectionopt |
34 |
input-section:: |
35 |
input-section-part |
36 |
input-section input-section-part |
37 |
input-section-part:: |
38 |
input-elementsopt new-line |
39 |
pp-directive |
40 |
input-elements:: |
41 |
input-element |
42 |
input-elements input-element |
43 |
input-element:: |
44 |
whitespace |
45 |
comment |
46 |
token |
64
Chapter 9 Lexical structure
1Five basic elements make up the lexical structure of a C# source file: Line terminators (§9.3.1), white space
2(§9.3.3), comments (§9.3.2), tokens (§9.4), and pre-processing directives (§9.5). Of these basic elements,
3only tokens are significant in the syntactic grammar of a C# program (§9.2.2), except in the case of a > token
4being combined with another token to form a single operator (§9.4.5).
5The lexical processing of a C# source file consists of reducing the file into a sequence of tokens which
6becomes the input to the syntactic analysis. Line terminators, white space, and comments can serve to
7separate tokens, and pre-processing directives can cause sections of the source file to be skipped, but
8otherwise these lexical elements have no impact on the syntactic structure of a C# program.
9When several lexical grammar productions match a sequence of characters in a source file, the lexical
10processing always forms the longest possible lexical element. [Example: The character sequence // is
11processed as the beginning of a single-line comment because that lexical element is longer than a single /
12token. end example]
139.3.1 Line terminators
14Line terminators divide the characters of a C# source file into lines.
15new-line::
16 |
Carriage return character (U+000D) |
17 |
Line feed character (U+000A) |
18 |
Carriage return character (U+000D) followed by line feed character (U+000A) |
19 |
Next line character (U+2085) |
20 |
Line separator character (U+2028) |
21 |
Paragraph separator character (U+2029) |
22For compatibility with source code editing tools that add end-of-file markers, and to enable a source file to
23be viewed as a sequence of properly terminated lines, the following transformations are applied, in order, to
24every source file in a C# program:
25• If the last character of the source file is a Control-Z character (U+001A), this character is deleted.
26• A carriage-return character (U+000D) is added to the end of the source file if that source file is non-
27empty and if the last character of the source file is not a carriage return (U+000D), a line feed (U+000A),
28a next line character (U+2085), a line separator (U+2028), or a paragraph separator (U+2029). [Note:
29The additional carriage-return allows a program to end in a pp-directive (§9.5) that does not have a
30terminating new-line. end note]
319.3.2 Comments
32Two forms of comments are supported: delimited comments and single-line comments.
33A delimited comment begins with the characters /* and ends with the characters */. Delimited comments
34can occupy a portion of a line, a single line, or multiple lines. [Example: The example
35/* Hello, world program
36 |
This program writes “hello, world” to the console |
37*/
38class Hello
39{
40 |
static void Main() { |
41 |
System.Console.WriteLine("hello, world"); |
42}
43}
44includes a delimited comment. end example]
45A single-line comment begins with the characters // and extends to the end of the line. [Example: The
46example
65
C# LANGUAGE SPECIFICATION
1// Hello, world program
2// This program writes “hello, world” to the console
3//
4class Hello // any name will do for this class
5{
6 |
static void Main() { // this method |
must be named "Main" |
7 |
System.Console.WriteLine("hello, |
world"); |
8}
9}
10shows several single-line comments. end example]
11comment::
12 |
single-line-comment |
13 |
delimited-comment |
14 single-line-comment::
15// input-charactersopt
16input-characters::
17 |
input-character |
|
18 |
input-characters input-character |
|
19 |
input-character:: |
|
20 |
Any Unicode character except a new-line-character |
|
21 |
new-line-character:: |
|
22 |
Carriage return character (U+000D) |
|
23 |
Line feed character (U+000A) |
|
24 |
Next line character (U+0085) |
|
25 |
Line separator character (U+2028) |
|
26 |
Paragraph separator character (U+2029) |
|
27 |
delimited-comment:: |
|
28 |
/* delimited-comment-textopt asterisks / |
|
29 |
delimited-comment-text:: |
|
30 |
delimited-comment-section |
|
31 |
delimited-comment-text delimited-comment-section |
|
32 |
delimited-comment-section:: |
|
33 |
not-asterisk |
|
34 |
asterisks |
not-slash |
35 |
asterisks:: |
|
36 |
* |
|
37 |
asterisks |
* |
38 |
not-asterisk:: |
|
39 |
Any Unicode character except * |
|
40 |
not-slash:: |
|
41 |
Any Unicode character except / |
42Comments do not nest. The character sequences /* and */ have no special meaning within a single-line
43comment, and the character sequences // and /* have no special meaning within a delimited comment.
44Comments are not processed within character and string literals.
459.3.3 White space
46White space is defined as any character with Unicode class Zs (which includes the space character) as well
47as the horizontal tab character, the vertical tab character, and the form feed character.
66