Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
C-sharp language specification.2004.pdf
Скачиваний:
14
Добавлен:
23.08.2013
Размер:
2.55 Mб
Скачать

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:

17If the literal has no suffix, it has the first of these types in which its value can be represented: int, uint,

18long, ulong.

19If the literal is suffixed by U or u, it has the first of these types in which its value can be represented:

20uint, ulong.

21If the literal is suffixed by L or l, it has the first of these types in which its value can be represented:

22long, ulong.

23If 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:

30When 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.

34When 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:

10A 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]

12A 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]

14A 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

Соседние файлы в предмете Электротехника