- •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
1 |
keyword:: one of |
|
|
|
|
2 |
abstract |
as |
base |
bool |
break |
3 |
byte |
case |
catch |
char |
checked |
4 |
class |
const |
continue |
decimal |
default |
5 |
delegate |
do |
double |
else |
enum |
6 |
event |
explicit |
extern |
false |
finally |
7 |
fixed |
float |
for |
foreach |
goto |
8 |
if |
implicit |
in |
int |
interface |
9 |
internal |
is |
lock |
long |
namespace |
10 |
new |
null |
object |
operator |
out |
11 |
override |
params |
private |
protected |
public |
12 |
readonly |
ref |
return |
sbyte |
sealed |
13 |
short |
sizeof |
stackalloc |
static |
string |
14 |
struct |
switch |
this |
throw |
true |
15 |
try |
typeof |
uint |
ulong |
unchecked |
16 |
unsafe |
ushort |
using |
virtual |
void |
17 |
volatile |
while |
|
|
|
18The following identifiers have special meaning in the syntactic grammar, but they are not keywords: add
19(§17.7), get (§17.6.2), global (§16.7), partial (§17.1.4), remove (§17.7), set (§17.6.2), value
20(§17.6.2, §17.7.2), where (§26.7), yield (§15.14), and alias (§16.3). For convenience and clarity, these
21identifiers appear as terminals in the syntactic grammar; however, they are identifiers. [Note: As a result,
22unlike keywords, these identifiers can be written with a @ prefix and can contain unicode-escape-sequences.
23end note]
249.4.4 Literals
25A literal is a source code representation of a value.
26literal::
27
28
29
30
31
32
boolean-literal integer-literal real-literal character-literal string-literal null-literal
339.4.4.1 Boolean literals
34There are two boolean literal values: true and false.
35boolean-literal::
36 |
true |
37false
38The type of a boolean-literal is bool.
399.4.4.2 Integer literals
40Integer literals are used to write values of types int, uint, long, and ulong. Integer literals have two
41possible forms: decimal and hexadecimal.
42integer-literal::
43 |
decimal-integer-literal |
44 |
hexadecimal-integer-literal |
45 |
decimal-integer-literal:: |
46 |
decimal-digits integer-type-suffixopt |
70
Chapter 9 Lexical structure
1 |
decimal-digits:: |
|
|
|
|
|
|
|
||
2 |
decimal-digit |
|
|
|
|
|
|
|||
3 |
decimal-digits decimal-digit |
|
|
|||||||
4 |
decimal-digit:: one of |
|
|
|
|
|
||||
5 |
0 |
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
6 |
integer-type-suffix:: |
one of |
|
|
|
|
||||
7 |
U |
u |
L |
l |
UL Ul uL ul LU Lu lU lu |
|||||
8 |
hexadecimal-integer-literal:: |
|
|
|
||||||
9 |
0x |
hex-digits |
integer-type-suffixopt |
|
||||||
100X hex-digits integer-type-suffixopt
11hex-digits::
12 |
hex-digit |
13 |
hex-digits hex-digit |
14 |
hex-digit:: one of |
15 |
0 1 2 3 4 5 6 7 8 9 A B C D E F a b c d e f |
16The type of an integer literal is determined as follows:
17• If the literal has no suffix, it has the first of these types in which its value can be represented: int, uint,
18long, ulong.
19• If the literal is suffixed by U or u, it has the first of these types in which its value can be represented:
20uint, ulong.
21• If the literal is suffixed by L or l, it has the first of these types in which its value can be represented:
22long, ulong.
23• If the literal is suffixed by UL, Ul, uL, ul, LU, Lu, lU, or lu, it is of type ulong.
24If the value represented by an integer literal is outside the range of the ulong type, a compile-time error
25occurs.
26[Note: As a matter of style, it is suggested that “L” be used instead of “l” when writing literals of type long,
27since it is easy to confuse the letter “l” with the digit “1”. end note]
28To permit the smallest possible int and long values to be written as decimal integer literals, the following
29two rules exist:
30• When a decimal-integer-literal with the value 2147483648 (231) and no integer-type-suffix appears as
31the token immediately following a unary minus operator token (§14.6.2), the result (of both tokens) is a
32constant of type int with the value −2147483648 (−231). In all other situations, such a decimal-integer-
33literal is of type uint.
34• When a decimal-integer-literal with the value 9223372036854775808 (263) and no integer-type-suffix or
35the integer-type-suffix L or l appears as the token immediately following a unary minus operator token
36(§14.6.2), the result (of both tokens) is a constant of type long with the value −9223372036854775808
37(−263). In all other situations, such a decimal-integer-literal is of type ulong.
389.4.4.3 Real literals
39Real literals are used to write values of types float, double, and decimal.
40real-literal::
41 |
decimal-digits |
. decimal-digits exponent-partopt real-type-suffixopt |
42 |
. decimal-digits exponent-partopt real-type-suffixopt |
|
43 |
decimal-digits exponent-part real-type-suffixopt |
|
44 |
decimal-digits |
real-type-suffix |
71
C# LANGUAGE SPECIFICATION
1 |
exponent-part:: |
|
2 |
e |
signopt decimal-digits |
3 |
E signopt decimal-digits |
|
4 |
sign:: one of |
|
5 |
+ |
- |
6 |
real-type-suffix:: one of |
|
7 |
F |
f D d M m |
8If no real-type-suffix is specified, the type of the real literal is double. Otherwise, the real-type-suffix
9determines the type of the real literal, as follows:
10• A real literal suffixed by F or f is of type float. [Example: The literals 1f, 1.5f, 1e10f, and
11123.456F are all of type float. end example]
12• A real literal suffixed by D or d is of type double. [Example: The literals 1d, 1.5d, 1e10d, and
13123.456D are all of type double. end example]
14• A real literal suffixed by M or m is of type decimal. [Example: The literals 1m, 1.5m, 1e10m, and
15123.456M are all of type decimal. end example] This literal is converted to a decimal value by
16taking the exact value, and, if necessary, rounding to the nearest representable value using banker's
17rounding (§11.1.7). Any scale apparent in the literal is preserved unless the value is rounded. [Note:
18Hence, the literal 2.900m will be parsed to form the decimal with sign 0, coefficient 2900, and scale 3.
19end note]
20If the specified literal is too large to be represented in the indicated type, a compile-time error occurs. [Note:
21In particular, a real-literal will never produce a floating-point infinity. A non-zero real-literal may,
22however, be rounded to zero. end note]
23The value of a real literal having type float or double is determined by using the IEC 60559 “round to
24nearest” mode.
259.4.4.4 Character literals
26A character literal represents a single character, and usually consists of a character in quotes, as in 'a'.
27character-literal::
28' character '
29character::
30 |
single-character |
31 |
simple-escape-sequence |
32 |
hexadecimal-escape-sequence |
33 |
unicode-escape-sequence |
34 |
single-character:: |
35 |
Any character except ' (U+0027), \ (U+005C), and new-line-character |
36 |
simple-escape-sequence:: one of |
37 |
\' \" \\ \0 \a \b \f \n \r \t \v |
38 |
hexadecimal-escape-sequence:: |
39 |
\x hex-digit hex-digitopt hex-digitopt hex-digitopt |
40[Note: A character that follows a backslash character (\) in a character shall be one of the following
41characters: ', ", \, 0, a, b, f, n, r, t, u, U, x, v. Otherwise, a compile-time error occurs. end note]
42A hexadecimal escape sequence represents a single Unicode character, with the value formed by the
43hexadecimal number following “\x”.
44If the value represented by a character literal is greater than U+FFFF, a compile-time error occurs.
45A Unicode character escape sequence (§9.4.1) in a character literal shall be in the range U+0000 to U+FFFF.
72
Chapter 9 Lexical structure
1 A simple escape sequence represents a Unicode character encoding, as described in the table below.
2
Escape |
Character |
Unicode code |
sequence |
name |
point |
|
|
|
\' |
Single quote |
0x0027 |
|
|
|
\" |
Double quote |
0x0022 |
|
|
|
\\ |
Backslash |
0x005C |
|
|
|
\0 |
Null |
0x0000 |
|
|
|
\a |
Alert |
0x0007 |
|
|
|
\b |
Backspace |
0x0008 |
|
|
|
\f |
Form feed |
0x000C |
|
|
|
\n |
New line |
0x000A |
|
|
|
\r |
Carriage return |
0x000D |
|
|
|
\t |
Horizontal tab |
0x0009 |
|
|
|
\v |
Vertical tab |
0x000B |
|
|
|
3
4The type of a character-literal is char.
59.4.4.5 String literals
6C# supports two forms of string literals: regular string literals and verbatim string literals. A regular string
7literal consists of zero or more characters enclosed in double quotes, as in "hello, world", and can
8include both simple escape sequences (such as \t for the tab character), and hexadecimal and Unicode
9escape sequences.
10A verbatim string literal consists of an @ character followed by a double-quote character, zero or more
11characters, and a closing double-quote character. [Example: A simple example is @"hello, world". end
12example] In a verbatim string literal, the characters between the delimiters are interpreted verbatim, with the
13only exception being a quote-escape-sequence. In particular, simple escape sequences, and hexadecimal and
14Unicode escape sequences are not processed in verbatim string literals. A verbatim string literal can span
15multiple lines.
16string-literal::
17 |
regular-string-literal |
18 |
verbatim-string-literal |
19 regular-string-literal::
20" regular-string-literal-charactersopt "
21regular-string-literal-characters::
22 |
regular-string-literal-character |
23 |
regular-string-literal-characters regular-string-literal-character |
24 |
regular-string-literal-character:: |
25 |
single-regular-string-literal-character |
26 |
simple-escape-sequence |
27 |
hexadecimal-escape-sequence |
28 |
unicode-escape-sequence |
29 |
single-regular-string-literal-character:: |
30 |
Any character except " (U+0022), \ (U+005C), and new-line-character |
73
