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

Ecma TC39-TG2/2004/14

C#

Language Specification

Working Draft 2.7, Jun, 2004

Public Review Document

Table of Contents

1

2

3

Table of Contents

4

Foreword......................................................................................................................................................

xvii

5

Introduction..................................................................................................................................................

xix

6

1.

Scope.............................................................................................................................................................

1

7

2. Conformance ...............................................................................................................................................

3

8

3.

Normative references ..................................................................................................................................

5

9

4.

Definitions ....................................................................................................................................................

7

10

5.

Notational conventions................................................................................................................................

9

11

6.

Acronyms and abbreviations ...................................................................................................................

11

12

7.

General description...................................................................................................................................

13

13

8.

Language overview....................................................................................................................................

15

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

8.1

Getting started .......................................................................................................................................

15

8.2

Types .....................................................................................................................................................

16

8.2.1 Predefined types.............................................................................................................................

17

8.2.2 Conversions....................................................................................................................................

19

8.2.3 Array types.....................................................................................................................................

20

8.2.4 Type system unification .................................................................................................................

22

8.3

Variables and parameters ......................................................................................................................

22

8.4

Automatic memory management ..........................................................................................................

25

8.5

Expressions ...........................................................................................................................................

27

8.6

Statements .............................................................................................................................................

28

8.7

Classes...................................................................................................................................................

31

8.7.1 Constants........................................................................................................................................

33

8.7.2 Fields..............................................................................................................................................

33

8.7.3 Methods..........................................................................................................................................

34

8.7.4 Properties .......................................................................................................................................

35

8.7.5 Events.............................................................................................................................................

36

8.7.6 Operators........................................................................................................................................

37

8.7.7 Indexers..........................................................................................................................................

38

8.7.8 Instance constructors......................................................................................................................

39

8.7.9 Destructors .....................................................................................................................................

40

8.7.10 Static constructors ........................................................................................................................

40

8.7.11 Inheritance....................................................................................................................................

41

8.7.12 Static classes ................................................................................................................................

42

8.7.13 Partial type declarations ...............................................................................................................

42

8.8

Structs....................................................................................................................................................

43

8.9

Interfaces ...............................................................................................................................................

44

8.10

Delegates .............................................................................................................................................

45

8.11

Enums..................................................................................................................................................

46

8.12

Namespaces and assemblies................................................................................................................

46

8.13

Versioning ...........................................................................................................................................

48

8.14

Extern Aliases .....................................................................................................................................

49

8.15

Attributes.............................................................................................................................................

51

8.16

Generics...............................................................................................................................................

52

iii

C# LANGUAGE SPECIFICATION

1

2

3

4

5

6

7

8.16.1 Why generics?..............................................................................................................................

52

8.16.2 Creating and consuming generics ................................................................................................

53

8.16.3 Multiple type parameters..............................................................................................................

54

8.16.4 Constraints ...................................................................................................................................

54

8.16.5 Generic methods ..........................................................................................................................

56

8.17

Anonymous methods...........................................................................................................................

56

8.18

Iterators ...............................................................................................................................................

59

8

9. Lexical structure........................................................................................................................................

63

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

9.1 Programs ...............................................................................................................................................

63

9.2 Grammars..............................................................................................................................................

63

9.2.1 Lexical grammar ............................................................................................................................

63

9.2.2 Syntactic grammar .........................................................................................................................

63

9.2.3 Grammar ambiguities.....................................................................................................................

64

9.3 Lexical analysis.....................................................................................................................................

64

9.3.1 Line terminators .............................................................................................................................

65

9.3.2 Comments ......................................................................................................................................

65

9.3.3 White space ....................................................................................................................................

66

9.4 Tokens ...................................................................................................................................................

67

9.4.1 Unicode escape sequences .............................................................................................................

67

9.4.2 Identifiers .......................................................................................................................................

68

9.4.3 Keywords .......................................................................................................................................

69

9.4.4 Literals ...........................................................................................................................................

70

9.4.4.1 Boolean literals........................................................................................................................

70

9.4.4.2 Integer literals..........................................................................................................................

70

9.4.4.3 Real literals..............................................................................................................................

71

9.4.4.4 Character literals .....................................................................................................................

72

9.4.4.5 String literals ...........................................................................................................................

73

9.4.4.6 The null literal .........................................................................................................................

74

9.4.5 Operators and punctuators..............................................................................................................

75

9.5 Pre-processing directives.......................................................................................................................

75

9.5.1 Conditional compilation symbols ..................................................................................................

76

9.5.2 Pre-processing expressions ............................................................................................................

76

9.5.3 Declaration directives.....................................................................................................................

77

9.5.4 Conditional compilation directives ................................................................................................

78

9.5.5 Diagnostic directives......................................................................................................................

80

9.5.6 Region control................................................................................................................................

81

9.5.7 Line directives................................................................................................................................

81

9.5.8 Pragma directives...........................................................................................................................

82

39

10. Basic concepts ..........................................................................................................................................

83

40

41

42

43

44

45

46

47

48

49

50

51

52

53

10.1

Application startup ..............................................................................................................................

83

10.2

Application termination.......................................................................................................................

84

10.3

Declarations.........................................................................................................................................

84

10.4

Members..............................................................................................................................................

87

10.4.1 Namespace members....................................................................................................................

87

10.4.2 Struct members ............................................................................................................................

87

10.4.3 Enumeration members .................................................................................................................

87

10.4.4 Class members .............................................................................................................................

87

10.4.5 Interface members........................................................................................................................

88

10.4.6 Array members.............................................................................................................................

88

10.4.7 Delegate members........................................................................................................................

88

10.5

Member access ....................................................................................................................................

88

10.5.1 Declared accessibility...................................................................................................................

88

10.5.2 Accessibility domains ..................................................................................................................

89

iv

Table of Contents

1

2

3

4

5

6

7

8

9

10

11

12

10.5.3 Protected access for instance members

........................................................................................ 91

10.5.4 Accessibility constraints...............................................................................................................

92

10.6

Signatures and overloading .................................................................................................................

93

10.7

Scopes .................................................................................................................................................

94

10.7.1 Name hiding.................................................................................................................................

96

 

10.7.1.1 Hiding through nesting..........................................................................................................

96

 

10.7.1.2 Hiding through inheritance....................................................................................................

97

10.8

Namespace and type names.................................................................................................................

98

10.8.1 Unqualified name.......................................................................................................................

100

10.8.2 Fully qualified names.................................................................................................................

100

10.9

Automatic memory management ......................................................................................................

101

10.10 Execution order ...............................................................................................................................

103

13

11. Types ......................................................................................................................................................

105

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

11.1 Value types........................................................................................................................................

105

11.1.1 The System.ValueType type ......................................................................................................

106

11.1.2 Default constructors ...................................................................................................................

106

11.1.3 Struct types.................................................................................................................................

107

11.1.4 Simple types...............................................................................................................................

107

11.1.5 Integral types..............................................................................................................................

108

11.1.6 Floating point types....................................................................................................................

109

11.1.7 The decimal type........................................................................................................................

109

11.1.8 The bool type .............................................................................................................................

110

11.1.9 Enumeration types......................................................................................................................

110

11.2 Reference types .................................................................................................................................

110

11.2.1 Class types..................................................................................................................................

111

11.2.2 The object type...........................................................................................................................

111

11.2.3 The string type ...........................................................................................................................

111

11.2.4 Interface types ............................................................................................................................

111

11.2.5 Array types.................................................................................................................................

111

11.2.6 Delegate types ............................................................................................................................

112

11.3 Boxing and unboxing ........................................................................................................................

112

11.3.1 Boxing conversions....................................................................................................................

112

11.3.2 Unboxing conversions................................................................................................................

113

34

12. Variables ................................................................................................................................................

115

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

12.1 Variable categories............................................................................................................................

115

12.1.1 Static variables ...........................................................................................................................

115

12.1.2 Instance variables.......................................................................................................................

115

12.1.2.1 Instance variables in classes................................................................................................

115

12.1.2.2 Instance variables in structs.................................................................................................

116

12.1.3 Array elements ...........................................................................................................................

116

12.1.4 Value parameters........................................................................................................................

116

12.1.5 Reference parameters.................................................................................................................

116

12.1.6 Output parameters ......................................................................................................................

116

12.1.7 Local variables ...........................................................................................................................

117

12.2 Default values....................................................................................................................................

117

12.3 Definite assignment...........................................................................................................................

118

12.3.1 Initially assigned variables.........................................................................................................

119

12.3.2 Initially unassigned variables.....................................................................................................

119

12.3.3 Precise rules for determining definite assignment .....................................................................

119

12.3.3.1 General rules for statements................................................................................................

120

12.3.3.2 Block statements, checked, and unchecked statements.......................................................

120

12.3.3.3 Expression statements .........................................................................................................

120

12.3.3.4 Declaration statements ........................................................................................................

120

v

C# LANGUAGE SPECIFICATION

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

12.3.3.5 If statements ........................................................................................................................

120

12.3.3.6 Switch statements................................................................................................................

121

12.3.3.7 While statements .................................................................................................................

121

12.3.3.8 Do statements ......................................................................................................................

121

12.3.3.9 For statements .....................................................................................................................

121

12.3.3.10 Break, continue, and goto statements................................................................................

122

12.3.3.11 Throw statements ..............................................................................................................

122

12.3.3.12 Return statements ..............................................................................................................

122

12.3.3.13 Try-catch statements .........................................................................................................

122

12.3.3.14 Try-finally statements .......................................................................................................

122

12.3.3.15 Try-catch-finally statements..............................................................................................

123

12.3.3.16 Foreach statements ............................................................................................................

124

12.3.3.17 Using statements ...............................................................................................................

124

12.3.3.18 Lock statements.................................................................................................................

124

12.3.3.19 General rules for simple expressions ................................................................................

124

12.3.3.20 General rules for expressions with embedded expressions ...............................................

124

12.3.3.21 Invocation expressions and object creation expressions ...................................................

125

12.3.3.22 Simple assignment expressions.........................................................................................

125

12.3.3.23 && expressions.................................................................................................................

126

12.3.3.24 || expressions......................................................................................................................

126

12.3.3.25 ! expressions......................................................................................................................

127

12.3.3.26 ?: expressions ....................................................................................................................

128

12.3.3.27 Anonymous method expressions.......................................................................................

128

12.3.3.28 Yield statements ................................................................................................................

129

12.4 Variable references............................................................................................................................

129

12.5 Atomicity of variable references .......................................................................................................

129

27

13. Conversions............................................................................................................................................

131

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

13.1

Implicit conversions ..........................................................................................................................

131

13.1.1 Identity conversion.....................................................................................................................

131

13.1.2 Implicit numeric conversions.....................................................................................................

131

13.1.3 Implicit enumeration conversions ..............................................................................................

132

13.1.4 Implicit reference conversions ...................................................................................................

132

13.1.5 Boxing conversions....................................................................................................................

133

13.1.6 Implicit type parameter conversions ..........................................................................................

133

13.1.7 Implicit constant expression conversions...................................................................................

133

13.1.8 User-defined implicit conversions .............................................................................................

134

13.2

Explicit conversions ..........................................................................................................................

134

13.2.1 Explicit numeric conversions.....................................................................................................

134

13.2.2 Explicit enumeration conversions ..............................................................................................

135

13.2.3 Explicit reference conversions ...................................................................................................

136

13.2.4 Unboxing conversions................................................................................................................

137

13.2.5 Unboxing conversions are described further in §11.3.2.Explicit type parameter conversions ..

137

13.2.6 User-defined explicit conversions..............................................................................................

137

13.3

Standard conversions.........................................................................................................................

138

13.3.1 Standard implicit conversions ....................................................................................................

138

13.3.2 Standard explicit conversions ....................................................................................................

138

13.4

User-defined conversions..................................................................................................................

138

13.4.1 Permitted user-defined conversions ...........................................................................................

138

13.4.2 Evaluation of user-defined conversions .....................................................................................

138

13.4.3 User-defined implicit conversions .............................................................................................

139

13.4.4 User-defined explicit conversions..............................................................................................

140

13.5

Anonymous method conversions ......................................................................................................

141

13.6

Method group conversions ................................................................................................................

142

vi

Table of Contents

1

14. Expressions ............................................................................................................................................

145

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

14.1 Expression classifications .................................................................................................................

145

14.1.1 Values of expressions.................................................................................................................

146

14.2 Operators ...........................................................................................................................................

146

14.2.1 Operator precedence and associativity.......................................................................................

146

14.2.2 Operator overloading .................................................................................................................

147

14.2.3 Unary operator overload resolution ...........................................................................................

148

14.2.4 Binary operator overload resolution...........................................................................................

149

14.2.5 Candidate user-defined operators...............................................................................................

149

14.2.6 Numeric promotions...................................................................................................................

149

14.2.6.1 Unary numeric promotions..................................................................................................

150

14.2.6.2 Binary numeric promotions.................................................................................................

150

14.3 Member lookup .................................................................................................................................

151

14.3.1 Base types ..................................................................................................................................

152

14.4 Function members .............................................................................................................................

152

14.4.1 Argument lists ............................................................................................................................

154

14.4.2 Overload resolution....................................................................................................................

157

14.4.2.1 Applicable function member...............................................................................................

157

14.4.2.2 Better function member.......................................................................................................

158

14.4.2.3 Better conversion ................................................................................................................

158

14.4.3 Function member invocation......................................................................................................

158

14.4.3.1 Invocations on boxed instances...........................................................................................

160

14.5 Primary expressions ..........................................................................................................................

160

14.5.1 Literals .......................................................................................................................................

161

14.5.2 Simple names .............................................................................................................................

161

14.5.2.1 Invariant meaning in blocks ................................................................................................

162

14.5.3 Parenthesized expressions..........................................................................................................

163

14.5.4 Member access...........................................................................................................................

163

14.5.4.1 Identical simple names and type names ..............................................................................

165

14.5.5 Invocation expressions...............................................................................................................

165

14.5.5.1 Method invocations.............................................................................................................

165

14.5.5.2 Delegate invocations ...........................................................................................................

167

14.5.6 Element access ...........................................................................................................................

167

14.5.6.1 Array access ........................................................................................................................

167

14.5.6.2 Indexer access .....................................................................................................................

168

14.5.7 This access .................................................................................................................................

168

14.5.8 Base access.................................................................................................................................

169

14.5.9 Postfix increment and decrement operators ...............................................................................

170

14.5.10 The new operator......................................................................................................................

171

14.5.10.1 Object creation expressions...............................................................................................

171

14.5.10.2 Array creation expressions ................................................................................................

172

14.5.10.3 Delegate creation expressions ...........................................................................................

174

14.5.11 The typeof operator..................................................................................................................

176

14.5.12 The checked and unchecked operators.....................................................................................

178

14.5.13 Default value expression ..........................................................................................................

180

14.5.14 Anonymous methods................................................................................................................

180

14.5.14.1 Anonymous method signatures .........................................................................................

181

14.5.14.2 Anonymous method blocks...............................................................................................

181

14.5.14.3 Outer variables ..................................................................................................................

181

14.5.14.4 Anonymous method evaluation.........................................................................................

184

14.5.14.5 Implementation example ...................................................................................................

185

14.6 Unary expressions .............................................................................................................................

187

14.6.1 Unary plus operator....................................................................................................................

187

14.6.2 Unary minus operator.................................................................................................................

187

vii

C# LANGUAGE SPECIFICATION

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

14.6.3 Logical negation operator ..........................................................................................................

188

14.6.4 Bitwise complement operator ....................................................................................................

188

14.6.5 Prefix increment and decrement operators.................................................................................

189

14.6.6 Cast expressions.........................................................................................................................

189

14.7 Arithmetic operators..........................................................................................................................

190

14.7.1 Multiplication operator...............................................................................................................

190

14.7.2 Division operator........................................................................................................................

191

14.7.3 Remainder operator....................................................................................................................

192

14.7.4 Addition operator .......................................................................................................................

193

14.7.5 Subtraction operator...................................................................................................................

195

14.8 Shift operators ...................................................................................................................................

197

14.9 Relational and type-testing operators ................................................................................................

198

14.9.1 Integer comparison operators.....................................................................................................

199

14.9.2 Floating-point comparison operators .........................................................................................

199

14.9.3 Decimal comparison operators...................................................................................................

200

14.9.4 Boolean equality operators.........................................................................................................

200

14.9.5 Enumeration comparison operators............................................................................................

201

14.9.6 Reference type equality operators..............................................................................................

201

14.9.7 String equality operators ............................................................................................................

203

14.9.8 Delegate equality operators........................................................................................................

203

14.9.9 The is operator ...........................................................................................................................

204

14.9.10 The as operator.........................................................................................................................

204

14.10 Logical operators.............................................................................................................................

205

14.10.1 Integer logical operators...........................................................................................................

206

14.10.2 Enumeration logical operators .................................................................................................

206

14.10.3 Boolean logical operators.........................................................................................................

206

14.11 Conditional logical operators ..........................................................................................................

206

14.11.1 Boolean conditional logical operators......................................................................................

207

14.11.2 User-defined conditional logical operators ..............................................................................

207

14.12 Conditional operator........................................................................................................................

208

14.13 Assignment operators......................................................................................................................

209

14.13.1 Simple assignment ...................................................................................................................

209

14.13.2 Compound assignment .............................................................................................................

211

14.13.3 Event assignment .....................................................................................................................

211

14.14 Expression .......................................................................................................................................

212

14.15 Constant expressions .......................................................................................................................

212

14.16 Boolean expressions........................................................................................................................

213

38

15. Statements..............................................................................................................................................

215

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

15.1

End points and reachability ...............................................................................................................

215

15.2

Blocks................................................................................................................................................

217

15.2.1 Statement lists ............................................................................................................................

217

15.3

The empty statement .........................................................................................................................

217

15.4

Labeled statements ............................................................................................................................

218

15.5

Declaration statements ......................................................................................................................

218

15.5.1 Local variable declarations ........................................................................................................

219

15.5.2 Local constant declarations ........................................................................................................

219

15.6

Expression statements .......................................................................................................................

220

15.7

Selection statements ..........................................................................................................................

220

15.7.1 The if statement..........................................................................................................................

220

15.7.2 The switch statement..................................................................................................................

221

15.8

Iteration statements ...........................................................................................................................

224

15.8.1 The while statement ...................................................................................................................

224

15.8.2 The do statement ........................................................................................................................

225

15.8.3 The for statement .......................................................................................................................

225

viii

Table of Contents

1

2

3

4

5

6

7

8

9

10

11

12

15.8.4 The foreach statement ................................................................................................................

226

15.9 Jump statements ................................................................................................................................

229

15.9.1 The break statement ...................................................................................................................

230

15.9.2 The continue statement ..............................................................................................................

231

15.9.3 The goto statement .....................................................................................................................

231

15.9.4 The return statement...................................................................................................................

232

15.9.5 The throw statement...................................................................................................................

233

15.10

The try statement.............................................................................................................................

234

15.11

The checked and unchecked statements..........................................................................................

236

15.12

The lock statement ..........................................................................................................................

237

15.13

The using statement.........................................................................................................................

237

15.14

The yield statement .........................................................................................................................

239

13

16. Namespaces............................................................................................................................................

243

14

15

16

17

18

19

20

21

22

16.1

Compilation units ..............................................................................................................................

243

16.2

Namespace declarations ....................................................................................................................

243

16.3

Extern alias directives .......................................................................................................................

244

16.4

Using directives.................................................................................................................................

245

16.4.1 Using alias directives .................................................................................................................

245

16.4.2 Using namespace directives .......................................................................................................

249

16.5

Namespace members.........................................................................................................................

251

16.6

Type declarations ..............................................................................................................................

251

16.7

Qualified alias member .....................................................................................................................

251

23

17. Classes ....................................................................................................................................................

255

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

17.1 Class declarations..............................................................................................................................

255

17.1.1 Class modifiers...........................................................................................................................

255

17.1.1.1 Abstract classes ...................................................................................................................

256

17.1.1.2 Sealed classes ......................................................................................................................

256

17.1.1.3 Static classes........................................................................................................................

256

17.1.2 Class base specification..............................................................................................................

257

17.1.2.1 Base classes.........................................................................................................................

258

17.1.2.2 Interface implementations...................................................................................................

259

17.1.3 Class body ..................................................................................................................................

260

17.1.4 Partial declarations.....................................................................................................................

260

17.2 Class members...................................................................................................................................

261

17.2.1 Inheritance..................................................................................................................................

263

17.2.2 The new modifier .......................................................................................................................

264

17.2.3 Access modifiers ........................................................................................................................

264

17.2.4 Constituent types........................................................................................................................

264

17.2.5 Static and instance members ......................................................................................................

264

17.2.6 Nested types ...............................................................................................................................

265

17.2.6.1 Fully qualified name ...........................................................................................................

265

17.2.6.2 Declared accessibility..........................................................................................................

266

17.2.6.3 Hiding..................................................................................................................................

266

17.2.6.4 this access............................................................................................................................

267

17.2.6.5 Access to private and protected members of the containing type .......................................

267

17.2.7 Reserved member names............................................................................................................

268

17.2.7.1 Member names reserved for properties...............................................................................

268

17.2.7.2 Member names reserved for events.....................................................................................

269

17.2.7.3 Member names reserved for indexers .................................................................................

269

17.2.7.4 Member names reserved for destructors .............................................................................

269

17.3 Constants ...........................................................................................................................................

269

17.4 Fields .................................................................................................................................................

271

17.4.1 Static and instance fields............................................................................................................

272

ix

C# LANGUAGE SPECIFICATION

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

17.4.2 Readonly fields ..........................................................................................................................

272

17.4.2.1 Using static readonly fields for constants............................................................................

273

17.4.2.2 Versioning of constants and static readonly fields..............................................................

273

17.4.3 Volatile fields.............................................................................................................................

274

17.4.4 Field initialization ......................................................................................................................

275

17.4.5 Variable initializers ....................................................................................................................

275

17.4.5.1 Static field initialization ......................................................................................................

276

17.4.5.2 Instance field initialization ..................................................................................................

277

17.5 Methods.............................................................................................................................................

277

17.5.1 Method parameters.....................................................................................................................

279

17.5.1.1 Value parameters.................................................................................................................

280

17.5.1.2 Reference parameters ..........................................................................................................

280

17.5.1.3 Output parameters ...............................................................................................................

281

17.5.1.4 Parameter arrays..................................................................................................................

282

17.5.2 Static and instance methods .......................................................................................................

284

17.5.3 Virtual methods..........................................................................................................................

284

17.5.4 Override methods.......................................................................................................................

286

17.5.5 Sealed methods ..........................................................................................................................

287

17.5.6 Abstract methods........................................................................................................................

288

17.5.7 External methods........................................................................................................................

289

17.5.8 Method body ..............................................................................................................................

290

17.5.9 Method overloading ...................................................................................................................

290

17.6 Properties...........................................................................................................................................

290

17.6.1 Static and instance properties.....................................................................................................

291

17.6.2 Accessors ...................................................................................................................................

292

17.6.3 Virtual, sealed, override, and abstract accessors........................................................................

297

17.7 Events................................................................................................................................................

299

17.7.1 Field-like events.........................................................................................................................

301

17.7.2 Event accessors ..........................................................................................................................

302

17.7.3 Static and instance events...........................................................................................................

303

17.7.4 Virtual, sealed, override, and abstract accessors........................................................................

303

17.8 Indexers .............................................................................................................................................

304

17.8.1 Indexer overloading ...................................................................................................................

307

17.9 Operators ...........................................................................................................................................

307

17.9.1 Unary operators..........................................................................................................................

309

17.9.2 Binary operators.........................................................................................................................

309

17.9.3 Conversion operators .................................................................................................................

310

17.10 Instance constructors .......................................................................................................................

311

17.10.1 Constructor initializers .............................................................................................................

312

17.10.2 Instance variable initializers.....................................................................................................

313

17.10.3 Constructor execution ..............................................................................................................

313

17.10.4 Default constructors .................................................................................................................

314

17.10.5 Private constructors..................................................................................................................

315

17.10.6 Optional instance constructor parameters ................................................................................

315

17.11 Static constructors ...........................................................................................................................

316

17.12 Destructors ......................................................................................................................................

318

47

18. Structs ....................................................................................................................................................

321

48

49

50

51

52

53

54

18.1

Struct declarations .............................................................................................................................

321

18.1.1 Struct modifiers..........................................................................................................................

321

18.1.2 Struct interfaces..........................................................................................................................

322

18.1.3 Struct body .................................................................................................................................

322

18.2

Struct members..................................................................................................................................

322

18.3

Class and struct differences...............................................................................................................

322

18.3.1 Value semantics .........................................................................................................................

322

x

Table of Contents

1

2

3

4

5

6

7

8

9

10

11

12

18.3.2 Inheritance..................................................................................................................................

323

18.3.3 Assignment.................................................................................................................................

323

18.3.4 Default values ............................................................................................................................

323

18.3.5 Boxing and unboxing .................................................................................................................

324

18.3.6 Meaning of this ..........................................................................................................................

324

18.3.7 Field initializers..........................................................................................................................

324

18.3.8 Constructors ...............................................................................................................................

325

18.3.9 Destructors .................................................................................................................................

325

18.3.10 Static constructors ....................................................................................................................

325

18.4 Struct examples .................................................................................................................................

326

18.4.1 Database integer type .................................................................................................................

326

18.4.2 Database boolean type................................................................................................................

327

13

19. Arrays.....................................................................................................................................................

329

14

15

16

17

18

19

20

21

19.1

Array types ........................................................................................................................................

329

19.1.1 The System.Array type...............................................................................................................

330

19.2

Array creation....................................................................................................................................

330

19.3

Array element access.........................................................................................................................

330

19.4

Array members..................................................................................................................................

330

19.5

Array covariance ...............................................................................................................................

330

19.6

Arrays and the generic IList interface ...............................................................................................

331

19.7

Array initializers................................................................................................................................

332

22

20. Interfaces................................................................................................................................................

335

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

20.1

Interface declarations ........................................................................................................................

335

20.1.1 Interface modifiers .....................................................................................................................

335

20.1.2 Base interfaces ...........................................................................................................................

336

20.1.3 Interface body ............................................................................................................................

336

20.2

Interface members .............................................................................................................................

337

20.2.1 Interface methods.......................................................................................................................

338

20.2.2 Interface properties ....................................................................................................................

338

20.2.3 Interface events ..........................................................................................................................

338

20.2.4 Interface indexers.......................................................................................................................

338

20.2.5 Interface member access ............................................................................................................

339

20.3

Fully qualified interface member names ...........................................................................................

340

20.4

Interface implementations .................................................................................................................

341

20.4.1 Explicit interface member implementations ..............................................................................

341

20.4.2 Interface mapping ......................................................................................................................

343

20.4.3 Interface implementation inheritance.........................................................................................

346

20.4.4 Interface re-implementation .......................................................................................................

348

20.4.5 Abstract classes and interfaces...................................................................................................

349

40 21. Enums.....................................................................................................................................................

351

41

42

43

44

45

21.1

Enum declarations .............................................................................................................................

351

21.2

Enum modifiers.................................................................................................................................

351

21.3 Enum members..................................................................................................................................

352

21.4

The System.Enum type .....................................................................................................................

354

21.5

Enum values and operations..............................................................................................................

354

46

22. Delegates.................................................................................................................................................

355

47

48

49

22.1

Delegate declarations ........................................................................................................................

355

22.2

Delegate instantiation........................................................................................................................

357

22.3

Delegate invocation...........................................................................................................................

357

50

23. Exceptions ..............................................................................................................................................

361

51

23.1 Causes of exceptions .........................................................................................................................

361

xi

C# LANGUAGE SPECIFICATION

1

2

3

23.2

The System.Exception class..............................................................................................................

361

23.3

How exceptions are handled..............................................................................................................

361

23.4

Common Exception Classes..............................................................................................................

362

4

24. Attributes ...............................................................................................................................................

365

5

6

7

8

9

10

11

12

13

14

15

16

17

18

24.1

Attribute classes ................................................................................................................................

365

24.1.1 Attribute usage ...........................................................................................................................

365

24.1.2 Positional and named parameters...............................................................................................

366

24.1.3 Attribute parameter types...........................................................................................................

367

24.2

Attribute specification .......................................................................................................................

367

24.3

Attribute instances.............................................................................................................................

372

24.3.1 Compilation of an attribute ........................................................................................................

372

24.3.2 Run-time retrieval of an attribute instance.................................................................................

373

24.4

Reserved attributes ............................................................................................................................

373

24.4.1 The AttributeUsage attribute......................................................................................................

373

24.4.2 The Conditional attribute ...........................................................................................................

373

 

24.4.2.1 Conditional Methods...........................................................................................................

373

 

24.4.2.2 Conditional Attribute Classes..............................................................................................

376

24.4.3 The Obsolete attribute................................................................................................................

376

19

25. Unsafe code ............................................................................................................................................

379

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

25.1

Unsafe contexts .................................................................................................................................

379

25.2

Pointer types......................................................................................................................................

381

25.3

Fixed and moveable variables ...........................................................................................................

384

25.4

Pointer conversions ...........................................................................................................................

384

25.5

Pointers in expressions ......................................................................................................................

385

25.5.1 Pointer indirection......................................................................................................................

386

25.5.2 Pointer member access...............................................................................................................

386

25.5.3 Pointer element access ...............................................................................................................

387

25.5.4 The address-of operator..............................................................................................................

388

25.5.5 Pointer increment and decrement...............................................................................................

389

25.5.6 Pointer arithmetic.......................................................................................................................

389

25.5.7 Pointer comparison ....................................................................................................................

390

25.5.8 The sizeof operator.....................................................................................................................

390

25.6

The fixed statement ...........................................................................................................................

391

25.7

Stack allocation .................................................................................................................................

394

25.8

Dynamic memory allocation .............................................................................................................

395

36

26. Generics..................................................................................................................................................

397

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

26.1

Generic class declarations .................................................................................................................

397

26.1.1 Type parameters.........................................................................................................................

397

26.1.2 The instance type .......................................................................................................................

398

26.1.3 Members of generic classes .......................................................................................................

399

26.1.4 Static fields in generic classes....................................................................................................

399

26.1.5 Static constructors in generic classes .........................................................................................

400

26.1.6 Accessing protected members....................................................................................................

400

26.1.7 Overloading in generic classes...................................................................................................

401

26.1.8 Parameter array methods and type parameters...........................................................................

402

26.1.9 Overriding and generic classes...................................................................................................

402

26.1.10 Operators in generic classes .....................................................................................................

402

26.1.11 Nested types in generic classes ................................................................................................

404

26.2

Generic struct declarations ................................................................................................................

404

26.3

Generic interface declarations...........................................................................................................

405

26.3.1 Uniqueness of implemented interfaces ......................................................................................

405

26.3.2 Explicit interface member implementations ..............................................................................

406

26.4

Generic delegate declarations............................................................................................................

406

xii

Table of Contents

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

26.5 Constructed types ..............................................................................................................................

407

26.5.1 Type arguments..........................................................................................................................

408

26.5.2 Open and closed types................................................................................................................

408

26.5.3 Base classes and interfaces of a constructed type ......................................................................

408

26.5.4 Members of a constructed type ..................................................................................................

409

26.5.5 Accessibility of a constructed type ............................................................................................

410

26.5.6 Conversions................................................................................................................................

410

26.5.7 Using alias directives .................................................................................................................

410

26.6 Generic methods................................................................................................................................

411

26.6.1 Generic method signatures.........................................................................................................

411

26.6.2 Virtual generic methods .............................................................................................................

412

26.6.3 Calling generic methods.............................................................................................................

413

26.6.4 Inference of type arguments.......................................................................................................

413

26.6.5 Using a generic method with a delegate.....................................................................................

415

26.6.6 No generic properties, events, indexers, operators, constructors, or destructors .......................

415

26.7 Constraints.........................................................................................................................................

415

26.7.1 Satisfying constraints .................................................................................................................

419

26.7.2 Member lookup on type parameters...........................................................................................

420

26.7.3 Type parameters and boxing ......................................................................................................

421

26.7.4 Conversions involving type parameters .....................................................................................

422

21

27. Iterators..................................................................................................................................................

423

22

23

24

25

26

27

28

29

30

31

32

33

27.1

Iterator blocks....................................................................................................................................

423

27.1.1 Enumerator interfaces ................................................................................................................

424

27.1.2 Enumerable interfaces................................................................................................................

424

27.1.3 Yield type...................................................................................................................................

424

27.1.4 This access .................................................................................................................................

424

27.2

Enumerator objects............................................................................................................................

424

27.2.1 The MoveNext method ..............................................................................................................

425

27.2.2 The Current property..................................................................................................................

426

27.2.3 The Dispose method...................................................................................................................

426

27.3

Enumerable objects ...........................................................................................................................

426

27.3.1 The GetEnumerator method.......................................................................................................

427

27.4

Implementation example ...................................................................................................................

427

34 Annex A. Grammar ....................................................................................................................................

433

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

A.1 Lexical grammar.................................................................................................................................

433

A.1.1 Line terminators ..........................................................................................................................

433

A.1.2 White space .................................................................................................................................

433

A.1.3 Comments ...................................................................................................................................

434

A.1.4 Tokens.........................................................................................................................................

434

A.1.5 Unicode character escape sequences...........................................................................................

434

A.1.6 Identifiers ....................................................................................................................................

435

A.1.7 Keywords ....................................................................................................................................

436

A.1.8 Literals.........................................................................................................................................

436

A.1.9 Operators and punctuators...........................................................................................................

438

A.1.10 Pre-processing directives...........................................................................................................

438

A.2 Syntactic grammar..............................................................................................................................

440

A.2.1 Basic concepts.............................................................................................................................

440

A.2.2 Types...........................................................................................................................................

440

A.2.3 Variables .....................................................................................................................................

441

A.2.4 Expressions .................................................................................................................................

442

A.2.5 Statements ...................................................................................................................................

445

A.2.6 Classes.........................................................................................................................................

449

A.2.7 Structs..........................................................................................................................................

454

xiii

C# LANGUAGE SPECIFICATION

1

2

3

4

5

6

7

A.2.8 Arrays..........................................................................................................................................

455

A.2.9 Interfaces .....................................................................................................................................

456

A.2.10 Enums........................................................................................................................................

456

A.2.11 Delegates ...................................................................................................................................

457

A.2.12 Attributes...................................................................................................................................

457

A.2.13 Generics.....................................................................................................................................

458

A.3 Grammar extensions for unsafe code .................................................................................................

459

8 Annex B. Portability issues.........................................................................................................................

461

9

10

11

12

B.1

Undefined behavior ............................................................................................................................

461

B.2

Implementation-defined behavior.......................................................................................................

461

B.3

Unspecified behavior..........................................................................................................................

462

B.4

Other Issues ........................................................................................................................................

462

13 Annex C. Naming guidelines ......................................................................................................................

463

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

C.1

Capitalization styles............................................................................................................................

463

C.1.1 Pascal casing................................................................................................................................

463

C.1.2 Camel casing ...............................................................................................................................

463

C.1.3 All uppercase...............................................................................................................................

463

C.1.4 Capitalization summary...............................................................................................................

463

C.2

Word choice........................................................................................................................................

464

C.3

Namespaces ........................................................................................................................................

464

C.4

Classes ................................................................................................................................................

464

C.5

Interfaces ............................................................................................................................................

465

C.6 Enums .................................................................................................................................................

465

C.7

Static fields .........................................................................................................................................

466

C.8

Parameters ..........................................................................................................................................

466

C.9

Methods ..............................................................................................................................................

466

C.10

Properties..........................................................................................................................................

466

C.11

Events ...............................................................................................................................................

467

C.12

Case sensitivity .................................................................................................................................

467

C.13

Avoiding type name confusion.........................................................................................................

468

31

Annex D. Standard Library .......................................................................................................................

469

32

Annex E. Documentation Comments ........................................................................................................

511

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

E.1 Introduction.........................................................................................................................................

511

E.2 Recommended tags .............................................................................................................................

512

E.2.1 <c>...............................................................................................................................................

513

E.2.2 <code> .........................................................................................................................................

513

E.2.3 <example> ...................................................................................................................................

513

E.2.4 <exception> .................................................................................................................................

513

E.2.5 <list>............................................................................................................................................

514

E.2.6 <para>..........................................................................................................................................

515

E.2.7 <param>.......................................................................................................................................

515

E.2.8 <paramref> ..................................................................................................................................

516

E.2.9 <permission> ...............................................................................................................................

516

E.2.10 <remarks>..................................................................................................................................

516

E.2.11 <returns>....................................................................................................................................

517

E.2.12 <see>..........................................................................................................................................

517

E.2.13 <seealso> ...................................................................................................................................

518

E.2.14 <summary>................................................................................................................................

518

E.2.15 <value> ......................................................................................................................................

518

E.3 Processing the documentation file ......................................................................................................

519

E.3.1 ID string format ...........................................................................................................................

519

E.3.2 ID string examples.......................................................................................................................

520

xiv

Table of Contents

1

2

3

4

E.4 An example.........................................................................................................................................

523

E.4.1 C# source code.............................................................................................................................

523

E.4.2 Resulting XML ............................................................................................................................

525

Annex F. Bibliography................................................................................................................................

528

xv

Foreword

1Foreword

2This third edition cancels and replaces the second edition. Changes from the previous edition include:

31. §1 has been made normative.

42. §3 Several entries have been moved to the (new) annex, §Annex F, “Bibliography”.

53. §9.2.3, “Grammar ambiguities”, is new.

64. §9.4.5 Due to the addition of generics, >> is made up of the two tokens > and >. Similarly, >>= is made

7up of the two tokens > and >=.

85. §9.5.7 corrected the production for file-name-character:: and expanded the definition of #line to allow

9other implementation-defined syntax.

106. §9.5.8, “Pragma directives”, is new.

117. §10.7.1 Clarifed an existing restriction on name hiding. (This also resulted in some text being deleted

12from §10.3.)

138. §10.7.1.2 Removed the text “The rules governing operator declarations (§17.9) make it impossible for a

14derived class to declare an operator with the same signature as an operator in a base class. Thus,

15operators never hide one another.”

169. §11.1.1, “The System.ValueType type”, is new; however, it adds no new normative requirements that

17didn’t already exist.

1810. §11.1.6 Added text regarding support for denormalized floating-point numbers.

1911. §11.1.7 (and other subclauses too) Amended to permit alternate representations for the decimal type.

2012. §12.1.7 Clarified local variable creation in iterative situations.

2113. §12.3.3.26 Added text regarding definite assignment.

2214. §14.5.13, “Default value expression” is new.

2315. §14.5.14, “Anonymous methods”, is new. (There is also new or changed text in §8.17, §12.3.3.27,

24§13.1, §13.5, §13.6, §14.5, §14.9.8, §25.5.4, §25.6, and §B.3.)

2516. §14.7.2 Changed the wording for the scale of the result of decimal division.

2617. §16.7 and contains text for qualified alias members. (There is also new or changed text in §10.8,

27§14.5.4, and §14.5.13.)

2818. §17.1.1.3, “Static classes”, is new. (There is also new or changed text in §8.7.12, §10.8, and §14.5.)

2919. §17.1.4, “Partial declarations”, is new. (There is also new or changed text in §8.7.13, §16.4.1, §17.1,

30§17.1.1, §17.1.1.1, §17.1.1.2, §17.1.2.1, §17.1.2.2, §17.2, §17.2.2, §18.1, §20.1, §24.2, §25.1, §26.7, and

31§B.3.)

3220. §17.6.2 The set and get accessors for properties and indexers can now have different accessibility.

3321. §21.4, “The System.Enum type” is new; however, it adds no new normative requirements that didn’t

34already exist.

3522. §26, “Generics” is new. (There is also new or changed text in §8.16, §10.1, §10.5.3, §10.8, §11, §14.3,

36§14.5.4, §14.5.11, §15.8.4, §17.1, §17.1.1, §17.2, §18.1, §18.2, §20.1, §20.2, §20.2.1, and §22.1.)

3723. §27, “Iterators”, is new. (There is also new or changed text in §8.18, §12.3.3.28, §15.14, and §B.3.)

3824. Annex F, “Bibliography”, is new.

xvii

Introduction

1Introduction

2This International Standard is based on a submission from Hewlett-Packard, Intel, and Microsoft, that

3describes a language called C#, which was developed within Microsoft. The principal inventors of this

4language were Anders Hejlsberg, Scott Wiltamuth, and Peter Golde. The first widely distributed

5implementation of C# was released by Microsoft in July 2000, as part of its .NET Framework initiative.

6ECMA Technical Committee 39 (TC39) Task Group 2 (TG2) was formed in September 2000, to produce a

7standard for C#. Another Task Group, TG3, was also formed at that time to produce a standard for a library

8and execution environment called Common Language Infrastructure (CLI). (CLI is based on a subset of the

9.NET Framework.) Although Microsoft’s implementation of C# relies on CLI for library and runtime

10support, other implementations of C# need not, provided they support an alternate way of getting at the

11minimum CLI features required by this C# standard.

12As the definition of C# evolved, the goals used in its design were as follows:

13C# is intended to be a simple, modern, general-purpose, object-oriented programming language.

14The language, and implementations thereof, should provide support for software engineering principles

15such as strong type checking, array bounds checking, detection of attempts to use uninitialized variables,

16and automatic garbage collection. Software robustness, durability, and programmer productivity are

17important.

18The language is intended for use in developing software components suitable for deployment in

19distributed environments.

20Source code portability is very important, as is programmer portability, especially for those

21programmers already familiar with C and C++.

22Support for internationalization is very important.

23C# is intended to be suitable for writing applications for both hosted and embedded systems, ranging

24from the very large that use sophisticated operating systems, down to the very small having dedicated

25functions.

26Although C# applications are intended to be economical with regards to memory and processing power

27requirements, the language was not intended to compete directly on performance and size with C or

28assembly language.

29The development of this standard started in November 2000.

30It is expected there will be future revisions to this standard, primarily to add new functionality.

xix

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