3.03.0
(Microsoft Corp.), 1999-2007. .
Microsoft, Windows, Visual Basic, Visual C# Visual C++
.
.
1. ...........................................................................................................................................................1
1.1 Hello world...........................................................................................................................1
1.2 ................................................................................................................................2
1.3 ....................................................................................................................................4
1.4 ..................................................................................................................................................8
1.5 .................................................................................................................................................10
1.6 .....................................................................................................................................14
1.6.1 ..................................................................................................................................................14
1.6.2 .......................................................................................................................................15
1.6.3 .................................................................................................................................15
1.6.4 ..................................................................................................................................16
1.6.5 ....................................................................................................................................................16
1.6.6 ...............................................................................................................................................17
1.6.6.1 ...................................................................................................................................17
1.6.6.2 .......................................................................................19
1.6.6.3 ...............................................................................19
1.6.6.4 , .......................................................20
1.6.6.5 .......................................................................................................................22
1.6.7 -.....................................................................................................................23
1.6.7.1 ..............................................................................................................................25
1.6.7.2 .......................................................................................................................................26
1.6.7.3 ................................................................................................................................26
1.6.7.4 .......................................................................................................................................27
1.6.7.5 ....................................................................................................................................27
1.6.7.6 ................................................................................................................................28
1.7 .................................................................................................................................................28
1.8 ....................................................................................................................................................29
1.9 ..............................................................................................................................................31
1.10 ..............................................................................................................................32
1.11 ..................................................................................................................................................33
1.12 .................................................................................................................................................34
2. ...............................................................................................................................37
2.1 ................................................................................................................................................37
2.2 ...............................................................................................................................................37
2.2.1 ...................................................................................................................37
2.2.2 ....................................................................................................................38
2.2.3 ..............................................................................................................38
2.3 ..................................................................................................................................38
2.3.1 .....................................................................................................................39
2.3.2 ......................................................................................................................................40
2.3.3 ................................................................................................................................................41
2.4 .....................................................................................................................................................41
2.4.1 .................................................................41
2.4.2 ................................................................................................................................42
2.4.3 ..................................................................................................................44
2.4.4 ............................................................................................................................................44
2.4.4.1 ..................................................................................................................44
2.4.4.2 ...........................................................................................................45
2.4.4.3 ..........................................................................................................46
2.4.4.4 ................................................................................................................46
1.1.1.1
.............................................................................................................47
2.4.4.5 null.................................................................................................................................49
2.4.5 ........................................................................................................49
2.5 ..................................................................................................................50
2.5.1 .......................................................................................................51
2.5.2 ...........................................................................................................51
2.5.3 ......................................................................................................................52
2.5.4 ....................................................................................................53
2.5.5 ....................................................................................................................55
2.5.6 ..........................................................................................................................56
2.5.7 ................................................................................................................................56
2.5.8 pragma.............................................................................................................................57
2.5.8.1 pragma warning..........................................................................................................57
3. ...................................................................................................................................59
3.1 ..................................................................................................................................59
3.2 ..........................................................................................................................60
3.3 ...............................................................................................................................................60
3.4 .........................................................................................................................................................62
3.4.1 .................................................................................................................63
3.4.2 ...............................................................................................................................63
3.4.3 ..........................................................................................................................63
3.4.4 ......................................................................................................................................63
3.4.5 .............................................................................................................................64
3.4.6 ...................................................................................................................................64
3.4.7 ...................................................................................................................................64
3.5 ....................................................................................................................................64
3.5.1 .................................................................................................................64
3.5.2 .........................................................................................................................65
3.5.3 ................................................................................68
3.5.4 ................................................................................................................69
3.6 ...........................................................................................................................69
3.7 ......................................................................................................................................................71
3.7.1 ...................................................................................................................................73
3.7.1.1 .............................................................................................................74
3.7.1.2 ......................................................................................................74
3.8 ............................................................................................................76
3.8.1 ....................................................................................................................................78
3.9 .....................................................................................................79
3.10 ..............................................................................................................................81
4. ...............................................................................................................................................................83
4.1 ..........................................................................................................................................83
4.1.1 System.ValueType........................................................................................................................84
4.1.2 ............................................................................................................84
4.1.3 .................................................................................................................................85
4.1.4 .....................................................................................................................................85
4.1.5 ........................................................................................................................................86
4.1.6 ...............................................................................................................88
4.1.7 decimal.........................................................................................................................................89
4.1.8 bool..............................................................................................................................................89
4.1.9 .........................................................................................................................90
4.1.10 ............................................................................................................................90
4.2 .......................................................................................................................................91
iv
4.2.1 ......................................................................................................................................91
4.2.2 ........................................................................................................................................92
4.2.3 ...................................................................................................................................92
4.2.4 ...............................................................................................................................92
4.2.5 ...................................................................................................................................92
4.2.6 ..................................................................................................................................92
4.3 ..............................................................................................................................93
4.3.1 .................................................................................................................93
4.3.2 ..............................................................................................................94
4.4 ............................................................................................................................95
4.4.1 .................................................................................................................................96
4.4.2 ..............................................................................................................96
4.4.3 ........................................................................................................97
4.4.4 .................................................................................................................97
4.5 ........................................................................................................................................98
4.6 ..........................................................................................................................99
5. .................................................................................................................................................101
5.1 ...........................................................................................................................101
5.1.1 ................................................................................................................101
5.1.2 ..................................................................................................................101
5.1.2.1 ...........................................................................................102
5.1.2.2 .....................................................................................102
5.1.3 ............................................................................................................................102
5.1.4 ..................................................................................................................102
5.1.5 ......................................................................................................................102
5.1.6 ......................................................................................................................103
5.1.7 ...................................................................................................................103
5.2 .........................................................................................................................104
5.3 ..................................................................................................................104
5.3.1 .............................................................................................105
5.3.2 ............................................................................................106
5.3.3 .....................................................106
5.3.3.1 ................................................................................................106
5.3.3.2 , ...............................................107
5.3.3.3 ..............................................................................................................107
5.3.3.4 ..............................................................................................................107
5.3.3.5 If..........................................................................................................................107
5.3.3.6 switch...................................................................................................................108
5.3.3.7 while....................................................................................................................108
5.3.3.8 do.........................................................................................................................108
5.3.3.9 for........................................................................................................................109
5.3.3.10 break, continue goto............................................................................................109
5.3.3.11 throw......................................................................................................................109
5.3.3.12 return.....................................................................................................................109
5.3.3.13 try-catch.............................................................................................................110
5.3.3.14 try-finally...........................................................................................................110
5.3.3.15 try-catch-finally.................................................................................................110
5.3.3.16 foreach...............................................................................................................111
5.3.3.17 using...................................................................................................................111
5.3.3.18 lock....................................................................................................................111
5.3.3.19 yield...................................................................................................................112
5.3.3.20 ..............................................................................112
(Microsoft Corp.), 1999-2007. .
5.3.3.21 ...........................................112
5.3.3.22 ..............................................................112
5.3.3.23 ........................................................................................113
5.3.3.24 &&........................................................................................................................113
5.3.3.25 ||.............................................................................................................................114
5.3.3.26 !.........................................................................................................................115
5.3.3.27 ?? ??..................................................................................................................115
5.3.3.28 ?:.......................................................................................................................116
5.3.3.29 ...............................................................................................................116
5.4 .........................................................................................................................117
5.5 .....................................................................................................117
6. ..........................................................................................................................................119
6.1 .......................................................................................................................119
6.1.1 ....................................................................................................119
6.1.2 .....................................................................................120
6.1.3 ...........................................................................120
6.1.4 ................................................................................120
6.1.5 null.........................................................................................................121
6.1.6 ..................................................................................121
6.1.7 ...............................................................................................................121
6.1.8 .............................................................................122
6.1.9 , .............................................................122
6.1.10 ................................................................................122
6.1.11 ...............................123
6.2 ...........................................................................................................................123
6.2.1 ........................................................................................123
6.2.2 ...............................................................................125
6.2.3 ....................................................................................125
6.2.4 ......................................................................................126
6.2.5 ............................................................................................................126
6.2.6 , .................................................................127
6.2.7 ......................................................................................128
6.3 ................................................................................................................128
6.3.1 ..........................................................................................128
6.3.2 ..............................................................................................128
6.4 ........................................................................................................128
6.4.1 ............................................................................128
6.4.2 .................................................................................129
6.4.3 ............................................................................129
6.4.4 ..................................................................................130
6.4.5 ......................................................................................131
6.5 ..................................................................................................132
6.5.1 ............................................133
6.5.2 ............................134
6.5.3 ..........................................................................................................................134
6.6 ..........................................................................................................137
7. ..................................................................................................................................................141
7.1 .................................................................................................................................141
7.1.1 ........................................................................................................................142
7.2 ...............................................................................................................................................142
7.2.1 .....................................................................................143
7.2.2 ....................................................................................................................144
vi
7.2.3 ................................................................................145
7.2.4 ..............................................................................145
7.2.5 -.......................................................................................146
7.2.6 ......................................................................................................................146
7.2.6.1 ............................................................................146
7.2.6.2 ..........................................................................146
7.2.7 ............................................................................................................147
7.3 ..........................................................................................................................................148
7.3.1 ....................................................................................................................................149
7.4 -......................................................................................................................................150
7.4.1 ..........................................................................................................................153
7.4.2 .......................................................................................................................................155
7.4.2.1 ...............................................................................................................................156
7.4.2.2 ................................................................................................................................156
7.4.2.3 .................................................................................................................................156
7.4.2.4 ..............................................................................................................................156
7.4.2.5 ...............................................................................................................................157
7.4.2.6 ....................................................................................................................157
7.4.2.7 ...............................................................................................157
7.4.2.8 ............................................................................................................................157
7.4.2.9 ...............................................................................................................157
7.4.2.10 ..........................................................................................................................158
7.4.2.11 ............................................................................158
7.4.2.12 ...................................................................159
7.4.2.13 ....................................159
7.4.3 ...................................................................................................................160
7.4.3.1 -......................................................................................................160
7.4.3.2 -.............................................................................................161
7.4.3.3 ....................................................................................162
7.4.3.4 ...............................................................................................162
7.4.3.5 ......................................................................................163
7.4.4 -......................................................................................................................163
7.4.4.1 ..........................................................................................165
7.5 .............................................................................................................................165
7.5.1 ..........................................................................................................................................166
7.5.2 .................................................................................................................................166
7.5.2.1 ..........................................................................................167
7.5.3 ..................................................................................................................168
7.5.4 ....................................................................................................................168
7.5.4.1 ............................................................................170
7.5.4.2 ............................................................................................171
7.5.5 ...........................................................................................................................171
7.5.5.1 ........................................................................................................................172
7.5.5.2 ...................................................................................................173
7.5.5.3 ......................................................................................................................175
7.5.6 ...............................................................................................................176
7.5.6.1 .......................................................................................................................176
7.5.6.2 ...............................................................................................................177
7.5.7 this........................................................................................................................................177
7.5.8 base.......................................................................................................................................178
7.5.9 .....................................................................179
7.5.10 new.................................................................................................................................180
7.5.10.1 ...............................................................................................180
(Microsoft Corp.), 1999-2007. .
vii
7.5.10.2 ......................................................................................................181
7.5.10.3 ...................................................................................................183
7.5.10.4 .................................................................................................184
7.5.10.5 ................................................................................................186
7.5.10.6 ...........................................................................188
7.5.11 typeof..............................................................................................................................189
7.5.12 checked unchecked....................................................................................................191
7.5.13 ............................................................................................193
7.5.14 ....................................................................................................194
7.6 ................................................................................................................................194
7.6.1 ..............................................................................................................194
7.6.2 ............................................................................................................194
7.6.3 ...................................................................................................195
7.6.4 ..................................................................................................195
7.6.5 .......................................................................195
7.6.6 ..........................................................................................................196
7.7 ..................................................................................................................197
7.7.1 ...................................................................................................................197
7.7.2 .............................................................................................................................198
7.7.3 .............................................................................................................................199
7.7.4 ..........................................................................................................................200
7.7.5 ........................................................................................................................202
7.8 ...................................................................................................................................204
7.9 ...............................................................................................205
7.9.1 ................................................................................................206
7.9.2 .......................................................................206
7.9.3 .......................................................................................207
7.9.4 ...................................................................................................207
7.9.5 ..............................................................................208
7.9.6 ..........................................................................208
7.9.7 .............................................................................................................209
7.9.8 ......................................................................................................210
7.9.9 null.............................................................................................210
7.9.10 is.....................................................................................................................................210
7.9.11 as.....................................................................................................................................211
7.10 .........................................................................................................................212
7.10.1 ......................................................................................212
7.10.2 ....................................................................................213
7.10.3 ...................................................................................................................213
7.10.4 .............................................................................................213
7.11 .........................................................................................................214
7.11.1 ..................................................................................................214
7.11.2 ...................................................................214
7.12 null.......................................................................................................................215
7.13 ...............................................................................................................................216
7.14 ........................................................................................................217
7.14.1 ......................................................................................................218
7.14.2 .............................................................................................................219
7.14.3 .................................................................................................................219
7.14.4 ....................................................................................................................220
7.14.4.1 ........................................................................................220
7.14.4.2 ....................................................................221
7.14.5 ............................................................................223
viii
7.15 ............................................................................................................................223
7.15.1 ...................................................................................224
7.15.2 .......................................................................................................225
7.15.2.1 select groupby ..................................................................225
7.15.2.2 ......................................................................................226
7.15.2.3 ......................................................................................226
7.15.2.4 from, let, where, join orderby.........................................................................227
7.15.2.5 select..................................................................................................................230
7.15.2.6 groupby.............................................................................................................230
7.15.2.7 .................................................................................................231
7.15.3 ..........................................................................................................232
7.16 .....................................................................................................................233
7.16.1 ...................................................................................................................234
7.16.2 ..................................................................................................................236
7.16.3 ..................................................................................................................237
7.17 ............................................................................................................................................237
7.18 ......................................................................................................................237
7.19 ........................................................................................................................239
8. .......................................................................................................................................240
8.1 ..........................................................................................................240
8.2 .......................................................................................................................................................242
8.2.1 ..........................................................................................................................242
8.3 .....................................................................................................................................243
8.4 ..........................................................................................................................243
8.5 ...........................................................................................................................244
8.5.1 ..............................................................................................244
8.5.2 ....................................................................................................246
8.6 -...........................................................................................................................246
8.7 ..................................................................................................................................246
8.7.1 if........................................................................................................................................247
8.7.2 switch................................................................................................................................247
8.8 ..............................................................................................................................251
8.8.1 while.................................................................................................................................251
8.8.2 do......................................................................................................................................252
8.8.3 for.....................................................................................................................................252
8.8.4 foreach..............................................................................................................................253
8.9 ...............................................................................................................................256
8.9.1 break.................................................................................................................................257
8.9.2 continue............................................................................................................................258
8.9.3 goto...................................................................................................................................258
8.9.4 return.................................................................................................................................259
8.9.5 throw.................................................................................................................................260
8.10 try..........................................................................................................................................261
8.11 checked unchecked..........................................................................................................264
8.12 lock........................................................................................................................................264
8.13 using......................................................................................................................................265
8.14 yield.......................................................................................................................................267
9. ....................................................................................................................................269
9.1 .............................................................................................................................269
9.2 .............................................................................................................269
9.3 ............................................................................................................................271
9.4 using.....................................................................................................................................271
(Microsoft Corp.), 1999-2007. .
ix
10.5.2.1 .............................310
10.5.2.2 ...........................311
10.5.3 volatile.....................................................................................................311
10.5.4 ......................................................................................................................313
10.5.5 .......................................................................................................313
10.5.5.1 .........................................................................................314
10.5.5.2 ...........................................................................................315
10.6 ..................................................................................................................................................315
10.6.1 ..........................................................................................................................317
10.6.1.1 .........................................................................................................318
10.6.1.2 ..............................................................................................................319
10.6.1.3 ..............................................................................................................320
10.6.1.4 ...............................................................................................................320
10.6.2 ..................................................................................323
10.6.3 .....................................................................................................................323
10.6.4 ..........................................................................................................325
10.6.5 ....................................................................................................................327
10.6.6 ......................................................................................................................328
10.6.7 ............................................................................................................................329
10.6.8 ......................................................................................................................330
10.6.9 ......................................................................................................................330
10.6.10 ...................................................................................................................................331
10.6.11 ........................................................................................................................331
10.7 ................................................................................................................................................331
10.7.1 ..............................................................................333
10.7.2 .............................................................................................................................333
10.7.3 .........................................................................................338
10.7.4 ...................................................................................................................................339
10.7.5 , , ...................340
10.8 .................................................................................................................................................342
10.8.1 , ............................................................................................................344
10.8.2 .........................................................................................................345
10.8.3 .............................................................................346
10.8.4 , , ...................347
10.9 .........................................................................................................................................347
10.9.1 ................................................................................................................351
10.10 ...........................................................................................................................................351
10.10.1 ......................................................................................................................352
10.10.2 ....................................................................................................................353
10.10.3 .........................................................................................................353
10.11 ................................................................................................................356
10.11.1 .................................................................................................357
10.11.2 ...............................................................................358
10.11.3 ...........................................................................................................358
10.11.4 ......................................................................................................360
10.11.5 ..............................................................................................................360
10.11.6 ...........................................................361
10.12 ................................................................................................................361
10.13 ........................................................................................................................................363
10.14 ...........................................................................................................................................365
10.14.1 ........................................................................................................365
10.14.2 .........................................................................................................365
10.14.3 yield........................................................................................................................................365
(Microsoft Corp.), 1999-2007. .
xi
10.14.4 ...............................................................................................................365
10.14.4.1 MoveNext....................................................................................................................366
10.14.4.2 Current....................................................................................................................367
10.14.4.3 Dispose........................................................................................................................367
10.14.5 ...............................................................................................................368
10.14.5.1 GetEnumerator............................................................................................................368
10.14.6 ......................................................................................................................368
11. .................................................................................................................................................375
11.1 ............................................................................................................................375
11.1.1 .............................................................................................................375
11.1.2 partial.......................................................................................................................376
11.1.3 .................................................................................................................376
11.1.4 ...............................................................................................................................376
11.2 ..................................................................................................................................376
11.3 ...............................................................................................376
11.3.1 .......................................................................................................................377
11.3.2 ..................................................................................................................................378
11.3.3 .................................................................................................................................378
11.3.4 ................................................................................................................378
11.3.5 ...................................................................................................................379
11.3.6 this......................................................................................................381
11.3.7 ..................................................................................................................381
11.3.8 .................................................................................................................................381
11.3.9 ...................................................................................................................................382
11.3.10 ..........................................................................................................382
11.4 ................................................................................................................................382
11.4.1 ..............................................................................382
11.4.2 ........................................................................................................384
12. .....................................................................................................................................................387
12.1 ........................................................................................................................................387
12.1.1 System.Array............................................................................................................................388
12.1.2 IList................................................................................388
12.2 ..................................................................................................................................389
12.3 ................................................................................................................389
12.4 ......................................................................................................................................389
12.5 ...........................................................................................................................389
12.6 .....................................................................................................................390
13. ..............................................................................................................................................393
13.1 .....................................................................................................................393
13.1.1 ...........................................................................................................393
13.1.2 partial.......................................................................................................................393
13.1.3 ......................................................................................................................394
13.1.4 ............................................................................................................................394
13.2 ................................................................................................................................395
13.2.1 .......................................................................................................................396
13.2.2 .....................................................................................................................396
13.2.3 .....................................................................................................................396
13.2.4 ..............................................................................................................396
13.2.5 ........................................................................................................397
13.3 ......................................................................................................399
13.4 ......................................................................................................................399
xii
13.4.1 .........................................................................................400
13.4.2 ................................................................................402
13.4.3 .............................................................................................403
13.4.4 .........................................................................................................404
13.4.5 .......................................................................................407
13.4.6 ...............................................................................................408
13.4.7 ..............................................................................................409
14. ..............................................................................................................................411
14.1 ...................................................................................................................411
14.2 ..............................................................................................................412
14.3 .............................................................................................................................412
14.4 System.Enum..................................................................................................................................414
14.5 ....................................................................................................414
15. ....................................................................................................................................................415
15.1 ..........................................................................................................................415
15.2 ....................................................................................................................417
15.3 .............................................................................................................417
15.4 ......................................................................................................................................418
16. ..............................................................................................................................................421
16.1 ..........................................................................................................................421
16.2 System.Exception........................................................................................................................421
16.3 ........................................................................................................................421
16.4 ..................................................................................................................422
17. ...................................................................................................................................................425
17.1 .................................................................................................................................425
17.1.1 ..............................................................................................................425
17.1.2 ....................................................................................426
17.1.3 ............................................................................................................427
17.2 .......................................................................................................................427
17.3 .........................................................................................................................433
17.3.1 ....................................................................................................................433
17.3.2 .............................................................433
17.4 ..............................................................................................................433
17.4.1 AttributeUsage..................................................................................................................434
17.4.2 Conditional.......................................................................................................................434
17.4.2.1 .....................................................................................................................435
17.4.2.2 ...................................................................................................437
17.4.3 Obsolete............................................................................................................................437
17.5 .............................................................................................................438
17.5.1 COM Win32.........................................................................438
17.5.1.1 .NET IndexerName........................................439
18. .....................................................................................................................................441
18.1 ......................................................................................................................441
18.2 .....................................................................................................................................443
18.3 ..................................................................................446
18.4 .................................................................................................................446
18.5 ......................................................................................................................447
18.5.1 .............................................................................................448
18.5.2 ........................................................................................................448
18.5.3 ..................................................................................................449
(Microsoft Corp.), 1999-2007. .
xiii
18.5.4 .............................................................................................................................450
18.5.5 ...........................................................................................451
18.5.6 .....................................................................................451
18.5.7 ....................................................................................................................452
18.5.8 sizeof...............................................................................................................................452
18.6 fixed.......................................................................................................................................453
18.7 ........................................................................................................457
18.7.1 ...........................................................................457
18.7.2 .........................................................................458
18.7.3 .......................................................................................459
18.8 ...................................................................................................................................459
18.9 ........................................................................................................460
A. ................................................................................................................463
A.1 .................................................................................................................................................463
A.2 ..............................................................................................................................464
A.2.1 <c>.............................................................................................................................................465
A.2.2 <code>........................................................................................................................................465
A.2.3 <example>..................................................................................................................................466
A.2.4 <exception>................................................................................................................................466
A.2.5 <include>...................................................................................................................................467
A.2.6 <list>..........................................................................................................................................467
A.2.7 <para>........................................................................................................................................468
A.2.8 <param>.....................................................................................................................................469
A.2.9 <paramref>.................................................................................................................................469
A.2.10 <permission>............................................................................................................................469
A.2.11 <summary>...............................................................................................................................470
A.2.12 <returns>..................................................................................................................................470
A.2.13 <see>........................................................................................................................................471
A.2.14 <seealso>..................................................................................................................................471
A.2.15 <summary>..............................................................................................................................471
A.2.16 <value>....................................................................................................................................472
A.2.17 <typeparam>............................................................................................................................472
A.2.18 <typeparamref>........................................................................................................................472
A.3 ...........................................................................................................473
A.3.1 .....................................................................................................473
A.3.2 ..................................................................................................474
A.4 ....................................................................................................................................................477
A.4.1 C#.............................................................................................................................477
A.4.2 XML....................................................................................................................480
B. .................................................................................................................................................483
B.1 ...................................................................................................................................................483
B.1.1 ...............................................................................................................483
B.1.2 ...................................................................................................................................483
B.1.3 ..............................................................................................................................................484
B.1.4 ...........................................................................................................................................484
B.1.5 ..............................................................484
B.1.6 .............................................................................................................................484
B.1.7 ...............................................................................................................................486
B.1.8 ..........................................................................................................................................486
B.1.9 ......................................................................................................488
B.1.10 ......................................................................................488
B.2 ...............................................................................................................................................491
xiv
B.2.1 .........................................................................................................................491
B.2.2 .................................................................................................................................................491
B.2.3 .....................................................................................................................................492
B.2.4 .......................................................................................................................................492
B.2.5 ........................................................................................................................................499
B.2.6 ..........................................................................................................................502
B.2.7 ..............................................................................................................................................503
B.2.8 ........................................................................................................................................510
B.2.9 ...........................................................................................................................................511
B.2.10 ...................................................................................................................................511
B.2.11 .................................................................................................................................512
B.2.12 ........................................................................................................................................513
B.2.13 ........................................................................................................................................513
B.3 ........................................................................514
C. ........................................................................................................................................................519
xv
1.
C# ( - -) , ,
- . C#
C , C, C++ Java.
C# ECMA ECMA-334, ISO/IEC
ISO/IEC 23270. Microsoft C# .NET Framework
.
C# - ,
_ .
.
, , ,
,
. C# ,
,
.
C# ,
. : ,
; ,
;
, ,
.
C# . C#, (,
int double), object. ,
, ,
. , C#
,
.
C#,
C# .
,
.
C# : virtual
override,
.
C#.
. , .
,
, .
using System;
class Hello
{
static void Main() {
Console.WriteLine("!");
}
}
hello.exe.
:
!
1.2
C# : , , ,
. C# .
, .
. , ,
. C# .
exe dll
.
:
using System;
namespace Acme.Collections
{
public class Stack
{
Entry top;
2
2
public void Push(object data) {
top = new Entry(top, data);
}
public object Pop() {
if (top == null) throw new InvalidOperationException();
object result = top.data;
top = top.next;
return result;
}
class Entry
{
public Entry next;
public object data;
public Entry(Entry next, object data) {
this.next = next;
this.data = data;
}
}
Stack, Acme.Collections.
Acme.Collections.Stack. : top,
Push Pop, Entry. Entry : next
data, . acme.cs,
csc /t:library acme.cs
( Main) acme.dll.
(Intermediate Language,
IL), . IL
JIT- .NET CLR.
,
, , C# #include.
C# , ,
. ,
Acme.Collections.Stack, acme.dll:
using System;
using Acme.Collections;
class Test
{
static void Main() {
Stack s = new Stack();
s.Push(1);
s.Push(10);
s.Push(100);
Console.WriteLine(s.Pop());
Console.WriteLine(s.Pop());
Console.WriteLine(s.Pop());
}
}
test.cs, acme.dll
/r:
(Microsoft Corp.), 1999-2007. .
test.exe,
:
100
10
1
C# .
C# .
.
. C#
, , ,
. C#
.
.
1.3
C# : .
,
().
. , ,
.
. (
ref out).
C# , ,
. C# , ,
.
C#.
enum E {...}
struct S {...}
,
null
: object
: string
class C {...}
interface I {...}
- , int[] int[,]
C#.
sbyte
128...127
16
short
32 768...32 767
32
int
64
long
9 223 372 036 854 775 808...9 223 372 036 854 775 807
byte
0...255
16
ushort
0...65 535
32
uint
64
ulong
32
float
64
double
128
decimal
C# .
. C#
: , , , .
, - () - (,
). ,
,
.
, , , - -.
, ,
. .
object.
-.
, , - .
.
.
. ,
.
, . ,
, -
.
, , ,
.
,
. ,
.
.
C# - . ,
, . ,
. , int[]
6
//
//
object (
), ,
. object , ,
, .
.
C#
. ,
, object.
C# , , ,
. .
, , ,
.
null
object
,
,
, ,
,
,
1.4
. ,
. : +, -, *, / new. :
, , .
, ,
. , x + y * z x + (y *
z), * +.
.
,
.
C#
.
.
x.m
x(...)
x[...]
x++
x--
new T(...)
new T(...){...}
new {...}
new T[...]
typeof(T)
System.Type T
checked(x)
checked
unchecked(x)
unchecked
default(T)
delegate {...}
( )
+x
-x
!x
~x
++x
--x
(T)x
x T
x*y
x/y
x%y
x+y
, ,
xy
x << y
x >> y
x<y
x>y
x <= y
x >= y
x is T
true, x T;
false
x as T
x, T,
null, x , T
x == y
x != y
AND
x&y
AND, AND
XOR
x^y
XOR,
XOR
OR
AND
x|y
OR, OR
x && y
y , x
true
OR
x || y
y , x
false
null
X ?? y
x null, y;
x
x?y:z
x true, y;
x false, z
x=y
x = y
;
:
(-)
1.5
. C#
, .
,
. , { }.
.
.
: , new,
10
= ,
++ --.
- . if switch.
.
while, do, for foreach.
.
break, continue, goto, throw, return yield.
try...catch ,
. try...finally ,
( , ).
checked unchecked
.
lock ,
.
using ,
.
C# .
if
11
12
switch
while
do
for
foreach
break
continue
goto
return
yield
throw
try
checked
unchecked
//
//
13
lock
class Account
{
decimal balance;
public void Withdraw(decimal amount) {
lock (this) {
if (amount > balance) {
throw new Exception(" ");
}
balance -= amount;
}
}
}
using
1.6
C# . ,
() ( -).
( ).
,
, .
. ,
, , ( ),
, . ,
, { }.
Point:
public class Point
{
public int x, y;
new,
, .
Point,
:
Point p1 = new Point(0, 0);
Point p2 = new Point(10, 20);
, , , . C#
( ) .
14
1.6.1
.
. ( ).
, .
, ,
1.6.2
,
, .
. .
public
protected
internal
protected internal
private
1.6.3
.
.
. Pair
TFirst TSecond:
15
, ,
. , .
:
Pair<int,string> pair = new Pair<int,string> { First = 1, Second = "" };
int i = pair.First;
// TFirst is int
string s = pair.Second; // TSecond is string
, Pair<int,string>,
.
1.6.4
,
. , ,
object. Point3D Point,
Point object:
public class Point
{
public int x, y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
}
public class Point3D: Point
{
public int z;
. ,
( ).
, .
Point3D x y Point. ,
Point3D : x, y z.
.
,
. , Point
Point, Point3D:
Point a = new Point(10, 20);
Point b = new Point3D(10, 20, 30);
1.6.5
, .
16
, static, .
.
.
, static, .
.
Color r,
g b. Black, White, Red, Green Blue:
public class Color
{
public static readonly
public static readonly
public static readonly
public static readonly
public static readonly
Color
Color
Color
Color
Color
private byte r, g, b;
readonly. readonly
.
1.6.6
, ,
. .
.
(, ) ,
, , ,
, . ,
void.
, , ,
. ,
.
, .
, , , .
.
.
, .
: , ,
.
.
, ,
. , .
(Microsoft Corp.), 1999-2007. .
17
, . ,
, .
, .
ref.
ref.
using System;
class Test
{
static void Swap(ref int x, ref int y) {
int temp = x;
x = y;
y = temp;
}
// : "2 1"
.
, ,
. out.
out.
using System;
class Test
{
static void Divide(int x, int y, out int result, out int remainder) {
result = x / y;
remainder = x % y;
}
static void Main() {
int res, rem;
Divide(10, 3, out res, out rem);
Console.WriteLine("{0} {1}", res, rem);
}
// : "3 1"
.
params.
.
. Write WriteLine System.Console
. .
public class Console
{
public static void Write(string fmt, params object[] args) {...}
public static void WriteLine(string fmt, params object[] args) {...}
}
...
, .
,
18
.
.
Console.WriteLine("x={0} y={1} z={2}", x, y, z);
:
string s = "x={0} y={1} z={2}";
object[] args = new object[3];
args[0] = x;
args[1] = y;
args[2] = z;
Console.WriteLine(s, args);
, .
, .
. ,
.
i j .
using System;
class Squares
{
static void Main() {
int i = 0;
int j;
while (i < 10) {
j = i * i;
Console.WriteLine("{0} x {0} = {1}", i, j);
i = i + 1;
}
}
}
C#
. , i (. ) ,
, i
.
return .
, void, return . ,
void , return ,
.
, static, .
.
, static, .
, . ,
, this.
this .
Entity , .
(Microsoft Corp.), 1999-2007. .
19
class Entity
{
static int nextSerialNo;
int serialNo;
public Entity() {
serialNo = nextSerialNo++;
}
public int GetSerialNo() {
return serialNo;
}
public static int GetNextSerialNo() {
return nextSerialNo;
}
Entity (, , ,
). Entity ( )
.
, serialNo,
nextSerialNo.
GetNextSerialNo SetNextSerialNo
nextSerialNo, serialNo
.
Entity.
using System;
class Test
{
static void Main() {
Entity.SetNextSerialNo(1000);
Entity e1 = new Entity();
Entity e2 = new Entity();
Console.WriteLine(e1.GetSerialNo());
Console.WriteLine(e2.GetSerialNo());
Console.WriteLine(Entity.GetNextSerialNo());
// : "1000"
// : "1001"
// : "1002"
, SetNextSerialNo GetNextSerialNo
, GetSerialNo .
,
virtual,
. virtual , .
,
, .
.
20
.
override,
. .
,
.
.
abstract ,
abstract.
.
Expression,
, : Constant, VariableReference Operation,
, .
( , 4.6.
.)
using System;
using System.Collections;
public abstract class Expression
{
public abstract double Evaluate(Hashtable vars);
}
public class Constant: Expression
{
double value;
public Constant(double value) {
this.value = value;
}
public override double Evaluate(Hashtable vars) {
return value;
}
}
public class VariableReference: Expression
{
string name;
public VariableReference(string name) {
this.name = name;
}
public override double Evaluate(Hashtable vars) {
object value = vars[name];
if (value == null) {
throw new Exception(" : " + name);
}
return Convert.ToDouble(value);
}
}
21
. , x + 3
.
Expression e = new Operation(
new VariableReference("x"),
'+',
new Constant(3));
Evaluate Expression
double. Hashtable,
( ) ( ). Evaluate
. ,
.
Constant Evaluate .
VariableReference -
. Operation
( Evaluate),
.
Expression x * (y + 2)
x y.
using System;
using System.Collections;
class Test
{
static void Main() {
Expression e = new Operation(
new VariableReference("x"),
'*',
new Operation(
new VariableReference("y"),
'+',
new Constant(2)
)
);
22
2
Hashtable vars = new Hashtable();
vars["x"] = 3;
vars["y"] = 5;
Console.WriteLine(e.Evaluate(vars));
// : "21"
vars["x"] = 1.5;
vars["y"] = 9;
Console.WriteLine(e.Evaluate(vars));
// : "16.5"
.
.
, , ,
, .
. Main ,
.
class Test
{
static void F() {
Console.WriteLine("F()");
}
static void F(object x) {
Console.WriteLine("F(object)");
}
static void F(int x) {
Console.WriteLine("F(int)");
}
static void F(double x) {
Console.WriteLine("F(double)");
}
static void F<T>(T x) {
Console.WriteLine("F<T>(T)");
}
static void F(double x, double y) {
Console.WriteLine("F(double, double)");
}
// F()
// F(int)
// F(double)
// F(object)
// F(double)
// F(object)
// F<T>(T)
// F(double, double)
,
.
23
1.6.7 -
, , -.
, -.
-, C#: , ,
, , .
List<T>,
. .
public class List<T>
{
const int defaultCapacity = 4;
T[] items;
int count;
24
2
public T this[int index] {
get {
return items[index];
}
set {
items[index] = value;
OnChanged();
}
}
C# .
, ,
. , ,
.
,
. , static,
. .
. , List<T>
: , int.
(Microsoft Corp.), 1999-2007. .
25
new.
List<string> List.
List<string> list1 = new List<string>();
List<string> list2 = new List<string>(10);
, .
, .
, .
. ,
,
. , , .
, ,
.
,
, get set, { }. ,
get set, .
, get, .
, set, .
get ,
. ,
, get
.
set value,
.
++ -- set ,
.
List<T> : Count Capacity (
). .
List<string> names = new List<string>();
names.Capacity = 100;
// set
int i = names.Count;
// get
int j = names.Capacity;
// get
, C#
. , static, .
.
.
virtual, abstract override, .
, ( ).
, this,
, [ ].
. , ,
. .
List , int.
List int.
:
26
2
List<string> names = new List<string>();
names.Add("");
names.Add("");
names.Add("");
for (int i = 0; i < names.Count; i++) {
string s = names[i];
names[i] = s.ToUpper();
}
. ,
, .
, .
,
event.
,
, , . ,
, .
, null.
List<T> Changed,
. Changed OnChanged,
, null (. . ).
, .
.
.
+=, -=.
Changed List<string> .
using System;
class Test
{
static int changeCount;
static void ListChanged(object sender, EventArgs e) {
changeCount++;
}
static void Main() {
List<string> names = new List<string>();
names.Changed += new EventHandler(ListChanged);
names.Add("");
names.Add("");
names.Add("");
Console.WriteLine(changeCount);
// : "3"
}
}
, ,
add remove,
set .
,
. : ,
(Microsoft Corp.), 1999-2007. .
27
.
public static.
List<T> : == !=,
, List. ,
List<T>
Equals. ==
List<int>.
using System;
class Test
{
static void Main() {
List<int> a = new List<int>();
a.Add(1);
a.Add(2);
List<int> b = new List<int>();
b.Add(1);
b.Add(2);
Console.WriteLine(a == b);
// : "True"
b.Add(3);
Console.WriteLine(a == b);
// : "False"
}
}
Console.WriteLine True,
. List<T>
==, Console.WriteLine False, a b
List<int>.
, ,
.
. .
. , .
.
.
using.
1.7
, , -
-. , ,
. ,
.
.
object.
,
. ,
-.
. ,
28
Point .
struct Point
{
public int x, y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
}
. Point
.
new,
.
( ),
.
. ,
, .
.
. , ,
Point .
Point a = new Point(10, 10);
Point b = a;
a.x = 20;
Console.WriteLine(b.x);
Point , 20, a b .
Point , 10, a b
, a.x.
, . -,
, , -
,
. -,
( ref out),
.
(Microsoft Corp.), 1999-2007. .
29
1.8
, ,
.
,
.
,
.
new. new
,
. 0 Length - 1. new
(
null ).
int,
.
using System;
class Test
{
static void Main() {
int[] a = new int[10];
for (int i = 0; i < a.Length; i++) {
a[i] = i * i;
}
for (int i = 0; i < a.Length; i++) {
Console.WriteLine("a[{0}] = {1}", i, a[i]);
}
}
}
. C#
.
, .
-, - .
int[] a1 = new int[10];
int[,] a2 = new int[10, 5];
int[,,] a3 = new int[10, 5, 2];
, int[],
null.
.
30
new
, , { }.
int[], .
int[] a = new int[] {1, 2, 3};
,
{ }. ,
.
int[] a = {1, 2, 3};
:
int[] t = new int[3];
t[0] = 1;
t[1] = 2;
t[2] = 3;
int[] a = t;
1.9
, .
, , .
. ,
, .
.
IComboBox ITextBox IListBox.
interface IControl
{
void Paint();
}
interface ITextBox: IControl
{
void SetText(string text);
}
interface IListBox: IControl
{
void SetItems(string[] items);
}
interface IComboBox: ITextBox, IListBox {}
.
EditBox IControl IDataBound.
interface IDataBound
{
void Bind(Binder b);
}
public class EditBox: IControl, IDataBound
{
public void Paint() {...}
public void Bind(Binder b) {...}
}
,
. :
(Microsoft Corp.), 1999-2007. .
31
, ,
. ,
IControl IDataBound .
EditBox, .
object obj = new EditBox();
IControl control = (IControl)obj;
IDataBound dataBound = (IDataBound)obj;
.
, IControl.Paint, EditBox,
EditBox
IControl.
EditBox editBox = new EditBox();
editBox.Paint();
IControl control = editBox;
control.Paint();
// :
//
1.10
,
. Color,
: Red, Green Blue.
using System;
enum Color
{
Red,
Green,
Blue
}
32
2
class Test
{
static void PrintColor(Color color) {
switch (color) {
case Color.Red:
Console.WriteLine("");
break;
case Color.Green:
Console.WriteLine("");
break;
case Color.Blue:
Console.WriteLine("");
break;
default:
Console.WriteLine(" ");
break;
}
}
static void Main() {
Color c = Color.Red;
PrintColor(c);
PrintColor(Color.Blue);
}
}
, .
, int.
.
, .
,
.
Alignment,
sbyte.
enum Alignment: sbyte
{
Left = -1,
Center = 0,
Right = 1
}
,
, .
.
, ,
, .
( )
. :
int i = (int)Color.Blue;
Color c = (Color)2;
// int i = 2;
// Color c = Color.Blue;
, .
,
. , 0
33
. ,
.
Color c = 0;
1.11
. ,
.
, . ,
, -
.
Function.
using System;
delegate double Function(double x);
class Multiplier
{
double factor;
public Multiplier(double factor) {
this.factor = factor;
}
public double Multiply(double x) {
return x * factor;
}
}
class Test
{
static double Square(double x) {
return x * x;
}
static double[] Apply(double[] a, Function f) {
double[] result = new double[a.Length];
for (int i = 0; i < a.Length; i++) result[i] = f(a[i]);
return result;
}
static void Main() {
double[] a = {0.0, 0.5, 1.0};
double[] squares = Apply(a, Square);
double[] sines = Apply(a, Math.Sin);
Function ,
double. Apply Function
double[] double[], . Main Apply
double[].
(, Square Math.Sin
), (, m.Multiply ). ,
34
, .
this.
,
, .
. ,
Multiplier:
double[] doubles = Apply(a, (double x) => x * 2.0);
, ,
. ,
, .
1.12
, C# ,
. , public,
protected, internal private. C#
.
.
HelpAttribute
.
using System;
public class HelpAttribute: Attribute
{
string url;
string topic;
public HelpAttribute(string url) {
this.url = url;
}
public string Url {
get { return url; }
}
public string Topic {
get { return topic; }
set { topic = value; }
}
}
System.Attribute,
.NET Framework. ,
.
Attribute, . ,
HelpAttribute .
[Help("http://msdn.microsoft.com/.../MyClass.htm")]
public class Widget
{
[Help("http://msdn.microsoft.com/.../MyClass.htm", Topic = "Display")]
public void Display(string text) {}
}
35
.
(, Topic ).
.
using System;
using System.Reflection;
class Test
{
static void ShowHelp(MemberInfo member) {
HelpAttribute a = Attribute.GetCustomAttribute(member,
typeof(HelpAttribute)) as HelpAttribute;
if (a == null) {
Console.WriteLine(" {0}", member);
}
else {
Console.WriteLine(" {0}:", member);
Console.WriteLine(" Url={0}, Topic={1}", a.Url, a.Topic);
}
}
, ,
. ,
.
36
2.
2.1
C# ,
(9.1). .
,
.
UTF-8.
:
:
.
: .
: .
2.2
C#,
. (2.2.2) ,
, , , .
(2.2.3) , ,
, C#.
2.2.1
.
.
,
.
,
.
,
. , :
_while:
while ( _ ) _
_while while, (,
_, ) _.
,
. , :
_:
_ , _
. , ,
.
. :
:
{ _ }
:
:
{ }
{ _ }
_, {
}.
, , one of (
) , .
. , :
__:
F f D d M m
:
__:
F
f
D
d
M
m
2.2.2
C# 2.3, 2.4 2.5.
. ,
(2.4), (2.3.3), (2.3.2)
(2.5).
C#
(2.3).
2.2.3
C# , .
,
. ,
C#.
C# _
(9.1).
2.3
C#.
C# .
:
_
38
_:
__
_ __
__:
_ _
_
_:
_
_ _
_:
C# :
(2.3.1), (2.3.3), (2.3.2), (2.4)
(2.5).
C# (2.2.3).
C# ,
. ,
;
,
C#.
, .
, // ,
, /.
2.3.1
C#.
_:
(U+000D)
(U+000A)
(U+000D) (U+000A)
(U+0085)
(U+2028)
(U+2029)
, ,
,
C# :
CTRL-Z ( U+001A),
;
(U+000D) ,
( U+000D),
(U+000A), (U+2028)
(U+2029).
39
2.3.2
: .
// .
/* */.
.
:
_
__
_:
// _
_:
_
_ _
_:
, __
__:
(U+000D)
(U+000A)
(U+0085)
(U+2028)
(U+2029)
__:
/* ___ /
___:
___
___ ___
___:
/
_____
:
*
*
_____:
, / *
. /* */
//, // /*
.
.
/* Hello, world
hello, world
*/
class Hello
{
static void Main() {
System.Console.WriteLine("hello, world");
}
}
40
.
// Hello, world
// hello, world
//
class Hello //
{
static void Main() { // "Main"
System.Console.WriteLine("hello, world");
}
}
.
2.3.3
Zs ( ),
, .
:
Zs
(U+0009)
(U+000B)
(U+000C)
2.4
: , , ,
. ,
.
:
_
_
_
_
_
___
2.4.1
.
(2.4.2),
(2.4.4.4) (2.4.4.5).
(,
, ).
__:
\u 16__ 16__ 16__ 16__
\U 16__ 16__ 16__ 16__ 16__ 16__ 16__ 16__
(escape- )
, , \u
\U. C# 16-
, U+10000 U+10FFFF
.
0x10FFFF .
(Microsoft Corp.), 1999-2007. .
41
. , \u005Cu005C
\u005C, \. \u005C \.
class Class1
{
static void Test(bool \u0066) {
char c = '\u0066';
if (\u0066)
System.Console.WriteLine(c.ToString());
}
}
\u0066, escape- f.
:
class Class1
{
static void Test(bool f) {
char c = 'f';
if (f)
System.Console.WriteLine(c.ToString());
}
}
2.4.2
,
15 , :
( C), escape , @
, .
:
_
@ ___
_:
___, _
___:
__ __
__:
_
_ ( U+005F)
__:
__
__ __
__:
_
_
_
_
__
42
_:
Lu, Ll, Lt, Lm, Lo Nl
__, Lu, Ll, Lt, Lm,
Lo Nl
_:
Mn Mc
__, Mn Mc
_:
Nd
__, Nd
_:
Pc
__, Pc
__:
Cf
__, Cf
.
, 3.0, 4.5.
: identifier1, _identifier2, @if.
,
C , 15 .
C ,
.
@ ,
. @
,
, . @
. @ ,
, ,
.
class @class
{
public static void @static(bool @bool) {
if (@bool)
System.Console.WriteLine("true");
else
System.Console.WriteLine("false");
}
}
class Class1
{
static void M() {
cl\u0061ss.st\u0061tic(true);
}
}
class static,
bool. , ,
43
, cl\u0061ss ,
, @class.
,
:
@, ;
___
;
__ .
, ( U+005F),
. ,
, .
2.4.3
.
@.
_:
abstract
byte
class
delegate
event
fixed
if
internal
new
override
readonly
short
struct
try
unsafe
volatile
as
base
bool
break
case
catch
char
checked
const
continue
decimal
default
do
double
else
enum
explicit
extern
false
finally
float
for
foreach
goto
implicit
in
int
interface
is
lock
long
namespace
null
object
operator
out
params
private
protected public
ref
return
sbyte
sealed
sizeof
stackalloc static
string
switch
this
throw
true
typeof
uint
ulong
unchecked
ushort
using
virtual
void
while
,
. , get set
(10.7.2). , get set,
,
.
2.4.4
.
:
_
_
_
_
_
_null
44
: true false.
_:
true
false
_ bool.
, int, uint, long ulong.
: .
_:
__
__
__:
_ __
_:
_
_ _
_:
0 1 2 3 4 5 6 7 8 9
__:
U u L l UL Ul uL ul LU Lu lU lu
__:
0x 16__ __
0X 16__ __
16__:
16__
16__ 16__
16__:
0 1 2 3 4 5 6 7 8 9 A B C D E F a b c d e f
, ,
: int, uint, long ulong;
U u, ,
: uint ulong;
L l, ,
: long ulong;
ulong,
.
L l long, l
1.
int long
:
(Microsoft Corp.), 1999-2007. .
45
__ 2147483648 (231)
__ ,
(7.6.2), int
2147483648 (231). __
uint;
__ 9223372036854775808 (263)
__ __ L l
, (7.6.2),
long 9223372036854775808 (263).
__ ulong.
float, double decimal.
_:
_ . _ _
__
. _ _ __
_ _ __
_ __
_:
e _
E _
:
+ -
__:
F f D d M m
__ , double.
:
,
.
float double
IEEE.
,
. , 1.3F , 1.F .
46
, 'a'.
_:
' '
:
_
__
_escape-
escape-_
_:
, ' (U+0027), \ (U+005C) __
__:
\' \" \\ \0 \a \b \f \n \r \t \v
_escape-:
\x 16__ 16__ 16__ 16__
, (\) , : ', ", \, 0,
a, b, f, n, r, t, u, U, x, v. .
escape- ,
, \x.
, , U+FFFF,
.
(2.4)
U+0000 U+FFFF.
,
.
Escape
\'
0x0027
\"
0x0022
\\
0x005C
\0
Null
0x0000
\a
0x0007
\b
0x0008
\f
0x000C
\n
0x000A
\r
0x000D
\t
0x0009
\v
0x000B
_ char.
47
1.1.1.1
C# :
.
, ,
"hello", ( \t
), escape- escape-
.
@ ,
.
@"hello".
, __.
, , escape-
escape- .
.
_:
__
__
__:
" ___ "
___:
___
___ ___
___:
____
__
_escape-
escape-_
____:
, " (U+0022), \ (U+005C) __
__:
@" ___ "
___:
___
___ ___
___:
____
__
____:
, "
__:
""
, (\)
___, : ', ", \, 0, a, b,
f, n, r, t, u, U, x, v. .
string a = "hello, world";
string b = @"hello, world";
48
// hello, world
// hello, world
(Microsoft Corp.), 1999-2007. .
2
string c = "hello \t world";
string d = @"hello \t world";
// hello
world
// hello \t world
// \\server\share\file.txt
// \\server\share\file.txt
string i = "one\r\ntwo\r\nthree";
string j = @"one
two
three";
. j
, . , ,
, .
escape-
, "\x123"
123. , 12
3, "\x00123" "\x12" + "3".
_ string.
.
, (7.9.7),
, . ,
class Test
{
static void Main() {
object a = "hello";
object b = "hello";
System.Console.WriteLine(a == b);
}
}
True, .
null
_null:
null
_null .
2.4.5
.
, . , a + b
+ a b.
.
___:
{
+
=
->
&=
}
<
==
|=
[
*
>
!=
^=
]
/
?
<=
<<
(
%
??
>=
<<=
)
&
::
+=
=>
.
|
++
-=
,
^
-*=
:
!
&&
/=
;
~
||
%=
49
shift_c:
>| >
_shift_:
>|>=
shift_c _shift_ , ,
, ( )
. ,
__ (10.1.3).
2.5
,
.
C C++. C# ;
.
_:
_
__
_
_
_
_pragma
:
#define #undef, , ,
(2.5.3);
#line, ,
(2.5.7);
#pragma, (2.5.8).
# . #
# .
, #define, #undef, #if, #elif, #else, #endif #line,
. ( /* */)
, .
C#. ,
C#. ,
:
#define A
#undef B
50
2
class C
{
#if A
void F() {}
#else
void G() {}
#endif
#if B
void H() {}
#else
void I() {}
#endif
}
, :
class C
{
void F() {}
void I() {}
}
, , ,
.
2.5.1
, #if, #elif, #else #endif,
(2.5.2) .
_:
___, true false
: .
,
(
). #define ,
, . ,
#undef ,
. , #define #undef
.
true, false.
,
. ,
, false.
C#.
#define #undef .
2.5.2
#if #elif.
!, ==, !=, && ||, .
_:
__
(Microsoft Corp.), 1999-2007. .
51
__:
__
__ || __
__:
__
__ && __
__:
__
__ == __
__ != __
__:
__
! __
__:
true
false
_
( _ )
true, false.
.
, (7.18), ,
, ,
.
2.5.3
.
_:
# define _ __
# undef _ __
__:
_ _
#define ,
, . #undef
, ,
.
#define #undef (2.3.3)
, . ,
#define #undef .
#define Enterprise
#if Professional || Enterprise
#define Advanced
#endif
52
2
namespace Megacorp.Data
{
#if Advanced
class PivotTable {...}
#endif
}
, #define (
namespace) .
, #define
:
#define A
namespace N
{
#define B
#if B
class Class1 {}
#endif
}
#define , -
#undef .
A , .
#define A
#define A
#undef .
A,
; #undef , .
#define A
#undef A
#undef A
2.5.4
.
__:
__if __elif __else _endif
__if:
# if _ __
_
__elif:
__elif
__elif __elif
__elif:
# elif _ __
_
__else:
# else __ _
_endif:
# endif __
(Microsoft Corp.), 1999-2007. .
53
_:
_
_
_:
__
_ __
__:
_ _
_
_:
__ _
__:
_, #
, ,
, , #if, #elif,
#else #endif. .
.
, .
__ _
:
_ #if #elif ,
true. true, _
;
_ false #else, _
#else;
_ .
_, , _: ,
, ;
, .
_, , _:
,
; ,
, .
_, _,
_ ( #if...#endif #region...#endregion)
_.
, :
#define Debug
#undef Trace
54
//
//
2
class PurchaseTransaction
{
void Commit() {
#if Debug
CheckConsistency();
#if Trace
WriteToLog(this.ToString());
#endif
#endif
CommitHelper();
}
}
,
. , ,
#else:
#define Debug
//
class PurchaseTransaction
{
void Commit() {
#if Debug
CheckConsistency();
#else
/*
#endif
}
}
,
.
,
. , :
class Hello
{
static void Main() {
System.Console.WriteLine(@"hello,
#if Debug
world
#else
Nebraska
#endif
");
}
}
:
hello,
#if Debug
world
#else
Nebraska
#endif
_.
55
#if X
/*
#else
/* */ class Q { }
#endif
( class Q { }), , X
. X , - #if
#endif. X , (#if, #else #endif) .
2.5.5
,
, .
_:
# error _
# warning _
_:
_
_ _
#warning Code review needed before check-in
#if Debug && Retail
#error A build can't be both debug and retail
#endif
class Test {...}
:
56
2
#if true
...
#endif
2.5.7
,
, .
,
C# .
_:
# line _ __
_:
_ _
_
default
hidden
_:
" __ "
__:
__
__ __
__:
_, "
#line
. #line, _,
default, (
, ).
#line default #line.
, #line
.
#line hidden , ,
. #line hidden
#line ( #line hidden)
. .
, _ , escape ; \
_.
2.5.8 pragma
#pragma
. , #pragma,
.
_pragma:
# pragma __pragma
__
__pragma:
___pragma
(Microsoft Corp.), 1999-2007. .
57
C# #pragma .
#pragma.
C#, C#
#pragma; ,
.
pragma warning
#pragma warning
.
___pragma:
warning _
warning _ _
_:
disable
restore
_:
_
_ , _
#pragma warning, ,
. #pragma warning, ,
, .
#pragma warning disable
.
#pragma warning restore
, . ,
, #pragma warning restore (
) .
#pragma warning
, ,
C# .
using System;
class Program
{
[Obsolete]
static void Foo() {}
static void Main() {
#pragma warning disable 612
Foo();
#pragma warning restore 612
}
}
58
3.
3.1 .
.
.
, .
. ,
, . ,
, , ,
.
, ,
.
.
, ,
. Main
:
static void Main() {...}
static void Main(string[] args) {...}
static int Main() {...}
static int Main(string[] args) {...}
, int.
(3.2).
. ,
string[].
string[], ,
. string[] null, ,
.
C# ,
- , .
Main,
.
Main ,
string[].
.
Main,
. Main
(, ).
C# . ,
(3.5.1) (10.3.5),
, , ,
, . , ,
. . ,
.
.
.
3.2 .
.
int,
.
, , .
void,
(}), , return
0.
,
, , (,
GC.SuppressFinalize).
3.3 .
C# . C#
(9),
. (9.6)
(10), (10.14), (13), (14) (15).
, , . ,
(10.4), (10.5), (10.6),
(10.7), (10.8), (10.9), (10.10), (10.11),
(10.12), (10.13) (10.3.8).
, .
(3.6), ,
, . ,
. ,
.
, .
___
__
, .
___
__
.
, .
__,
__, __ _.
,
. ,
. ,
. , ,
60
,
(3.6). ,
,
.
. .
.
_.
.
___.
switch_ .
_,
_goto.
.
.
, , . ,
, ,
, , , , , , ,
. .
( ).
(3.7).
(14.3) ,
_.
,
.
:
namespace Megacorp.Data
{
class Customer
{
...
}
}
(Microsoft Corp.), 1999-2007. .
61
namespace Megacorp.Data
{
class Order
{
...
}
}
,
Megacorp.Data.Customer
Megacorp.Data.Order. ,
,
.
, .
F G , i
. H I ,
i .
class A
{
void F() {
int i = 0;
if (true) {
int i = 1;
}
} void G() {
if (true) {
int i = 0;
}
int i = 1;
}
void H() {
if (true) {
int i = 0;
}
if (true) {
int i = 1;
}
}
void I() {
for (int i = 0; i < 10; i++)
H();
for (int i = 0; i < 10; i++)
H();
}
3.4 .
.
, , .,
.
, .
, , ,
, .
,
62
, ,
. ,
(3.5.1), ,
(3.7.1.2).
3.4.1 .
, ,
. ,
.
, ,
. ,
.
. ,
, .
3.4.2 .
, , ,
System.ValueType object.
.
sbyte System.SByte.
byte System.Byte.
short System.Int16.
ushort System.UInt16.
int System.Int32.
uint System.UInt32.
long System.Int64.
ulong System.UInt64.
char System.Char.
float System.Single.
double System.Double.
decimal System.Decimal.
bool System.Boolean.
3.4.3 .
, , ,
System.Enum
System.ValueType object.
3.4.4 .
, , ,
( object, ). , ,
(Microsoft Corp.), 1999-2007. .
63
, , , , , , ,
, .
.
, , , , ,
, , , ,
.
object string ,
.
object System.Object.
string System.String.
3.4.5 .
,
. object, , -
(13.2). object
(7.3).
3.4.6 .
, System.Array.
3.4.7 .
, System.Delegate.
3.5 .
.
(3.5.1)
, .
, . ,
, . ,
, , (3.5.2) .
3.5.1 .
.
, public .
public .
, protected .
protected -
, -.
, internal .
internal .
( ),
protected internal .
protected internal ,
-.
64
, private .
private -.
, ,
. ,
, , ,
.
public.
.
, ,
public internal, internal.
,
private. ( , ,
, , ,
,
public internal.)
public.
.
public.
.
3.5.2 .
( ) ,
. ,
, , .
,
, ,
{" "} _,
_, _ _ (, , ,
).
(, object, int double) .
T (4.4.3), P,
.
T public, T P
, P.
T internal, T P.
,
, .
65
T<A1, ...,AN>
T A1, ...,AN.
M, T P,
(, M ).
M public, M
T.
M protected internal, D
P , T, P.
M T D.
M protected, D T
, T. M
T D.
M internal, M
T P.
M private, M T.
,
, . ,
, ,
.
, M
.
, M ( ),
.
, M public, .
, M protected internal, ,
, M,
, , M,
(3.5.3).
, M protected, ,
, M, ,
, M,
(3.5.3).
, M internal, ,
, M.
, M private, ,
, M.
, .
public class A
{
public static int X;
internal static int Y;
private static int Z;
}
66
2
internal class B
{
public static int X;
internal static int Y;
private static int Z;
public class C
{
public static int X;
internal static int Y;
private static int Z;
}
private class D
{
public static int X;
internal static int Y;
private static int Z;
}
}
A A.X .
A.Z A.
B.C.Z B.C.
B.D.Z B.D.
,
. , , X
, A.X , -.
3.4 , ,
, .
.
, .
class A
{
int x;
static void F(B b) {
b.x = 1;
// Ok
}
}
class B: A
{
static void F(B b) {
b.x = 1;
// , x
}
}
(Microsoft Corp.), 1999-2007. .
67
B x A. ,
_ A. b.x A.F,
B.F.
3.5.3 .
protected , ,
protected internal , ,
, , .
.
,
.
B , M, D
B. _ D M .
_ _ M.
_ E.M , E T T
, T D , D.
_ base.M.
_ (10.11.1).
public class A
{
protected int x;
static void F(A a, B b) {
a.x = 1;
// Ok
b.x = 1;
// Ok
}
}
public class B: A
{
static void F(A a, B b) {
a.x = 1;
// , B
b.x = 1;
// Ok
}
}
A x A B,
A A . B
x A, A B.
class C<T>
{
protected T x;
}
68
2
class D<T>: C<T>
{
static void F() {
D<T> dt = new D<T>();
D<int> di = new D<int>();
D<string> ds = new D<string>();
dt.x = default(T);
di.x = 123;
ds.x = "test";
}
}
x , ,
.
3.5.4 .
C# ,
. , T M,
T M. , T
M, T , M.
.
, .
,
.
,
.
, .
, .
,
.
, .
, .
, .
,
.
,
.
class A {...}
public class B: A {...}
B , A , B.
,
class A {...}
69
public class B
{
A F() {...}
internal A G() {...}
public A H() {...}
}
H B , A
, .
3.6 .
, , .
, (,
) , .
, ,
, .
, params,
, .
(, )
, .
params,
.
,
.
params, .
,
. .
,
.
,
, , .
,
.
,
, ,
.
, .
, out ref , ,
, ref out.
, , ,
out ref, .
(, ) ref out
. (
C# Common Language Infrastructure
(CLI), , ref out.)
70
.
interface ITest
{
void F();
void F(int x);
void F(ref int x);
void F(out int x);
// F(int)
// F(ref int)
// F(out int)
// F(int, int)
// F(string)
// F()
// F(int)
// F(string[])
// F(string[])
3.7 .
, ,
. ,
( ,
3.3,
, ).
, ,
.
, ___ (9.5),
__ .
, ___
__ N, __
__ N, N
.
, __ ,
_, _ ___
.
__ -
. , __ ,
, .
, _ (9.4),
_____
__, _.
_
71
, _ __,
.
, _ ,
_ __, .
, __ _ (10.1),
_, ___ _
_.
, __ _
(11.1), _, ___
_ _.
, __ _
(13.1), _, ___
_ _.
, __ _ (15.1),
__, __
___ _.
, __ (10.1.6), _,
. , _
, (3.5.2) .
, __ (11.2), _,
.
, ___ (14.3),
__, .
, _ (10.6), _
_.
, _ (10.9),
__ _.
, _ (10.10),
_.
, _ (10.11),
_ _.
, _ (), __
_.
, __ (),
__.
, _ (8.4), ,
.
, __ (8.5.1),
, .
, _ switch (8.7.2),
_.
72
, __ (8.5.2),
, .
, _, .
, _foreach, _using,
_lock _, .
, ,
, . :
class A
{
void F() {
i = 1;
}
int i = 0;
}
F i .
,
__ ,
. :
class A
{
int i = 0;
void F() {
i = 1;
int i;
i = 2;
}
void G() {
int j = (j = 1);
}
// ,
//
void H() {
int a = 1, b = ++a;
}
//
F i ,
. ,
, . G
j j ,
__. H
__ ,
__ __.
,
.
,
, , ,
, ,
.
,
.
(Microsoft Corp.), 1999-2007. .
73
using System;
class A {}
class Test
{
static void Main() {
string A = "hello, world";
string s = A;
Type t = typeof(A);
//
Console.WriteLine(s);
Console.WriteLine(t);
//
// "hello, world"
// "A"
A A
A.
3.7.1 .
, .
, , ,
. ,
. , , .
,
.
.
.
,
.
class A
{
int i = 0;
void F() {
int i = 1;
}
void G() {
i = 1;
}
}
F i i, G
i .
,
.
class Outer
{
static void F(int i) {}
static void F(string s) {}
74
2
class Inner
{
void G() {
F(1);
F("Hello");
}
}
// Outer.Inner.F
// .
F(1) F, Inner, F
. F("Hello")
.
.
, ,
. .
, , , , ,
.
, , ,
, ( ,
).
, ,
( ).
, (10.10),
, .
.
,
.
class Base
{
public void F() {}
}
class Derived: Base
{
public void F() {}
}
// ,
F Derived .
, . ,
, Base F,
. ,
, ,
.
, ,
new:
class Base
{
public void F() {}
}
(Microsoft Corp.), 1999-2007. .
75
new , F Derived ,
.
.
class Base
{
public static void F() {}
}
class Derived: Base
{
new private static void F() {}
}
class MoreDerived: Derived
{
static void G() { F(); }
}
// Base.F Derived
// Base.F
F Derived F, Base, ,
F Derived , MoreDerived.
F() MoreDerived.G , Base.F.
3.8 .
C# __ .
__:
____
_:
____
__ :
_opt__
____ . _op_ _
__
__ ____,
. ____
__ ,
. (4.4.1)
__ ( ).
_ ____, .
____ _ ,
.
____ ,
9.7.
____ .
76
N.I
K , ____
(10.6),
(10.1.3) I, ____ .
, ____
, T (10.3.1),
,
( ):
K , T I,
____ .
, T
I K, ____
, .
, .
, , (, , , ,
, , , ,
),
____.
, N,
, ____,
( ), ,
.
K , I N.
, ____,
N,
__ __,
I ,
____ ,
.
____
I N.
, N I K.
K ,
____, N,
__
__, I
, ____ ,
.
77
, ____ ,
.
, , ____,
N.
o
K
__ __,
I ,
____ .
, ,
___,
I K, ____
, .
, ,
___,
I K,
____ ,
.
____ ,
.
, ____ N.I N.I<A1, ..., AK>.
N ____. N
, . N.I N.I<A1, ...,
AK> .
K N
I, ____
.
, N
I K, ____ ,
.
, N (, )
I K,
____ ,
. ,
.
N.I ____,
.
____ (10.1.1.3)
.
3.8.1 .
,
.
N .
78
N , N.
S.N, S ,
N .
, N ,
N, .
, .
.
class A {}
namespace X
{
class B
{
class C {}
}
namespace Y
{
class D {}
}
// A
// X
// X.B
// X.B.C
// X.Y
// X.Y.D
}
namespace X.Y
{
class E {}
}
// X.Y
// X.Y.E
3.9 .
C# ,
, .
. .
, ,
.
, ,
. C#
,
. ,
, ,
, ( )
.
, ,
(10.13) ( ) .
.
, ,
.
, , ,
, .
79
, , ,
.
, , C# ,
. , C#
, ,
,
.
System.GC. ,
( ) . .
, ,
.
using System;
class A
{
~A() {
Console.WriteLine("Destruct instance of A");
}
}
class B
{
object Ref;
public B(object o) {
Ref = o;
}
~B() {
Console.WriteLine("Destruct instance of B");
}
class Test
{
static void Main() {
B b = new B(new A());
b = null;
GC.Collect();
GC.WaitForPendingFinalizers();
}
}
A B.
, b null,
. :
Destruct instance of A ( A)
Destruct instance of B ( A)
Destruct instance of B
Destruct instance of A
80
,
.
. ,
using System;
class A
{
~A() {
Console.WriteLine("Destruct instance of A");
}
class B
{
public A Ref;
~B() {
Console.WriteLine("Destruct instance of B");
Ref.F();
}
class Test
{
public static A RefA;
public static B RefB;
static void Main() {
RefB = new B();
RefA = new A();
RefB.Ref = RefA;
RefB = null;
RefA = null;
// A B
GC.Collect();
GC.WaitForPendingFinalizers();
// B , A
if (RefA != null)
Console.WriteLine("RefA is not null");
}
, A
B, :
Destruct instance of A
Destruct instance of B
A.F
RefA is not null (RefA null)
, , , A , A
, A ( F) .
, ,
(Microsoft Corp.), 1999-2007. .
81
. B
, A, ,
Test.RefA. WaitForPendingFinalizers B , A
- Test.RefA.
,
, ,
.
System.IDisposable. ,
using (8.13).
3.10
C# ,
.
volatile, volatile,
. ,
, volatile (10.5.3), lock
(8.12) .
C#, .
.
, ,
.
(10.5.4 10.5.5).
volatile
(10.5.3). , ,
, ,
(
volatile).
(, )
.
82
4.
C# : .
,
. ,
.
:
_
_
_
.
. 18.2.
,
,
(). .
, , .
.
.
C# ,
. C# object,
.
object.
(4.3).
4.1
: . C#
, .
.
_:
_
_
_:
_
_
_
_:
_
bool
_:
_
___
decimal
_:
sbyte
byte
short
ushort
int
uint
long
ulong
char
___:
float
double
_:
__ ?
__:
_:
_
, null
, .
, null.
.
, ,
.
4.1.1 System.ValueType
System.ValueType, , ,
object. . ,
(10.1.1.2).
, System.ValueType _.
_, _.
4.1.2
, .
, .
84
_ ,
:
o
char '\x0000'.
float 0.0f.
double 0.0d.
decimal 0.0m.
bool false.
_ E 0 E.
_
, , null.
_ , HasValue
false, Value .
.
,
new.
. i j
.
class A
{
void F() {
int i = 0;
int j = new int();
}
}
,
. ,
(11.3.8).
4.1.3
, , ,
, , , , ,
. . 11.1.
4.1.4
C# , .
.
System (. ).
85
sbyte
System.SByte
byte
System.Byte
short
System.Int16
ushort
System.UInt16
int
System.Int32
uint
System.UInt32
long
System.Int64
ulong
System.UInt64
char
System.Char
float
System.Single
double
System.Double
bool
System.Boolean
decimal
System.Decimal
,
. , int , System.Int32, ,
System.Object. :
int i = int.MaxValue;
string s = i.ToString();
string t = 123.ToString();
// System.Int32.MaxValue constant
// System.Int32.ToString() instance method
// System.Int32.ToString() instance method
,
:
(2.4.4).
, 123 int, 'a' char. C#
.
.
, ,
. _
(7.18). , , ,
.
const (10.4).
, static readonly.
, ,
.
(6.4.3).
4.1.5
C# : sbyte, byte, short, ushort, int, uint, long, ulong char.
:
86
byte 8- 0 255.
ulong 64- 0
18446744073709551615.
, 32- 64 :
+ ~ T, T
int, uint, long ulong,
. , T.
T.
T, T int
long, .
, T.
T. ulong.
<< >> T, T
int, uint, long ulong,
. ,
T. T.
char , :
char. ,
, sbyte, byte ushort
char, sbyte, byte ushort char
.
char _
char. , (char)10 '\x000A' .
87
checked unchecked (7.5.12). checked
System.OverflowException. unchecked , ,
, .
4.1.6
C# : float double. float double
32- ( ) 64- ( )
IEEE 754 :
.
.
(7.7.2).
. , ,
. , 1.0 / 0.0
, 1.0 / 0.0 .
NaN. NaN
, .
s x m x 2e, s 1 1, m e
, float 0 < m < 224 149 e 104,
double 0 < m < 253 1075 e 970.
.
, ,
.
, double,
double. ,
double. double ( bool ).
,
float. float ( bool ).
, ,
.
, ,
:
,
.
,
.
88
,
NaN.
NaN,
NaN.
,
. ,
extended long double,
double.
.
( ).
, C#
.
- , .
x * y / z, ,
double, ,
double,
.
4.1.7 decimal
decimal 128- ,
. decimal 1,0 x 1028
7,9 x 1028 () 2829 .
decimal (1) s x c x10-e, s
0 1, c 0 c < 296, e
0 e 28. decimal ,
. decimal 96- ,
. decimal 1.0m
28 . decimal
1.0m 28 29 . float double,
decimal , 0,1. float
double ,
.
decimal,
decimal. (
) decimal.
decimal.
( , )
.
. ,
, (
). 0 0.
decimal
5 10-29, .
decimal ,
decimal, System.OverflowException.
decimal ,
. , ,
, decimal .
(Microsoft Corp.), 1999-2007. .
89
.
decimal.
decimal ,
.
4.1.8 bool
bool . bool : true
false.
bool . , bool
.
bool .
C C++ ,
false.
true. C#
,
null.
4.1.9
,
. :
byte, sbyte, short, ushort, int, uint, long ulong.
.
.
(14.1).
4.1.10
.
T?, T .
System.Nullable<T>.
.
, ,
System.Nullable<T> T? ( T), ,
(
struct). System.Nullable<T> T (10.1.5).
,
.
. , int?? string? .
T? , :
HasValue bool.
Value T.
, T?
, T. x T
new T?(x)
T?, Value x.
.
null T? (6.1.5) T T? (6.1.4).
4.2
: , ,
.
_:
_
_
_
_
_:
_
object
string
_:
_
_:
__ _
__:
_:
_
_ _
_:
[ _ ]
_:
,
_ ,
_:
_
(). null
.
4.2.1
, - ( ), (, , , , , ,
), .
, ,
.
__ (7.5.10.1).
(Microsoft Corp.), 1999-2007. .
91
. 10.
C# (. ).
System.Object
. . 4.2.2.
System.String
C#. . 4.2.3.
System.ValueType
. . 4.1.1.
System.Enum
. .14.
System.Array
. . 12.
System.Delegate
. . 15.
System.Exception
. . 16.
4.2.2
object . C#
object.
object
System.Object.
4.2.3
string ,
object. string , .
string (2.4.4.5).
string
System.String.
4.2.4
. , ,
. .
.
. 13.
4.2.5
, ,
.
,
.
. 12.
4.2.6
, .
.
C C++ .
, ,
92
, .
,
, .
. 15.
4.3
C#.
_ _
_ object .
,
.
4.3.1
_ _.
:
_ object.
_ System.ValueType.
__ _,
_.
_ _,
_.
_ System.Enum.
_, _,
System.Enum.
,
,
(6.1.9).
__
.
null ( HasValue false) _,
.
.
__,
,
:
sealed class Box<T>: System.ValueType
{
T value;
public Box(T t) {
value = t;
}
}
v T new Box<T>(v)
object. ,
93
int i = 123;
object box = i;
int i = 123;
object box = i;
Box<T> ,
. T
T. is T.
,
int i = 123;
object box = i;
if (box is int) {
Console.Write("Box contains an int");
}
Point p = new Point(10, 10);
object box = p;
p.x = 20;
Console.Write(((Point)box).x);
10,
( p box) p.
Point class, 20, p box
.
4.3.2
_ _.
:
object _.
System.ValueType _.
_ __,
_.
_ _,
_.
94
System.Enum _.
System.Enum _,
_.
,
,
(6.2.6).
__ ,
__.
.
_ null,
_.
, _.
, ,
box _ T
((Box<T>)box).value. ,
object box = 123;
int i = (int)box;
object box = new Box<int>(123);
int i = ((Box<int>)box).value;
__
__. null,
System.NullReferenceException. ,
System.InvalidCastException.
_
null
__, _.
, System.InvalidCastException.
4.4
,
.
( < >)
. , ,
. ,
.
_typeof (7.5.11).
(7.5.2)
(7.5.4).
____ ,
. , ,
, .
, :
95
namespace Widgets
{
class Queue {...}
class Queue<TElement> {...}
}
namespace MyApplication
{
using Widgets;
class X
{
Queue q1;
// Widgets.Queue
Queue<int> q2;
// Widgets.Queue
}
}
_ , .
, ,
(10.3.8.6):
class Outer<T>
{
public class Inner {...}
}
public Inner i;
// i Outer<T>.Inner
_ (18.2).
4.4.1
.
__:
< _ >
_:
_
_ , _
_:
(18) _ .
,
(10.1.5).
4.4.2
. ,
. :
, .
,
.
,
.
96
.
, ,
,
.
.
.
.
,
. , ,
, . ,
.
4.4.3
.
.
, .
,
, .
typeof (7.5.11).
4.4.4
,
(10.1.5).
where A, ,
:
, ,
C, .
, .
, A C
:
o
(6.1.1).
(6.1.6)
(6.1.7), A .
, A C.
( class), A
:
o
A , , . ,
System.ValueType System.Enum
.
A , (10.1.5).
( struct), A
:
97
A .
, System.ValueType System.Enum
.
A ,
(10.1.5).
new(), A
abstract .
, :
o
A ,
(4.1.2).
A ,
(10.1.5).
A ,
(10.1.5).
A , abstract,
public, .
A abstract (10.11.4).
,
.
,
. T D.
T , B<T>.
, E , List<T> IEnumerable
T.
class B<T> where T: IEnumerable {...}
class D<T>: class B<T> where T: IEnumerable {...}
class E<T>: B<List<T>> {...}
4.5
, ,
.
_:
, , ,
. :
(10.2.4)
(13.1.3).
( ). . 7.3.
( ). .
6.1.9 6.2.6.
98
null , ,
, (6.1.9).
default (7.5.13). ,
, , , ,
null == != (7.9.6).
new (7.5.10.1) ,
_ (10.1.5).
(7.5.4) (3.8)
.
_ (18.2).
, ,
. ,
. , ,
,
(4.4.2). , ,
, .
4.6
.
System.Linq.Expressions.Expression<D>, D .
Expression<D>.
D,
Expression<D>.
, .
.
,
.
Expression<D> ,
D.
,
. Func<int,int>,
Expression<Func<int,int>>:
Func<int,int> del = x => x + 1;
Expression<Func<int,int>> exp = x => x + 1;
//
//
del , x + 1,
exp , x => x + 1.
Expression<D>,
.
99
. ,
,
. - ,
.
Expression<D> Compile, D:
Func<int,int> del2 = exp.Compile();
, .
, , del del2 ,
:
int i1 = del(1);
int i2 = del2(1);
i1 i2 2.
100
5.
. , ,
. C#
, C# ,
. +
+ - -.
,
(5.3).
,
.
.
.
,
, .
5.1
C# : , ,
, , ,
. .
class A
{
public static int x;
int y;
x , y , v[0]
, a , b , c , i
.
5.1.1
, static, .
(10.12)
.
,
(5.2).
101
5.1.2
, static, .
, (
).
,
(5.2).
.
, ,
. ,
.
, . ,
,
. ,
.
5.1.3
.
(5.2).
.
5.1.4
, ref out, .
- (,
, ) ,
, , .
, , -
. , (7.14),
, ,
, .
.
5.1.5
, ref, .
.
, -
102
. ,
.
.
, 5.1.6.
(5.3),
- .
-
.
struct this
, struct (7.5.7).
5.1.6
, out, .
.
, -
. ,
.
.
, 5.1.5.
-
.
- ,
, .
-
.
-
(5.3) -
.
struct this ,
struct (7.5.7).
5.1.7
__,
, _for, _switch _using,
_foreach __catch _try.
,
. ,
for, switch, using, foreach __catch,
, , for, switch, using, foreach
__catch . (
, , for, switch, using, foreach
__catch.)
(7.14.4.1), ,
, , ,
, .
103
5.2
.
___, ,
___ (4.1.2).
_ null.
, ,
.
, .
5.3
-
, (5.3.3) ,
. ,
:
104
(5.3.1) .
(5.3.2)
, , ,
:
o
(7.16.1), .
(7.5.5) (7.5.10.1),
.
(8.5.1),
.
,
_struct .
_struct ,
.
, .
.
:
o
_struct
.
,
. , -
.
- ,
- ( return
-). , -
, -,
,
.
this _struct
, .
5.3.1
:
.
struct .
105
, catch foreach.
5.3.2
:
struct .
, this
struct.
, catch foreach.
5.3.3
, ,
, .
-,
. v
v -:
(8.1)
v :
. ,
v .
.
bool ( )
:
o
true.
, v ,
true, ,
false.
false.
, v ,
false, ,
true.
v .
106
v -.
v .
v
v
, . ,
v ,
.
,
(8.1).
v
checked, unchecked, if, while, do, for, foreach, lock, using switch
v ,
. v
,
. v
. ,
(8.1).
,
v ,
( , )
v , checked
unchecked.
stmt, expr:
v expr,
stmt;
v expr,
stmt;
stmt.
stmt , v
stmt, .
stmt ,
v
( ).
If
if :
if ( expr ) then-stmt else else-stmt
v expr,
stmt;
v expr,
then-stmt,
else-stmt, stmt, else ;
107
v
true expr,
then-stmt
else-stmt, stmt, else ;
v
false expr, else-stmt
then-stmt.
stmt ,
then-stmt;
v
then-stmt else-stmt stmt, else
.
switch
switch stmt expr:
v expr
v stmt;
v
switch
v expr.
while
while stmt :
while ( expr ) while-body
v expr,
stmt;
v expr,
while-body
stmt;
v
true expr,
while-body,
stmt;
v
false expr,
stmt,
while-body.
do
do stmt :
do do-body while ( expr ) ;
108
v
stmt do-body, stmt.
v expr,
do-body.
v expr,
stmt.
v
false expr,
stmt.
for
for
for ( for-initializer ; for-condition ; for-iterator ) embedded-statement
:
{
for-initializer ;
while ( for-condition ) {
embedded-statement ;
for-iterator ;
}
}
_for for ,
, _for
true.
break, continue goto
v ,
break, continue goto,
v .
throw
stmt
throw expr ;
v expr
v stmt.
return
stmt
return expr ;
v expr
v stmt.
v ,
:
expr
109
stmt
return ;
v ,
:
o
stmt
try-catch
stmt
try try-block
catch(...) catch-block-1
...
catch(...) catch-block-n
v try-block
v stmt.
v catch-block-i (
i) v stmt.
v stmt
, v try-block catch-block-i
( i 1 n).
try-finally
stmt
try try-block finally finally-block
v try-block
v stmt.
v finally-block
v stmt.
v stmt
:
o
v try-block
v finally-block
(, goto), try-block
, v
, v finally-block. (
v
, .)
try-catch-finally
try-catch-finally,
110
2
try try-block
catch(...) catch-block-1
...
catch(...) catch-block-n
, try-finally try-catch:
try {
try try-block
catch(...) catch-block-1
...
catch(...) catch-block-n
}
finally finally-block
, try (8.10)
.
class A
{
static void F() {
int i, j;
try {
goto LABEL;
// i, j
i = 1;
// i
}
catch {
// i, j
i = 3;
// i
}
finally {
// i, j
j = 5;
// j
}
// i j
LABEL:;
// j
}
foreach
foreach stmt
foreach ( type identifier in expr ) embedded-statement
v expr
v stmt.
111
v
_ stmt v
expr.
using
using stmt
using ( _ ) _
v resource-acquisition
v stmt.
v
_ v
_.
lock
lock stmt
lock ( expr ) _
v expr
v stmt.
v
_ v expr.
yield
yield return stmt
yield return expr ;
v expr
v stmt.
v stmt
v expr.
yield break .
: (7.5.1),
(7.5.2), (7.5.4), (7.5.8),
typeof (7.5.11) (7.5.13).
v
v .
: (7.5.3),
(7.5.6), (7.5.8),
(7.5.9, 7.6.5), (7.6.6), +, -, ~,
*, +, -, *, /, %, <<, >>, <, <=, >, >=, ==, !=, is, as, &, |, ^ (7.7, 7.8, 7.9,
7.10), (7.16.2), checked unchecked (7.5.12),
(7.5.13).
112
,
. , %
, .
,
. expr, expr1, expr2, ..., exprn,
:
v expr1
expr.
v expri (i )
expri-1.
v expr
exprn.
expr
_ ( arg1 , arg2 , , argn )
new type ( arg1 , arg2 , , argn )
v
_ v expr;
v arg1
v _;
v arg1
v expr;
argi v argi
ref out;
argi i
v argi v argi-1;
v out (, out v)
, v expr .
v expr v
argn;
(7.5.10.4), (7.5.10.2),
(7.5.10.3) (7.5.10.6)
, .
expr w = expr-rhs:
v expr-rhs
v expr.
w , v, v
expr .
113
v expr
v expr-rhs.
&&
expr expr-first && expr-second:
v expr-first
v expr;
v expr-second ,
v expr-first
true.
;
v expr
:
o
v expr-first , v
expr ;
, v expr-second
, v expr-first
false,
v expr ;
, v expr-second
true,
v expr
true;
, v expr-first
false, expr-second
false,
expr
false;
v expr .
:
class A
{
static void F(int x, int y) {
int i;
if (x >= 0 && (i = y) >= 0) {
// i
}
else {
// i
}
// i
}
}
i
if. if F i
, (i = y)
. , i
114
, x >= 0
false, i .
||
expr expr-first || expr-second:
v expr-first
v expr;
v expr-second ,
v expr-first
false.
;
v expr
:
o
v expr-first , v
expr ;
, v expr-second
, v expr-first
true,
v expr ;
, v expr-second
false,
v expr
false;
, v expr-first
true, expr-second
true,
v expr
true;
v expr .
class A
{
static void G(int x, int y) {
int i;
if (x >= 0 || (i = y) >= 0) {
// i
}
else {
// i
}
// i
}
}
i
if. if G i
, (i = y)
. , i
(Microsoft Corp.), 1999-2007. .
115
, x >= 0
true, i .
!
expr ! expr-operand:
v expr-operand
v expr.
v expr
:
o
v expr-operand ,
expr ;
v expr-operand ,
expr ;
v expr-operand
false, expr
true;
v expr-operand
true, expr
false.
??
expr expr-first ?? expr-second:
v expr-first
expr;
v expr-second
expr-first.
v expr
:
o
v expr-cond
expr;
v expr-true ,
expr-cond
true;
116
v expr-false ,
expr-cond
false;
v expr
:
o
, expr-cond (7.18),
false, v expr
expr-false;
, v
expr-true, expr-false, expr
;
v expr .
- __ expr ,
:
v body
expr. ,
.
v expr
expr.
:
delegate bool Filter(int i);
void F() {
int max;
// , max
Filter f = (int n) => n < max;
max = 5;
DoWork(f);
}
, max
. :
delegate void D();
void F() {
int n;
D d = () => { n = 1; };
d();
// , n
Console.WriteLine(n);
117
,
n
n .
5.4
__ , .
__ ,
, .
__:
C C++ __ lvalue.
5.5
: bool, char, byte, sbyte,
short, ushort, uint, int, float .
.
, long, ulong, double decimal, ,
.
, , ,
, .
118
6.
.
,
.
, . ,
int long , int
long. long int
.
int a = 123;
long b = a;
// int long
int c = (int) b; // long int
.
(6.4).
6.1
:
null.
,
(7.4.4), (7.6.6) (7.16).
.
.
6.1.1
.
, ,
.
6.1.2
:
byte short, ushort, int, uint, long, ulong, float, double decimal.
float double.
S? T?.
S T?.
, S T,
:
S? T?:
o
S? S,
S T (4.1.9) T T?.
120
S T?,
S T T T?.
6.1.5 null
null .
null (4.1.9) .
6.1.6
:
_ object.
_ S _ T ( S T).
_ S _ T ( S T).
_ S _ T ( S T).
_ S, SE, _ T,
TE, :
o
S T . , S T
.
SE TE _.
SE TE.
_ System.Array.
S[] System.Collections.Generic.IList<T>
, S
T.
_ System.Delegate.
null _.
, , .
, , . 6.1.9.
_
.
. ,
, , .
,
. , List<B> ( , )
List<A>, B A. ,
List<B> List<object>.
6.1.7
_ .
__ object,
System.ValueType, _,
__. , _
System.Enum.
_ ,
__ .
(Microsoft Corp.), 1999-2007. .
121
__
.
System.ValueType, (11.3.2).
_ :
T,
.
. 4.3.1.
6.1.8
:
_ long ulong,
_ .
6.1.9 ,
T:
T C, T C , T
, C. T ,
.
.
T I, T, T
I. T ,
.
.
T U, T U. T , U
, .
, T U , T U ,
. T , U
,
.
null T, T .
T (10.1.5),
(6.1.6). , T
, , ,
(6.1.7).
6.1.10
, ,
.
6.4.4.
122
6.1.11
,
.
. 6.5 6.6 .
6.2
:
(7.6.6).
. ,
.
, , ,
, .
6.2.1
_
_, (6.1.2):
sbyte byte, ushort, uint, ulong char.
float sbyte, byte, short, ushort, int, uint, long, ulong, char decimal.
double sbyte, byte, short, ushort, int, uint, long, ulong, char, float decimal.
decimal sbyte, byte, short, ushort, int, uint, long, ulong, char, float double.
123
,
_
(7.6.6).
.
:
(7.5.12), :
o
checked ,
.
System.OverflowException.
unchecked :
,
.
.
,
.
,
. .
,
.
decimal
. .
,
System.OverflowException.
float double
(7.5.12), :
o
124
checked :
,
System.OverflowException.
. ,
.
System.OverflowException.
unchecked :
,
.
. ,
.
. double
float,
. double ,
.
6.2.2
:
sbyte, byte, short, ushort, int, uint, long, ulong, char, float, double decimal
_.
_ sbyte, byte, short, ushort, int, uint, long, ulong, char, float,
double decimal.
_ _.
_ _
.
, _ E int, E byte
(6.2.1) int byte, byte
E (6.1.2) byte int.
6.2.3
,
, .
S
T (6.1.1, 6.1.2, 6.1.3, 6.2.1 6.2.2)
:
S? T?.
S T?.
S? T.
, S T,
:
S? T?:
o
S? S,
S T T T?.
125
S T?,
S T T T?.
S? T,
S? S S T.
, null
.
6.2.4
:
object _.
_ S _ T, S T.
_ S _ T, S
T.
_ S _ T, T
S.
_ S _ T, S
T.
_ S, SE, _ T,
TE, :
o
S T . , S T
.
SE TE _.
SE TE.
System.Array _.
S[] System.Collections.Generic.IList<T>
, S T.
System.Collections.Generic.IList<S>
T[], S T.
System.Delegate _.
, , .
, , . 6.2.6.
.
null, ,
, ,
(6.1.6).
System.InvalidCastException.
. ,
, , .
126
6.2.5
_.
object System.ValueType
__, _
__, _. ,
System.Enum _.
_ ,
__,
_.
,
_. .
_ null _.
System.ValueType,
(11.3.2).
. 4.3.2.
6.2.6 ,
T:
T C T, C T.
T ,
.
.
T. T ,
.
.
T _ I, T I.
T ,
.
.
U T, T U. T , U
, .
, T U , T U ,
. T , U
,
.
T ,
(6.2.4). , T ,
, ,
(6.2.5).
, , .
. , :
127
class X<T>
{
public static long F(T t) {
return (long)t;
}
}
//
t int, ,
X<int>.F(7) 7L. ,
,
.
:
class X<T>
{
public static long F(T t) {
return (long)(object)t;
long
}
}
// , T
, X<int>.F(7)
, int
long.
6.2.7
, ,
.
6.4.5.
6.3
,
.
6.3.1
:
(6.1.1).
(6.1.2).
(6.1.4).
(6.1.6).
(6.1.7).
(6.1.8).
, (6.1.9).
.
6.3.2
,
,
128
. , A
B, A B B A.
6.4
C#
.
(10.10.3) .
6.4.1
C# . ,
.
, S T ( S T
) S0 T0 ( S0 T0
S T ).
S T ,
:
S0 T0 .
S0, T0 , .
S0, T0 _.
, S T T S, .
, , .
10.10.3.
6.4.2
S
T ,
S? T?.
S? S, S T,
T T? ( , S?
null T? null.
( ),
.
,
.
6.4.3
, .
,
. :
,
. ,
( ,
, , ,
). , _,
.
129
,
.
, (6.3)
,
.
,
. ,
, ,
.
.
.
, :
, ,
.
, ,
.
. ,
S T S X
X T.
. :
(6.3.1) A B,
A B _, , A B, B
A.
,
. ,
. ,
,
.
,
. ,
. ,
,
.
6.4.4
S T :
S0 T0. S T , S0 T0
( S0 T0 S T ).
(D),
. S0 ( S0 ),
130
S0 ( S0 ) T0 ( T0
).
(U).
, D
, S, , T. U ,
.
(SX) U:
o
U S, SX
S.
SX
U.
,
.
(TX) U:
o
U T, TX
T.
TX
U.
,
.
:
o
U SX TX,
.
, U
SX TX, .
,
.
:
o
S SX, S SX.
SX TX .
TX T, TX T.
6.4.5
S T :
S0 T0. S T , S0 T0
( S0 T0 S T ).
(D),
. S0 ( S0 ),
S0 ( S0 ), T0 ( T0 )
T0 ( T0 ).
131
(U).
, D
, S , , T
. U ,
.
(SX) U:
o
U S, SX
S.
, U ,
S, SX
. ,
.
SX
U.
,
.
(TX) U:
o
U T, TX
T.
, U ,
T, TX
.
,
.
TX
U.
,
.
:
o
U SX TX,
.
, U
SX TX, .
:
o
S SX, S SX.
SX TX
.
TX T, TX T.
132
6.5
__ _
(7.14). ,
. , D F
:
F __, D F
.
F __, D
, D
out.
F , D
, F.
F , D ref
out.
D void, F
, D, F
(wrt 7), _ (8.6).
D void, F
, D, F
(wrt 8.2), return
.
D void , F
, D,
F (wrt 7),
D.
D void , F
, D,
F (wrt 8.2) ,
return ,
D.
Expression<D> F ,
D F.
Func<A,R>,
, A R:
delegate R Func<A,R>(A arg);
Func<int,int> f1 = x => x + 1;
//
Func<int,double> f2 = x => x + 1;
//
Func<double,int> f3 = x => x + 1;
//
, .
Func<int,int>. x int, x + 1
, int.
(Microsoft Corp.), 1999-2007. .
133
,
Func<int,double>, x + 1 ( int)
double.
.
x double, x + 1 ( double)
int.
,
. . 7.4.
6.5.1
, ( ) ,
. .
,
.
, , .
. , ,
null, this - .
( )
( )
. .
,
. ,
.
delegate double Function(double x);
class Test
{
static double[] Apply(double[] a, Function f) {
double[] result = new double[a.Length];
for (int i = 0; i < a.Length; i++) result[i] = f(a[i]);
return result;
}
( )
, ,
. ,
.
6.5.2
(4.6). ,
, . ,
.
134
6.5.3
C#. ,
Microsoft C#, , .
,
.
,
. ,
C#. D
:
public delegate void D();
:
class Test
{
static void F() {
D d = () => { Console.WriteLine("test"); };
}
}
,
, :
class Test
{
static void F() {
D d = new D(__Method1);
}
this:
class Test
{
int x;
void F() {
D d = () => { Console.WriteLine(x); };
}
,
:
class Test
{
int x;
void F() {
D d = new D(__Method1);
}
void __Method1() {
Console.WriteLine(x);
}
135
:
class Test
{
void F() {
int y = 123;
D d = () => { Console.WriteLine(y); };
}
}
. ()
.
(7.14.4.2) .
. ,
:
class Test
{
void F() {
__Locals1 __locals1 = new __Locals1();
__locals1.y = 123;
D d = new D(__locals1.__Method1);
}
class __Locals1
{
public int y;
this,
:
class Test
{
int x;
void F() {
int y = 123;
for (int i = 0; i < 10; i++) {
int z = i * 2;
D d = () => { Console.WriteLine(x + y + z); };
}
}
,
.
. __Locals2,
, z ,
__Locals1. __Locals1, ,
y , this
-. __Local2
136
. ,
.
class Test
{
void F() {
__Locals1 __locals1 = new __Locals1();
__locals1.__this = this;
__locals1.y = 123;
for (int i = 0; i < 10; i++) {
__Locals2 __locals2 = new __Locals2();
__locals2.__locals1 = __locals1;
__locals2.z = i * 2;
D d = new D(__locals2.__Method1);
}
}
class __Locals1
{
public Test __this;
public int y;
}
class __Locals2
{
public __Locals1 __locals1;
public int z;
public void __Method1() {
Console.WriteLine(__locals1.__this.x + __locals1.y + z);
}
}
, ,
.
,
. ,
()
.
6.6
(6.1) (7.1) .
D E, ,
E D , E ,
(7.4.3.1) ,
D, .
E D
. , E D ,
.
M (7.5.5.1) E(A)
:
A ,
(ref out),
__ D.
137
, (7.4.3.1),
, .
7.5.5.1 , .
M, ,
D. .
M (15.2) D.
.
M , , E,
.
M ,
, .
D, ,
.
, .
, 7.5.5.1 ,
E(A) (7.5.5.2).
.
:
delegate string D1(object o);
delegate object D2(string s);
delegate object D3();
delegate string D4(object o, params object[] a);
delegate string D5(int i);
class Test
{
static string F(object o) {...}
static void G() {
D1 d1 = F;
D2 d2 = F;
D3 d3 = F;
D4 d4 = F;
D5 d5 = F;
//
//
//
//
// ,
}
}
d1 F D1.
d2 ,
()
().
d3 .
d4 , .
d5 ,
.
,
. ,
138
2
object obj = new EventHandler(myDialog.OkClick);
object obj = (EventHandler)myDialog.OkClick;
, .
. 7.4.
:
, ,
E :
o
. ,
.
_, ,
, . null,
System.NullReferenceException. .
_, (4.3.1)
. .
,
null.
D.
,
System.OutOfMemoryException. .
, ,
, .
139
7.
. ,
, .
7.1
.
. .
. ,
.
.
__ (7.5.4).
.
. __ (7.5.4)
as (7.9.11), is (7.9.10) typeof (7.5.11).
.
, ,
(7.3).
.
, this (7.5.7).
_ (7.5.5) __
(7.5.10.5) (6.6).
.
null.
.
.
.
. , .
, .
( get set)
, this (7.5.7).
. , .
, .
+= -= (7.16.3).
.
. ,
. ,
. ( get set)
,
this (7.5.7),
.
. ,
void.
_ (8.6).
, ,
. , ,
, .
get set.
. ,
__set (7.16.1).
get (7.1.1).
7.1.1
, , ,
. ,
, , , .
,
, .
, ,
.
(5.3), .
__get
. __get, .
- (7.4.4),
.
__get
. __get, .
- (7.4.4) ,
,
.
7.2
. ,
. , , +, -, *, / new. ,
, , , .
.
.
(, x) (, x++).
.
(, x + y).
. , ?:.
(c? x: y).
(7.2.1).
142
7.5
7.6
7.7
* / %
7.7
+ -
7.8
<< >>
7.9
7.9
== !=
7.10
AND
&
7.10
XOR
7.10
OR
7.11
AND
&&
7.11
OR
||
7.12
??
7.13
?:
7.16,
7.14
,
.
. , . ,
x + y + z (x + y) + z.
(Microsoft Corp.), 1999-2007. .
143
(?:) .
, . , x = y = z
x = (y = z).
. , x + y *
z y z, x, (x + y) * z
x y, z.
7.2.2
,
. operator
(10.10).
.
7.2.3 7.2.4,
, .
, , :
+ - ! ~ ++ -- true false
true false (
7.2.1), ,
: (7.19) (7.13)
(7.11).
, , :
+ - * / % & | ^ << >> == != > < >= <=
, . ,
, =, &&, ||, ??, ?:, =>, checked,
unchecked, new, typeof, default, as is.
( )
. , * *=.
7.16.2. , ( =)
.
.
, (T)x,
(6.4).
, a[x], , .
(10.9).
,
.
. op ,
. op ++ -- .
op , .
144
op x
operator op(x)
x op
operator op(x)
x op y
operator op(x, y)
, . ,
, .
,
. , / , ,
.
,
, . ,
operator ==
.
7.5-7.11
, .
,
, .
7.2.3
op x x op, op , , x
X, .
7.2.5 -,
X operator op(x).
- ,
- . -
operator op,
.
(7.5 7.6).
, (x),
- 7.4.3,
.
,
.
7.2.4
x op y, op , , x
X, y Y, .
-, X Y
operator op(x, y). -,
X, -, Y.
7.2.5. X Y
X Y ,
- .
- ,
- . -
operator op,
.
(7.7-7.11).
145
, (x, y),
- 7.4.3,
.
,
.
7.2.5 -
T operator op(A), op , , A
, -, T
operator op(A), .
T0. T null, T0 ,
T0 T.
operator op T0 ,
A (7.4.3.1),
- T0.
, T0 object, - .
-, T0, , T0
T0, T0 .
7.2.6
.
,
. ,
.
*:
int operator *(int x, int y);
uint operator *(uint x, uint y);
long operator *(long x, long y);
ulong operator *(ulong x, ulong y);
float operator *(float x, float y);
double operator *(double x, double y);
decimal operator *(decimal x, decimal y);
(7.4.3)
, . ,
b * s, b byte, s short, operator *(int,
int). , b s int, int.
i * d, i int, d double, operator
*(double, double).
+, ~.
sbyte, byte, short, ushort char int. ,
uint
long.
146
+, , *, /, %, &, |, ^, ==, !=, >, <, >= <=.
. ,
, .
.
decimal, decimal,
float double, .
, double, double.
, float, float.
, ulong, ulong,
sbyte, short, int long,
.
, long, long.
, uint, uint.
int.
, ,
decimal double float. ,
decimal double float .
, ulong,
. , ,
ulong .
,
, .
decimal AddPercent(decimal x, double percent) {
return x * (1.0 + percent / 100.0);
}
, decimal
double.
decimal, .:
decimal AddPercent(decimal x, double percent) {
return x * (decimal)(1.0 + percent / 100.0);
}
7.2.7
,
, .
,
, .
+ ++ - -- ! ~
(Microsoft Corp.), 1999-2007. .
147
,
.
? .
null, null.
,
.
.
+ - * / % & | ^ << >>
,
.
? .
null, null (
& | bool?, . 7.10.3).
,
.
== !=
,
, bool.
? .
null, null .
null,
bool .
< > <= >=
,
, bool.
? .
false, null.
bool
.
7.3
, .
_ (7.5.2)
__ (7.5.4) . _ __
_ _ (7.5.5.1), , .
,
(15), , .
, ,
, , .
, .
N K T .
148
N.
T ,
N ,
(10.1.5) T, N object.
(3.5) N T,
N object. T
, ,
10.3.2. override.
, K , ,
. K , .
, K , ,
(7.4.2) .
, , __ .
, . S.M ,
S , M, .
o
M , , , ,
, S.
M , S
, , ,
M, S.
M , S ,
.
, . ,
T T ,
object, (10.1.5). S.M
, S , M, , S
, object.
o
M , , , ,
, , .
M , ,
,
, M.
, .
o
, ,
.
, , .
, .
, ,
(
)
.
. ,
, 13.2.5.
149
7.3.1
, T :
T object, .
T _, T System.Enum,
System.ValueType object.
T _, T System.ValueType
object.
T _, T T,
object.
T _, T T
object.
T _, T System.Array object.
T _, T System.Delegate
object.
7.4 -
- , . -
. C#
-:
( )
- -. -.
(7.4.1) -
-.
, ,
, - .
7.4.3.
- (,
), -
7.4.4.
,
-, . e, x, y value
150
, T , F ,
P .
F(x, y)
F
.
(x, y).
static,
this.
T.F(x, y)
F
T .
static,
.
(x, y).
e.F(x, y)
F ,
, e
.
static,
.
e (x, y).
get P. P
, .
static,
this.
P = value
set P (value).
P ,
.
static, this.
T.P
T get
P. P static
,
.
T.P = value
T set
P (value). P
static ,
.
e.P
, ,
e, get P
e. P static
,
.
e.P = value
, ,
e, set P
e (value).
P static ,
.
151
E += value
add E. E
static, this.
E -= value
remove E. E
static, this.
T.E += value
T add
E. E static,
.
T.E -= value
T
remove E. E
static, .
e.E += value
, ,
e, add E
e. E static,
.
e.E -= value
, ,
e, remove E
e. E
static, .
e[x, y]
,
, e,
.
get e
(x, y).
,
.
e[x, y] =
value
,
, e,
.
set e
(x, y, value).
,
.
-x
, x,
.
(x).
x+y
, x y,
.
(x, y).
new T(x, y)
T
.
(x, y).
152
7.4.1
- ,
-.
- -.
,
_, .
get ,
, set.
,
+= -=.
,
. set
,
.
ref
out
_ , .
.
, , (10.6.1.1).
ref, __ (5.4), ,
(10.6.1.2).
, (5.3). out,
__ (5.4), ,
(10.6.1.3). -,
, (5.3).
- (7.4.4)
.
153
(6.1) .
-.
,
-. , ,
_, ,
. ,
System.ArrayTypeMismatchException.
,
(10.6.1.4). - ,
, (7.4.3.1):
- ,
, (6.1)
.
.
- ,
,
, (6.1)
.
, ,
.
. ,
class Test
{
static void F(int x, int y, int z) {
System.Console.WriteLine("x = {0}, y = {1}, z = {2}", x, y, z);
}
static void Main() {
int i = 0;
F(i++, i++, i++);
}
}
x = 0, y = 1, z = 2
(12.5) A[]
B[], B A.
, _ ,
,
.
class Test
{
static void F(ref object x) {...}
154
F System.ArrayTypeMismatchException,
b string, object.
- ,
,
(7.5.10.4). ,
void F(int x, int y, params object[] args);
F(10, 20);
F(10, 20, 30, 40);
F(10, 20, 1, "hello", 3.0);
F(10, 20, new object[] {});
F(10, 20, new object[] {30, 40});
F(10, 20, new object[] {1, "hello", 3.0});
, , ,
.
7.4.2
.
. ,
class Chooser
{
static Random rand = new Random();
Choose :
int i = Chooser.Choose(5, 213);
// Choose<int>
// Choose<string>
int string .
(7.5.5.1)
.
,
. ,
.
,
.
(Microsoft Corp.), 1999-2007. .
155
, .
. ,
.
,
. ,
Tr M<X1Xn>(T1 x1 Tm xm)
M(E1 Em)
S1Sn X1Xn, M<S1Sn>(E1Em) .
Xi Si,
. T.
Xi .
.
.
,
. .
. .
7.4.2.12,
7.4.2.13.
Ei:
Ei ,
(7.4.2.7) Ei Ti
(7.4.2.6) Ei Ti
156
, Xi,
,
o
Xj, Xi;
Xi .
, .
, ,
.
Ei Ti, (7.4.2.4)
Xj, (7.4.2.3) ,
(7.4.2.6) Ei Ti. .
E , T
, T E
T.
E , T
, T E T.
Xi
Xj, Ek Tk Xj Ek Tk, Xi
Ek Tk.
Xj Xi, Xj Xi Xi Xk Xk
Xj. , ,
.
E T :
E U (7.4.2.11),
T Tb,
U Tb (7.4.2.9).
, E , T
Tb T1Tk Tb,
E T1Tk U,
U Tb .
, e U, U T
.
E T .
E c U1Uk T
V1Vk, Ui (7.4.2.8)
Ui Vi.
U V .
V Xi, U Xi.
, U Ue[] V Ve[] ,
Ue Ve.
, V C<V1Vk> U
C<U1Uk>, Ui Vi.
157
U V .
V Xi, U Xi.
, U Ue[] V Ve[] ,
U Ue[] V
IEnumerable<Ve>, ICollection<Ve> IList<Ve>,
o
, Ue , Ue Ve
;
Ue Ve.
, V C<V1Vk>
U1Uk, U C<U1Uk>,
Ui Vi.
Xi .
Uj Xi.
Xi: U Xi
Uj,
U.
- Uj V,
-, Xi V.
F
.
,
,
.
.
F , F
.
F ,
return T (7.4.2.13), F T.
E .
Select,
System.Linq.Enumerable:
158
2
namespace System.Linq
{
public static class Enumerable
{
public static IEnumerable<TResult> Select<TSource,TResult>(
this IEnumerable<TSource> source,
Func<TSource,TResult> selector)
{
foreach (TSource element in source) yield return selector(element);
}
}
}
, System.Linq using
Customer Name string.
Select:
List<Customer> customers = GetCustomerList();
IEnumerable<string> names = customers.Select(c => c.Name);
(7.5.5.2) Select
:
IEnumerable<string> names = Enumerable.Select(customers, c => c.Name);
,
. customers source, T Customer.
c
Customer, c.Name selector,
S string. ,
Sequence.Select<Customer,string>(customers, (Customer c) => c.Name)
IEnumerable<string>.
,
.
static Z F<X,Y,Z>(X value, Func<X,Y> f1, Func<Y,Z> f2) {
return f2(f1(value));
}
double seconds = F("1:15:30", s => TimeSpan.Parse(s), t => t.TotalSeconds);
. 1:15:30 value,
X string. s
string, TimeSpan.Parse(s) f1, Y
System.TimeSpan. , t
System.TimeSpan, t.TotalSeconds
f2, Z double. , double.
,
M, , D (6.6).
Tr M<X1Xn>(T1 x1 Tm xm)
M D,
S1Sn, :
(Microsoft Corp.), 1999-2007. .
159
M<S1Sn>
(15.1) D.
,
, . , , ,
.
Xi ,
Uj D Tj M. - Xi
, . Xi Si,
.
. ,
.
E1Em
Tr M<X>(X x1 X xm)
Ei .
, X.
Ei X. , X S
.
7.4.3
- . -
C#:
, _ (7.5.5.1);
, __ (7.5.10.1);
__ (7.5.6);
, (7.2.3
7.2.4).
-
-, . ,
, override (7.3),
,
(7.5.5.1).
-
- .
160
-
-. -,
- . -
-,
7.4.3.2. -, , ,
- .
-
-.
-
- - A,
.
A -.
A ( ,
ref out)
(6.1)
, ref out,
. ref out
.
-, , -
, . -,
, ,
.
-
,
A . A
, -,
- .
, A
o
, ,
(6.1)
, ref out,
.
-
A { E1, E2, ..., EN }
- MP MQ { P1, P2, ..., PN } { Q1, Q2, ..., QN } MP
-, MQ,
EX QX EX
PX
EX PX EX QX.
MP MQ , PX QX
.
161
MP , MQ , MP
, MQ.
, MP , MQ params
, MP , MQ.
, MP , MQ, MP ,
MQ. , - params,
.
, MP , MQ, MP ,
MQ. {R1, R2, , RN} {S1, S2, , SN}
- MP MQ. MP
, MQ, RX , SX,
RX , SX.
o
, .
, (
),
.
, ( ),
, .
, , ,
.
- .
C1, E T1,
C2, E T2,
.
T1 T2 , .
E S S T1 S T2, C1
.
E S S T2 S T1, C2
.
E , T1 T2
(7.4.2.11)
E X:
T1 Y1, T2 Y2
X Y1 X Y2, C1 ;
T1 Y1, T2 Y2
X Y2 X Y1, C2 ;
T1 Y, T2 void, C1
;
162
T1 void, T2 Y, C2
.
.
C1, S T1, C2,
S T2, .
T1 T2 , .
S T1, C1.
S T2, C2.
T1 T2 T2
T1, C1.
T2 T1 T1
T2, C2.
, ,
. , ,
6 short , 6 ushort,
short ushort.
,
. ,
, .
:
interface I1<T> {...}
163
class G1<U>
{
int F1(U u);
int F1(int i);
class G2<U,V>
{
void F3(U u, V v);
void F3(V v, U u);
void F4(U u, I1<V> v);
void F4(I1<V> v, U u);
// G<int>.F1
//
//
// ,
// G2<int,int>.F3
// ,
// G2<I1<int>,int>.F4
//
7.4.4 -
,
-. ,
-, ,
-.
- .
-. , ,
. -
.
- . , .
- ,
.
- this (7.5.7).
- , M
-, E ( M ).
M -
o
, 7.4.1.
M.
M - , _
o
E. ,
.
E , E
E. E
. this M,
. , , M
this, E .
, 7.4.1.
164
M. , E, ,
this.
M - , _
E. ,
.
, 7.4.1.
E _, (4.3) E
object. , E object. M
System.Object.
E. E null,
System.NullReferenceException .
-.
E , -
M, ,
E. -
(13.4.4), M,
, E.
, M -, -
M, ,
E. -
(10.6.3) M
, E.
M -, -
M.
-, . ,
E, , this.
-, _
_ .
- , object,
object.
- -
_.
- .
, _
, this -.
, , -
, .
7.5
165
:
,
,
this
base
typeof
checked
unchecked
__
______
, ,
, .:
object o = new int[3][1];
object o = (new int[3])[1];
7.5.1
_, (2.4.4) .
7.5.2
_ , :
:
_ I I<A1, ..., AK>, I , <A1, ...,
AK> __. __ ,
, K . _ .
K _
( ) (3.3) ,
I, _ ,
.
K _
I, _
.
166
T (10.3.1),
( )
o
K T I, _
.
, I (7.3) T K :
T
,
this. ,
(7.5.5.1).
, T
,
,
(7.5.4) this.I. , K
.
N, ,
_, ( )
, , .
o
K I N, :
_ N
__extern
__using, I ,
_ .
_ I N.
, N I K , :
K _
N
__extern __using, I
, _
.
____ ,
.
, _ N,
:
K __extern
__using, I
, _
.
, , _using_namespace
, I K ,
_ , .
167
, , _using_namespace
, I K
, _ .
,
____ (3.8).
_ .
_
_
(8.2) _switch (8.7.2)
. ,
.
class Test
{
double x;
void F(bool b) {
x = 1.0;
if (b) {
int x;
x = 1;
}
}
}
, x
( if). ,
class Test
{
double x;
void F(bool b) {
if (b) {
x = 1.0;
}
else {
int x;
x = 1;
}
}
, x .
, .
(7.5.4). :
struct Point
{
int x, y;
168
. x y ,
this.x this.y .
7.5.3
__ , .
( )
__ .
, ,
. __
.
7.5.4
___ _, _
__, . .
__.
.
.
.
:
bool
byte
object sbyte
char
short
decimal
uint
float
int
ulong
ushort
long
__ 9.7.
___ E.I E.I<A1, ..., AK>, E
, I , <A1, ..., AK> __.
__ , , K .
___ .
K , E E I,
.
, E , E I K
, , .
E _ _,
, E I (7.3) E K
, E.I .
I , ,
.
I ,
. ,
(7.5.5.1).
169
I static,
.
I static,
, , , ,
I E.
, I E.
I static,
, ,
____ (10.8), E.I
, I .
I , , .
I , ,
.
E.I , .
E , T
I (7.3) T K , E.I
.
o
-, E ,
(7.1.1) E
.
I ,
E. ,
(7.5.5.1).
I ,
E.
T _ I _,
o
170
E null, System.NullReferenceException.
,
, , ,
, I , E.
, I , E.
T _ I _,
E
, ,
, , I ,
E.
, I ,
E.
I ,
, ,
____ (10.8), E.I
, I .
E.
E.I (7.5.5.2).
, E.I
.
E.I, E , E
_ (7.5.2) , , ,
, E _ (3.8), E.
E.I , I
E. ,
E,
. :
struct Color
{
public static readonly Color White = new Color(...);
public static readonly Color Black = new Color(...);
public Color Complement() {...}
}
class A
{
public Color Color;
// Color Color
void F() {
Color = Color.Black;
// Color.Black
Color = Color.Complement();
// Complement() Color
}
// Color.White
A Color, Color, ,
Color, .
_ (7.5.2) ___ (7.5.4)
. ,
F(G<A,B>(7));
( ) _ (7.5.2),
___ (7.5.4) ____ (18.5.2),
__ (4.4.1), ,
>.
(Microsoft Corp.), 1999-2007. .
171
( ) ] } : ; , . ? == !=
__ _, ___
____,
. __ _,
___ ____,
. ,
__ ____ (3.8).
F(G<A,B>(7));
F ,
G
.
F(G < A, B > 7);
F(G < A, B >> 7);
F .
x = F < A > +y;
, ,
x = (F < A) > (+y), _
__, .
x = y is C<T> + z;
C<T> ____
__.
7.5.5
_ .
( )
___
_. _ , _
(7.5.5.1). _ _,
_ (7.5.5.3). _
, _, .
_ (7.4.1)
.
_ .
_ , void,
. , ,
_ (8.6).
, .
___ .
,
.
, _.
172
M(A), M (,
__), A _,
.
- . F,
M
o
F , F , :
F A (7.4.3.1).
F , M , F
, :
(7.4.2),
,
F
(4.4.4), F A (7.4.3.1).
F , M , F
, :
F ,
,
F (4.4.4),
F A (7.4.3.1).
- .
C.F , C , F, ,
C . , C ,
object, , .
( ,
, object,
.)
- ,
,
(7.5.5.2). ,
.
- ,
. (
, ,
13.2.5, .)
-
7.4.3. ,
.
(
) .
173
.
, _
___ . ,
_, ___
__.
, .
,
( ) (4.4.4),
. -
, .
7.4.4.
, , .
, , ,
, ,
, .
, , ,
. ,
.
(7.5.5.1)
. ( )
. ( )
. < > ( )
. < > ( )
,
. _ C,
:
C. ( )
C. ( , )
C. < > ( )
C. < > ( , )
C .
,
,
, .
o
Ci Mj ,
(. ), .
,
,
Ci Mj ,
174
(. ), .
, .
, (7.4.3).
, .
C , .
C (7.4.4).
,
, , ,
,
, , ,
,
. :
public static class E
{
public static void F(this object obj, int i) { }
}
class A { }
class B
{
public void F(int i) { }
}
class C
{
public void F(object obj) { }
}
class X
{
static void Test(A a, B b, C c) {
a.F(1);
// E.F(object, int)
a.F("hello");
// E.F(object, string)
b.F(1);
b.F("hello");
// B.F(int)
// E.F(object, string)
c.F(1);
c.F("hello");
// C.F(object)
// C.F(object)
B , C
.
175
namespace N1
{
public static class D
{
public static void F(this int i) { Console.WriteLine("D.F({0})", i); }
public static void G(this int i) { Console.WriteLine("D.G({0})", i); }
}
}
namespace N2
{
using N1;
public static class E
{
public static void F(this int i) { Console.WriteLine("E.F({0})", i); }
}
class Test
{
static void Main(string[] args)
{
1.F();
2.G();
3.H();
}
}
:
E.F(1)
D.G(2)
C.H(3)
D.G C.G, E.F D.F C.F.
___ _.
, , _ -
, _, (7.4.3.1)
___.
D(A), D
___, A _,
.
D. ,
.
D. D null,
System.NullReferenceException .
D . - (7.4.4)
. ,
, ,
.
176
7.5.6
___ _____,
[ _, ]. _
, .
:
, [ ]
:
,
_____, ___
_, ___
(7.5.6.1). _____,
, ,
___ (7.5.6.2).
_____,
___ _.
_ _,
int, uint, long, ulong , .
,
, _.
P[A], P
_____, _, A
_, .
P. ,
.
_ .
(6.1)
: int, uint, long, ulong. ,
. , short,
int, short
int short long.
,
.
P. P null,
System.NullReferenceException .
_
, P.
,
System.IndexOutOfRangeException .
, ,
.
177
_____
___ ,
, ,
_ ___.
P[A], P
_____, , T,
A _, .
, T. ,
T T ,
(3.4.7).
, .
S.I , S , I,
.
o
I A (7.4.3.1), I .
I A (7.4.3.1), ,
S .
I A (7.4.3.1) S , object,
, .
- ,
.
-
7.4.3. ,
.
_ .
,
. ,
, P A.
__get
__set . ,
__set (7.16.1).
__get (7.1.1).
7.5.7 this
_this this.
this:
this
_this ,
. .
this _ ,
. (10.3.1) ,
, .
this _
, .
178
(10.3.1) , ,
, .
this _ ,
. (10.3.1) ,
, .
this , out
, , ,
.
this _
, .
(10.3.1), .
o
(10.14), this
, , ,
ref .
, this
, , ,
.
this _ , ,
. , this
, _ .
7.5.8 base
_base base, .
, _, :
base
base .
base [ ]
_base ,
. _base
, . base.I
, I .
, base[E] ,
.
_base base.I base[E] ,
((B)this).I ((B)this)[E], B
, . , base.I base[E]
this.I this[E] , this
.
_base - (,
), - (7.4.4) .
- (10.6.3)
- B ( this ,
, base). ,
- _base
-. -, _base,
, .
179
7.5.9
:
++
:
- ,
, .
, .
, get set.
.
(7.2.3). ++ -- : sbyte, byte, short, ushort,
int, uint, long, ulong, char, float, double, decimal .
++ , 1 , --
, 1 . checked
, System.OverflowException.
x++ x- .
x , :
o
x .
x .
x .
, , ,
x.
x .
x , :
o
( x static) ( x
), x,
get set.
get x, .
x .
set x ,
value.
x .
180
7.5.10 new
new .
new:
new ,
. ,
, , new
.
__ _
_.
:
new ( )
new
:
__ _, _
_. _ abstract.
_ (7.4.1) , _
_.
,
.
.
, ,
, ,
(7.5.10.2) (7.5.10.3).
__ new T(A), T
_ _, A _,
.
T _ A .
o
, T _ A .
o
__ .
__ T, T ,
4.1.1.
T (10.1.5),
.
181
__ ,
,
. .
, T _ _:
T _ abstract, .
7.4.3.
, T, A (7.4.3.1).
, .
__ T, ,
, .
__
.
__ new T(A), T
_ _, A _,
.
T _:
o
T. ,
System.OutOfMemoryException
(5.2).
- (7.4.4) .
,
this.
T _:
o
T.
_, .
- (7.4.4) .
,
this.
.
:
{ }
{ , }
,
:
=
182
,
{ } .
,
.
.
.
, , ,
(7.16.1) .
, ,
, .
.
,
.
, ,
.
, , ,
. ,
7.5.10.3.
.
public class Point
{
int x, y;
public int X { get { return x; } set { x = value; } }
public int Y { get { return y; } set { y = value; } }
}
Point :
Point a = new Point { X = 0, Y = 1 };
Point __a = new Point();
__a.X = 0;
__a.Y = 1;
Point a = __a;
__a .
, :
public class Rectangle
{
Point p1, p2;
Rectangle :
183
Rectangle __r = new Rectangle();
Point __p1 = new Point();
__p1.X = 0;
__p1.Y = 1;
__r.P1 = __p1;
Point __p2 = new Point();
__p2.X = 2;
__p2.Y = 3;
__r.P2 = __p2;
Rectangle r = __r;
Point
:
Rectangle r = new Rectangle {
P1 = { X = 0, Y = 1 },
P2 = { X = 2, Y = 3 }
};
Rectangle __r = new Rectangle();
__r.P1.X = 0;
__r.P1.Y = 1;
__r.P2.X = 2;
__r.P2.Y = 3;
Rectangle r = __r;
.
:
{ }
{ , }
,
184
:
{ }
,
{ } . ,
, ,
{ } .
, ,
. __
7.17.
, :
List<int> digits = new List<int> { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
, , ,
System.Collections.Ienumerable,
. Add
,
. ,
Add .
:
public class Contact
{
string name;
List<string> phoneNumbers = new List<string>();
public string Name { get { return name; } set { name = value; } }
}
List<Contact> :
var contacts = new List<Contact> {
new Contact {
Name = "Chris Smith",
PhoneNumbers = { "206-555-0101", "425-882-8080" }
},
new Contact {
Name = "Bob Harris",
PhoneNumbers = { "650-555-0199" }
}
};
var contacts = new List<Contact>();
Contact __c1 = new Contact();
__c1.Name = "Chris Smith";
__c1.PhoneNumbers.Add("206-555-0101");
__c1.PhoneNumbers.Add("425-882-8080");
contacts.Add(__c1);
Contact __c2 = new Contact();
__c2.Name = "Bob Harris";
__c2.PhoneNumbers.Add("650-555-0199");
contacts.Add(__c2);
__c1 __c2 , .
(Microsoft Corp.), 1999-2007. .
185
__ _.
:
new [ ]
new
new
,
. ,
new int[10, 20] int[,], new int[10][,]
int[][,]. int, uint,
long ulong , .
. ,
_
.
(18.1) .
,
, ,
, .
.
. ,
new int[,] {{0, 1}, {2, 3}, {4, 5}}
new int[3, 2] {{0, 1}, {2, 3}, {4, 5}}
. ,
, (7.4.2.13)
. ( _,
) , _.
12.6.
,
.
.
_ .
(6.1)
: int, uint, long, ulong. ,
.
,
.
.
,
System.OverflowException .
186
.
, System.OutOfMemoryException
(5.2).
,
.
, , ,
.
,
(,
).
, . ,
100 int[].
null. ,
//
.
, .:
int[][] a = new int[100][];
for (int i = 0; i < 100; i++) a[i] = new int[5];
,
, .
101 100 . ,
, , .
.
var a = new[] { 1, 10, 100, 1000 };
var b = new[] { 1, 1.5, 2, 2.5 };
var c = new[,] { { "hello", null }, { "world", "!" } };
var d = new[] { 1, "one", 2, "two" };
// int[]
// double[]
// string[,]
//
, int string
, .
, ,
object[]. ,
.
(7.5.10.6) :
187
__ _.
:
new ( )
,
_. , ,
, . ,
.
_, , .
__ new D(E), D
_, E , .
E , ,
(6.6) E D.
E , ,
(6.5) E D.
E , E (15.1) D,
D, , E.
E D, .
__ new D(E), D
_, E , .
E ,
(6.6) E D.
E ,
E D (6.5).
E _:
o
E. ,
.
E null, System.NullReferenceException
.
D.
, System.OutOfMemoryException
,
, E.
188
.
.
(15.1) . .
, , , ,
, .
, ,
.
delegate double DoubleFunc(double x);
class A
{
DoubleFunc f = new DoubleFunc(Square);
static float Square(float x) {
return x * x;
}
A.f , Square,
DoubleFunc. Square , .
___ .
:
new
{ }
{ , }
:
,
:
=
.
, object.
,
, . ,
new { p1 = e1 , p2 = e2 , pn = en }
189
class __Anonymous1
{
private readonly T1 f1 ;
private readonly T2 f2 ;
private readonly Tn fn ;
public __Anonymous1(T1 a1, T2 a2,, Tn an) {
f1 = a1 ;
f2 = a2 ;
fn = an ;
}
public T1 p1 { get { return f1 ; } }
public T2 p2 { get { return f2 ; } }
Tx ex. ,
_, . _ null
, .
, .
,
.
,
,
.
var p1 = new { Name = "Lawnmower", Price = 495.00 };
var p2 = new { Name = "Shovel", Price = 26.95 };
p1 = p2;
, p1 p2 .
Equals GetHashcode ,
object, Equals GetHashcode ,
.
(7.5.2) (7.5.4).
. ,
:
=
= .
, ,
, .
, .
190
7.5.11 typeof
typeof System.Type .
typeof
typeof ( )
typeof ( )
typeof ( void )
:
::
.
:
< >
:
,
,
_typeof typeof, .
System.Type .
System.Type. , T
typeof(T) == typeof(T).
_typeof typeof,
__ . __ _ (3.8)
, __ __,
_ __. _typeof
, __
_, __,
__, _.
__ .
_
__ __
object, _.
_ , .
__ ,
(4.4.3).
_typeof System.Type
.
_typeof typeof,
void . System.Type,
. , typeof(void), ,
.
, ,
, , void,
System.Type.
typeof . System.Type
, . typeof
(4.4.3).
System.Type System.Type
. ,
(Microsoft Corp.), 1999-2007. .
191
System.Type ,
.
using System;
class X<T>
{
public static void PrintTypes() {
Type[] t = {
typeof(int),
typeof(System.Int32),
typeof(string),
typeof(double[]),
typeof(void),
typeof(T),
typeof(X<T>),
typeof(X<X<T>>),
typeof(X<>)
};
for (int i = 0; i < t.Length; i++) {
Console.WriteLine(t[i]);
}
}
}
class Test
{
static void Main() {
X<int>.PrintTypes();
}
}
System.Int32
System.Int32
System.String
System.Double[]
System.Void
System.Int32
X`1[System.Int32]
X`1[X`1[System.Int32]]
X`1[T]
, int System.Int32 .
, typeof(X<>) ,
typeof(X<T>) .
7.5.12 checked unchecked
checked unchecked
.
checked:
checked ( )
unchecked:
unchecked ( )
192
checked ,
unchecked . _checked _unchecked
__ (7.5.3) ,
.
checked
unchecked (8.11).
, checked
unchecked, .
++ -- (7.5.9 7.6.5), .
(7.6.2), .
+, -, *, / (7.7), .
(6.2.1) float
double .
, .
checked (7.18),
. ,
System.OverflowException.
unchecked ,
.
(, ),
checked unchecked,
unchecked, (,
) checked.
(, )
checked.
unchecked, ,
.
checked unchecked,
.
class Test
{
static readonly int x = 1000000;
static readonly int y = 1000000;
static int F() {
return checked(x * y);
}
// OverflowException
193
//
,
. F System.OverflowException,
G 727379968 ( 32 , ).
H ,
, F, G.
class Test
{
const int x = 1000000;
const int y = 1000000;
static int F() {
return checked(x * y);
}
// ,
// ,
, F H,
, checked.
G ,
unchecked, .
checked unchecked ,
( ). , .
class Test
{
static int Multiply(int x, int y) {
return x * y;
}
static int F() {
return checked(Multiply(1000000, 1000000));
}
}
checked F x * y Multiply, x * y
.
unchecked
. :
class Test
{
public const int AllBits = unchecked((int)0xFFFFFFFF);
}
194
uint.
int, unchecked int
.
checked unchecked
.
. ,
unchecked.
checked. ,
, .
7.5.13
(5.2) .
,
, . (
, null .)
:
default ( )
___ ,
null, .
___ ,
_ (4.1.2).
___ (7.18),
, (10.1.5). ,
__ ,
: sbyte, byte, short, ushort, int, uint, long, ulong, char, float, double,
decimal, bool .
7.5.14
__
. 7.14.
7.6
+, -, !, ~, ++, -- .
:
+
-
!
~
7.6.1
+x ,
(7.2.3). ,
. c
:
(Microsoft Corp.), 1999-2007. .
195
.
7.6.2
-x ,
(7.2.3). ,
. c
:
:
int operator (int x);
long operator (long x);
x . x
(231 int 263 long),
. checked,
System.OverflowException. unchecked,
.
uint, long
long. ,
int 2147483648 (231) (2.4.4.2).
ulong,
. , long
9223372036854775808 (263) (2.4.4.2).
:
float operator (float x);
double operator (double x);
x . x NaN,
NaN.
:
decimal operator (decimal x);
x .
System.Decimal.
7.6.3
!x ,
(7.2.3). ,
.
:
bool operator !(bool x);
. true,
false. false, true.
196
7.6.4
~x ,
(7.2.3). ,
.
:
int operator ~(int x);
uint operator ~(uint x);
long operator ~(long x);
ulong operator ~(ulong x);
x.
E :
E operator ~(E x);
~x, x E U,
(E) (~(U)x).
7.6.5
:
++
:
--
,
, .
, .
, get set.
.
(7.2.3). ++ -- : sbyte, byte, short, ushort, int, uint,
long, ulong, char, float, double, decimal . ++
, 1 , -- ,
1 . checked
, System.OverflowException.
++x --x
.
x , :
o
x .
x .
, , ,
x.
, , .
x , :
197
( x static) ( x
), x,
get set.
get x.
,
get.
set x ,
value.
, , .
, .
, ,
, ~, !, (, (2.4.1),
(2.4.4) (2.4.3) as is.
,
.
- . , x y
, x.y , x.y
.
, x y , (x)y, (x)(y)
(x)(-y) __, (x)-y , x .
x , (, int),
__ (
).
198
7.7
*, /, %, + .
:
*
/
%
:
+
7.7.1
x * y ,
(7.2.4).
, .
. x y.
:
int operator *(int x, int y);
uint operator *(uint x, uint y);
long operator *(long x, long y);
ulong operator *(ulong x, ulong y);
checked
, System.OverflowException. unchecked
, ,
, .
float operator *(float x, float y);
double operator *(double x, double y);
IEEE 754.
, ,
NaN z x * y.
, z . , z
.
+y
+0
NaN
+x
+z
+0
NaN
+z
+0
NaN
+0
+0
+0
NaN
NaN
NaN
+0
+0
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
.
(Microsoft Corp.), 1999-2007. .
199
decimal,
System.OverflowException.
decimal, .
.
System.Decimal.
7.7.2
x / y ,
(7.2.4).
, .
. x y.
:
int operator /(int x, int y);
uint operator /(uint x, uint y);
long operator /(long x, long y);
ulong operator /(ulong x, ulong y);
,
System.DivideByZeroException.
,
, .
, , ,
.
int long,
1, . checked
System.ArithmeticException ( ). unchecked
System.ArithmeticException ( )
( )
.
:
float operator /(float x, float y);
double operator /(double x, double y);
IEEE 754.
, ,
NaN z x / y.
, z . , z
.
200
+y
+0
NaN
+x
+z
+0
NaN
+z
+0
NaN
+0
+0
NaN
NaN
+0
NaN
+0
NaN
NaN
+0
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
:
decimal operator /(decimal x, decimal y);
,
System.DivideByZeroException.
decimal, System.OverflowException.
decimal,
. ,
.
System.Decimal.
7.7.3
x % y ,
(7.2.4).
, .
. x y.
:
int operator %(int x, int y);
uint operator %(uint x, uint y);
long operator %(long x, long y);
ulong operator %(ulong x, ulong y);
x % y x (x / y) * y. y ,
System.DivideByZeroException.
int long, -1,
System.OverflowException. x / y , x % y
.
:
float operator %(float x, float y);
double operator %(double x, double y);
,
, NaN z x % y x n *
y, n , x / y.
, ,
IEEE 754 ( n , x / y).
201
+y
+0
NaN
+x
+z
+z
NaN
NaN
NaN
NaN
NaN
NaN
+0
+0
+0
NaN
NaN
+0
+0
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
:
decimal operator %(decimal x, decimal y);
,
System.DivideByZeroException.
, ( ) x.
System.Decimal.
7.7.4
x + y ,
(7.2.4).
, .
.
.
,
.
:
int operator +(int x, int y);
uint operator +(uint x, uint y);
long operator +(long x, long y);
ulong operator +(ulong x, ulong y);
checked ,
System.OverflowException. unchecked
, , ,
.
:
float operator +(float x, float y);
double operator +(double x, double y);
IEEE 754.
, , NaN x y
, z x + y. x y ,
, z . x + y
, z , x +
y.
202
+0
NaN
NaN
+0
+0
+0
NaN
+0
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
:
decimal operator +(decimal x, decimal y);
decimal,
System.OverflowException.
.
System.Decimal.
.
, E , U
E:
E operator +(E x, U y);
E operator +(U x, E y);
(E)((U)x + (U)y).
:
string operator +(string x, string y);
string operator +(string x, object y);
string operator +(object x, string y);
+ , string.
null, .
ToString, bject. ToString null,
.
using System;
class Test
{
static void Main() {
string s = null;
Console.WriteLine("s = >" + s + "<");
int i = 1;
Console.WriteLine("i = " + i);
float f = 1.2300E+15F;
Console.WriteLine("f = " + f);
decimal d = 2.900m;
Console.WriteLine("d = " + d);
}
}
// s = ><
// i = 1
// f = 1.23E+15
// d = 2.900
, ,
.
(Microsoft Corp.), 1999-2007. .
203
null.
System.OutOfMemoryException.
.
, D :
D operator +(D x, D y);
+ ,
D. ( , .)
null, (
null). , null,
. ,
, .
. 7.7.5 15.4. System.Delegate ,
+ .
7.7.5
x y, ,
(7.2.4).
, .
. y x.
:
int operator (int x, int y);
uint operator (uint x, uint y);
long operator (long x, long y);
ulong operator (ulong x, ulong y);
checked ,
System.OverflowException. unchecked
, , ,
.
:
float operator (float x, float y);
double operator (double x, double y);
IEEE 754.
, , NaN x y
, z x - y. x y , z
. x - y , z
, x - y.
204
+0
NaN
NaN
+0
+0
+0
NaN
+0
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
.
decimal operator (decimal x, decimal y);
decimal,
System.OverflowException.
.
System.Decimal.
.
, E , U
E:
U operator (E x, E y);
(U)((U)x (U)y). ,
x y,
.
E operator (E x, U y);
(E)((U)x y). ,
, .
.
, D :
D operator (D x, D y);
- ,
D. , .
null, null. ,
null, .
(15.1) ,
, ,
,
. (
, (7.9.8).)
.
.
,
. , null.
:
delegate void D(int x);
class C
{
public static void M1(int i) { /* */ }
public static void M2(int i) { /* */ }
}
205
class Test
{
static void Main() {
D cd1 = new D(C.M1);
D cd2 = new D(C.M2);
D cd3 = cd1 + cd2 + cd2 + cd1;
cd3 -= cd1;
// M1 + M2 + M2 + M1
// => M1 + M2 + M2
// M1 + M2 + M2 + M1
// => M2 + M1
// M1 + M2 + M2 + M1
// => M1 + M1
// M1 + M2 + M2 + M1
// => M1 + M2
// M1 + M2 + M2 + M1
// => M1 + M2 + M2 + M1
7.8
<< >> .
:
<<
x << x >> , ,
(7.2.4).
,
.
, , int.
.
:
int operator <<(int x, int count);
uint operator <<(uint x, int count);
long operator <<(long x, int count);
ulong operator <<(ulong x, int count);
<< x ,
.
x ,
, .
int operator >>(int x, int count);
uint operator >>(uint x, int count);
long operator >>(long x, int count);
ulong operator >>(ulong x, int count);
>> x ,
.
206
x int long, x ,
, , x
, , x .
x uint ulong, x ,
, .
.
x int uint, .
, & 0x1F.
x long ulong, .
, & 0x3F.
, x.
checked unchecked.
>> ,
, ( )
. >> ,
, .
. , x int, unchecked((int)((uint)x
>> y)) x .
7.9
==, !=, <, >, <=, >=, is as .
:
<
>
<=
>=
is
as
:
==
!=
is 7.9.10, as 7.9.11.
==, !=, <, >, <= >= . x op y, op
, ,
(7.2.4). ,
.
.
bool, .
207
x == y
true, x y, false
x != y
true, x y, false
x<y
true, x y, false
x>y
true, x y, false
x <= y
true, x y, false
x >= y
true, x y, false
7.9.1
:
bool
bool
bool
bool
operator
operator
operator
operator
bool
bool
bool
bool
operator
operator
operator
operator
bool
bool
bool
bool
operator
operator
operator
operator
bool
bool
bool
bool
operator
operator
operator
operator
bool
bool
bool
bool
operator
operator
operator
operator
bool
bool
bool
bool
operator
operator
operator
operator
bool, , .
7.9.2
:
bool operator ==(float x, float y);
bool operator ==(double x, double y);
bool operator !=(float x, float y);
bool operator !=(double x, double y);
bool operator <(float x, float y);
bool operator <(double x, double y);
208
2
bool operator >(float x, float y);
bool operator >(double x, double y);
bool operator <=(float x, float y);
bool operator <=(double x, double y);
bool operator >=(float x, float y);
bool operator >=(double x, double y);
IEEE 754.
- , false,
!=, true. x
!= y , !(x == y).
, <, >, <=, >= ,
. , x y , x < y
false, !(x >= y) true.
,
< . < ... < . < 0.0 == +0.0 < +. < ... < +. < +
. . ,
.
.
o
,
.
,
.
7.9.3
:
bool operator ==(decimal x, decimal y);
bool operator !=(decimal x, decimal y);
bool operator <(decimal x, decimal y);
bool operator >(decimal x, decimal y);
bool operator <=(decimal x, decimal y);
bool operator >=(decimal x, decimal y);
bool, , .
System.Decimal.
7.9.4
:
bool operator ==(bool x, bool y);
bool operator !=(bool x, bool y);
209
x op y, x y E U,
op , ((U)x) op ((U)y).
,
.
7.9.6
:
bool operator ==(object x, object y);
bool operator !=(object x, object y);
.
object, , operator ==
operator !=. ,
.
.
_ null. ,
(6.3.1).
T, T _,
null. , T .
- , .
.
, ,
. , A B
A, B ,
. , .
. ,
.
210
,
.
T null T
, false.
,
null.
class C<T>
{
void F(T x) {
if (x == null) throw new ArgumentNullException();
...
}
}
x == null , T ,
false, T .
x == y x != y operator == operator !=,
(7.2.4)
.
,
object.
using System;
class Test
{
static void Main() {
string s = "Test";
string t = string.Copy(s);
Console.WriteLine(s == t);
Console.WriteLine((object)s == t);
Console.WriteLine(s == (object)t);
Console.WriteLine((object)s == (object)t);
}
}
True
False
False
False
s t string,
True, string
(7.9.7).
False, object
.
, .
211
class Test
{
static void Main() {
int i = 123;
int j = 123;
System.Console.WriteLine((object)i == (object)j);
}
}
False,
int.
7.9.7
:
bool operator ==(string x, string y);
bool operator !=(string x, string y);
string , :
null;
, .
, ,
. 7.9.6,
.
7.9.8
:
bool operator ==(System.Delegate x, System.Delegate y);
bool operator !=(System.Delegate x, System.Delegate y);
, .
null, , null.
, .
(15.1), ,
(
) .
, .
( ), .
,
__ ( )
( ) .
212
7.9.9 null
== != , ,
null, (
).
x == null
null == x
x != null
null != x
x , (7.2.4)
, HasValue x.
, !x.HasValue,
x.HasValue.
7.9.10 is
is ,
. E is T, E , T ,
, , E T
, .
.
E ,
E null E
E null, false.
D E .
o
E , D ,
E.
E , D .
E , D E.
D T .
o
T , true, D T , D
D T D
D T.
T , true, D T.
T , true, T D
false.
, is .
7.9.11 as
as
. (7.6.6) as
. , , null.
E as T E , T ,
, , . ,
.
.
(Microsoft Corp.), 1999-2007. .
213
E T (6.1.1),
(6.1.6), (6.1.7),
(6.2.4) (6.2.5).
E T .
E null.
E as T ,
E is T ? (T)(E) : (T)null
, E .
E as T,
.
, ,
as .
class X
{
public string F(object o) {
return o as string;
// OK,
}
public T G<T>(object o) where T: Attribute {
return o as T;
// , T
}
public U H<U>(object o) {
return o as U;
}
// , U
T G , . ,
U H , as H .
7.10
&, ^ | .
:
&
:
^
:
|
x op y, op ,
, (7.2.4).
,
.
.
214
7.10.1
:
int operator &(int x, int y);
uint operator &(uint x, uint y);
long operator &(long x, long y);
ulong operator &(ulong x, ulong y);
int operator |(int x, int y);
uint operator |(uint x, uint y);
long operator |(long x, long y);
ulong operator |(ulong x, ulong y);
int operator ^(int x, int y);
uint operator ^(uint x, uint y);
long operator ^(long x, long y);
ulong operator ^(ulong x, ulong y);
& AND , |
OR , ^
OR .
.
7.10.2
E
:
E operator &(E x, E y);
E operator |(E x, E y);
E operator ^(E x, E y);
x op y, x y E U,
op , (E) ((U)x op (U)y).
,
.
7.10.3
:
bool operator &(bool x, bool y);
bool operator |(bool x, bool y);
bool operator ^(bool x, bool y);
215
true, false null.
x
x&y
x|y
true
true
true
true
true
false
false
true
true
null
null
true
false
true
false
true
false
false
false
false
false
null
false
null
null
true
null
true
null
false
false
null
null
null
null
null
7.11
&& || .
.
:
&&
:
||
&& || & |.
x && y x & y, , y ,
x false.
x || y x | y, , y ,
x true.
x && y x || y (7.2.4),
x & y x | y.
, .
, (7.10.3)
(7.10.4), ,
7.11.1.
, ,
7.11.2.
216
.
,
. 7.11.2.
7.11.1
&& || bool ,
operator & operator |, bool,
.
x && y x ? y : false. , x
bool. , x true, y bool
. false.
x || y x ? true : y. , x
bool. , x true, true. y
bool .
7.11.2
&& || ,
operator & operator |, , T ,
.
T.
, AND OR
T T.
- , .
&& || operator true
operator false :
x , y
, .
, operator true operator false, . 11.4.2.
7.12 null
?? null.
(Microsoft Corp.), 1999-2007. .
217
null:
|| null
null a ?? b , a .
a null, a ?? b a, b.
b , a null.
null , ,
. , a ?? b ?? c a ?? (b ?? c).
E1 ?? E2 ?? ... ?? EN , null, null,
null.
a ?? b ,
. a ?? b A0, A B, A a, B b (
, b ), A0 A, A ,
A . , a ?? b .
A ,
.
A b A0,
A0. a. a null, a
A0 . b
A0 .
, b A, A.
a. a null, a .
b A .
, b B A0 B,
B. a. a null, a
A0 ( A A0 ),
B . b .
a b .
7.13
?: . .
:
null
null ? :
b ? x : y b. , b true,
x . y
. x, y.
, ,
. , a ? b : c ? d : e a ? b : (c ? d : e).
?: ,
bool, , operator true.
, .
?: . X Y
.
218
X Y , .
, X Y (6.1), Y X ,
Y.
, Y X (6.1), X Y ,
X.
, .
b ? x : y .
b b:
o
b bool,
.
operator true,
b.
true, x
.
y
.
7.14
, .
,
.
. , ,
, ,
,
.
,
: _ __.
_ , __,
.
-:
=>
:
delegate
:
:
( )
,
219
:
ref
out
:
( )
,
:
=> , ( =) .
_ .
.
,
, ,
, (6.5).
.
( ) =>
:
=>
__
. , ,
,
out.
.
x => x + 1
// ,
x => { return x + 1; }
// ,
(int x) => x + 1
// ,
// ,
(x, y) => x * y
//
() => Console.WriteLine()
//
//
delegate { return 1 + 1; }
//
_ __
:
220
__ ,
.
_ ,
__ .
_ ,
__ .
_ _,
__ (4.6).
7.14.1
__ ()
.
__. (3.7) ( )
(3.3). ,
, ,
__ _,
.
___,
,
. (6.6)
.
__,
, out.
, __
. __ ,
.
, , ,
(4.6).
7.14.2
( ) .
, , ,
. ,
(6.5), .
ref out,
, , ref out (
) .
this , this
. ( this.x) (. x, x
). ,
.
(7.14.4) .
,
_ __ (7.14.5).
221
return
, -. , return,
,
_ __ (6.5).
, ,
- __. ,
, .
,
.
7.14.3
.
. 7.4.2.3.
222
orderDetails.Sum Sum,
d => d.UnitPrice * d.UnitCount double.
Sum.
7.14.4
, ,
_ __,
. - this
, -.
,
.
, (5.1.7).
, ,
, .
using System;
delegate int D();
class Test
{
static D F() {
int x = 0;
D result = () => ++x;
return result;
}
x
, , F, (
).
x, :
1
2
3
,
(18.3),
. ,
, fixed,
.
223
, ,
. , x
, .
static void F() {
for (int i = 0; i < 3; i++) {
int x = i * 2 + 1;
...
}
}
x , x
.
static void F() {
int x;
for (int i = 0; i < 3; i++) {
x = i * 2 + 1;
...
}
}
, ,
, .
.
, .
using System;
delegate void D();
class Test
{
static D[] F() {
D[] result = new D[3];
for (int i = 0; i < 3; i++) {
int x = i * 2 + 1;
result[i] = () => { Console.WriteLine(x); };
}
return result;
}
static void Main() {
foreach (D d in F()) d();
}
}
1
3
5
x :
224
2
static D[] F() {
D[] result = new D[3];
int x;
for (int i = 0; i < 3; i++) {
x = i * 2 + 1;
result[i] = () => { Console.WriteLine(x); };
}
return result;
}
5
5
5
for ,
. , ,
static D[] F() {
D[] result = new D[3];
for (int i = 0; i < 3; i++) {
result[i] = () => { Console.WriteLine(i); };
}
return result;
}
.
3
3
3
. , F
static D[] F() {
D[] result = new D[3];
int x = 0;
for (int i = 0; i < 3; i++) {
int y = 0;
result[i] = () => { Console.WriteLine("{0} {1}", ++x, ++y); };
}
return result;
}
x, y. :
11
21
31
.
:
using System;
delegate void Setter(int value);
225
class Test
{
static void Main() {
int x = 0;
Setter s = (int value) => { x = value; };
Getter g = () => { return x; };
s(5);
Console.WriteLine(g());
s(10);
Console.WriteLine(g());
}
}
x
. :
5
10
7.14.5
F D E
, new D(F).
, 6.5.
7.15
, , SQL XQuery.
:
from
from:
from in
:
select group
:
:
from
let
where
join
join into
orderby
let:
let =
where:
where
join:
join in on equals
226
join into:
join in on equals into
orderby:
orderby
:
,
:
:
ascending
descending
select group:
select
group
select:
select
group:
group by
:
into
from select group.
from from, let, where, join orderby.
from , ,
. let ,
, .
where .
join ,
. orderby
. select group
. , into ,
.
7.15.1
,
, . , from,
where, join, on, equals, into, let, orderby, ascending, descending, select, group by.
,
,
.
, from
, ;, = ,.
,
@ (2.4.2).
227
7.15.2
C# .
, (7.15.3).
, Where, Select, SelectMany,
Join, GroupJoin, OrderBy, OrderByDescending, ThenBy, ThenByDescending, GroupBy Cast.
, 7.15.3.
,
, .
,
- .
, C# .
. , , ,
.
,
. :
,
.
,
*. 7.15.2.7.
select groupby
from into x
from x in ( from )
, into.
from c in customers
group c by c.Country into g
select new { Country = g.Key, CustCount = g.Count() }
from g in
from c in customers
group c by c.Country
select new { Country = g.Key, CustCount = g.Count() }
customers.
GroupBy(c => c.Country).
Select(g => new { Country = g.Key, CustCount = g.Count() })
from,
from T x in e
228
from x in ( e ) . Cast < T > ( )
join,
join T x in e on k1 equals k2
join x in ( e ) . Cast < T > ( ) on k1 equals k2
,
.
from c in customers.Cast<Customer>()
where c.City == "London"
select c
customers.
Cast<Customer>().
Where(c => c.City == "London")
,
IEnumerable,
IEnumerable<T>. , customers
ArrayList.
from x in e select x
( e ) . Select ( x => x )
from c in customers
select c
customers.Select(c => c)
.
, ,
. ,
,
, .
, ,
Select . , Select
, , .
(Microsoft Corp.), 1999-2007. .
229
( e1 ) . SelectMany( x1 => e2 , ( x1 , x2 ) => v )
from, , select,
from x1 in e1
from x2 in e2
from * in ( e1 ) . SelectMany( x1 => e2 , ( x1 , x2 ) => new { x1 , x2 } )
let
from x in e
let y = f
from * in ( e ) . Select ( x => new { x , y = f } )
where
from x in e
where f
from x in ( e ) . Where ( x => f )
( e1 ) . Join( e2 , x1 => k1 , x2 => k2 , ( x1 , x2 ) => v )
from * in ( e1 ) . Join(
e2 , x1 => k1 , x2 => k2 , ( x1 , x2 ) => new { x1 , x2 })
230
join into,
select,
from x1 in e1
join x2 in e2 on k1 equals k2 into g
select v
( e1 ) . GroupJoin( e2 , x1 => k1 , x2 => k2 , ( x1 , g ) => v )
join into, ,
select,
from x1 in e1
join x2 in e2 on k1 equals k2 into g
from * in ( e1 ) . GroupJoin(
e2 , x1 => k1 , x2 => k2 , ( x1 , g ) => new { x1 , g })
orderby
from x in e
orderby k1 , k2 , , kn
from x in ( e ) .
OrderBy ( x => k1 ) .
ThenBy ( x => k2 ) .
.
ThenBy ( x => kn )
descending,
OrderByDescending ThenByDescending.
, let, where,
join orderby, from.
from c in customers
from o in c.Orders
select new { c.Name, o.OrderID, o.Total }
.
SelectMany(c => c.Orders,
(c,o) => new { c.Name, o.OrderID, o.Total }
)
from c in customers
from o in c.Orders
orderby o.Total descending
select new { c.Name, o.OrderID, o.Total }
(Microsoft Corp.), 1999-2007. .
231
from * in customers.
SelectMany(c => c.Orders, (c,o) => new { c, o })
orderby o.Total descending
select new { c.Name, o.OrderID, o.Total }
customers.
SelectMany(c => c.Orders, (c,o) => new { c, o }).
OrderByDescending(x => x.o.Total).
Select(x => new { x.c.Name, x.o.OrderID, x.o.Total })
x , ,
.
from o in orders
let t = o.Details.Sum(d => d.UnitPrice * d.Quantity)
where t >= 1000
select new { o.OrderID, Total = t }
from * in orders.
Select(o => new { o, t = o.Details.Sum(d => d.UnitPrice * d.Quantity) })
where t >= 1000
select new { o.OrderID, Total = t }
orders.
Select(o => new { o, t = o.Details.Sum(d => d.UnitPrice * d.Quantity) }).
Where(x => x.t >= 1000).
Select(x => new { x.o.OrderID, Total = x.t })
x , ,
.
from c in customers
join o in orders on c.CustomerID equals o.CustomerID
select new { c.Name, o.OrderDate, o.Total }
customers.Join(orders, c => c.CustomerID, o => o.CustomerID,
(c, o) => new { c.Name, o.OrderDate, o.Total })
from c in customers
join o in orders on c.CustomerID equals o.CustomerID into co
let n = co.Count()
where n >= 10
select new { c.Name, OrderCount = n }
232
2
from * in customers.
GroupJoin(orders, c => c.CustomerID, o => o.CustomerID,
(c, co) => new { c, co })
let n = co.Count()
where n >= 10
select new { c.Name, OrderCount = n }
customers.
GroupJoin(orders, c => c.CustomerID, o => o.CustomerID,
(c, co) => new { c, co }).
Select(x => new { x, n = x.co.Count() }).
Where(y => y.n >= 10).
Select(y => new { y.x.c.Name, OrderCount = y.n)
x y , ,
.
from o in orders
orderby o.Customer.Name, o.Total descending
select o
orders.
OrderBy(o => o.Customer.Name).
ThenByDescending(o => o.Total)
select
from x in e select v
( e ) . Select ( x => v )
, v x,
(e)
:
from c in customers.Where(c => c.City == London)
select c
customers.Where(c => c.City == London)
groupby
from x in e group v by k
( e ) . GroupBy ( x => k , x => v )
, v x,
( e ) . GroupBy ( x => k )
(Microsoft Corp.), 1999-2007. .
233
from c in customers
group c.Name by c.Country
.
GroupBy(c => c.Country, c => c.Name)
,
*. ,
.
,
. .
,
.
,
.
, .
, ,
. C#
, .
,
.
from c in customers
from o in c.Orders
orderby o.Total descending
select new { c.Name, o.Total }
from * in customers.
SelectMany(c => c.Orders, (c,o) => new { c, o })
orderby o.Total descending
select new { c.Name, o.Total }
customers.
SelectMany(c => c.Orders, (c,o) => new { c, o }).
OrderByDescending(* => o.Total).
Select(* => new { c.Name, o.Total })
customers.
SelectMany(c => c.Orders, (c,o) => new { c, o }).
OrderByDescending(x => x.o.Total).
Select(x => new { x.c.Name, x.o.Total })
234
x , ,
.
from c in customers
join o in orders on c.CustomerID equals o.CustomerID
join d in details on o.OrderID equals d.OrderID
join p in products on d.ProductID equals p.ProductID
select new { c.Name, o.OrderDate, p.ProductName }
from * in customers.
Join(orders, c => c.CustomerID, o => o.CustomerID,
(c, o) => new { c, o })
join d in details on o.OrderID equals d.OrderID
join p in products on d.ProductID equals p.ProductID
select new { c.Name, o.OrderDate, p.ProductName }
customers.
Join(orders, c => c.CustomerID, o => o.CustomerID, (c, o) => new { c, o }).
Join(details, * => o.OrderID, d => d.OrderID, (*, d) => new { *, d }).
Join(products, * => d.ProductID, p => p.ProductID, (*, p) => new { *, p }).
Select(* => new { c.Name, o.OrderDate, p.ProductName })
customers.
Join(orders, c => c.CustomerID, o => o.CustomerID,
(c, o) => new { c, o }).
Join(details, x => x.o.OrderID, d => d.OrderID,
(x, d) => new { x, d }).
Join(products, y => y.d.ProductID, p => p.ProductID,
(y, p) => new { y, p }).
Select(z => new { z.y.x.c.Name, z.y.x.o.OrderDate, z.p.ProductName })
x, y z , ,
.
7.15.3
,
.
,
. ,
, ,
, .
C<T>,
. ,
,
.
delegate R Func<T1,R>(T1 arg1);
235
class C
{
public C<T> Cast<T>();
}
class C<T> : C
{
public C<T> Where(Func<T,bool> predicate);
public C<U> Select<U>(Func<T,U> selector);
public C<V> SelectMany<U,V>(Func<T,C<U>> selector,
Func<T,U,V> resultSelector);
public C<V> Join<U,K,V>(C<U> inner, Func<T,K> outerKeySelector,
Func<U,K> innerKeySelector, Func<T,U,V> resultSelector);
public C<V> GroupJoin<U,K,V>(C<U> inner, Func<T,K> outerKeySelector,
Func<U,K> innerKeySelector, Func<T,C<U>,V> resultSelector);
public O<T> OrderBy<K>(Func<T,K> keySelector);
public O<T> OrderByDescending<K>(Func<T,K> keySelector);
public C<G<K,T>> GroupBy<K>(Func<T,K> keySelector);
public C<G<K,E>> GroupBy<K,E>(Func<T,K> keySelector,
Func<T,E> elementSelector);
}
class O<T> : C<T>
{
public O<T> ThenBy<K>(Func<T,K> keySelector);
}
7.16
, ,
.
:
236
:
=
+=
-=
*=
/=
%=
&=
|=
^=
<<=
, ,
.
= .
, , .
(
, 10.8.1). 7.16.1.
, =,
. ,
, ,
. 7.16.2.
+= -=
__.
.
7.16.3.
; ,
. , a = b = c a = (b = c).
7.16.1
= .
, .
, ,
.
.
, , .
,
set.
.
x = y .
x , :
o
x .
y x
(6.1).
, x, _,
, , y
, x.
237
, y null
(6.1.6) , y,
, x.
System.ArrayTypeMismatchException.
o
, y, ,
x.
x , :
( x static) ( x
), x,
set.
y x
(6.1).
set x , y,
value.
(12.5) A[]
B[], B A.
_
,
.
string[] sa = new string[10];
object[] oa = sa;
oa[0] = null;
// Ok
oa[1] = "Hello";
// Ok
oa[2] = new ArrayList(); // ArrayTypeMismatchException
System.ArrayTypeMismatchException,
ArrayList string[].
_,
, ,
. ,
. 7.5.4 .
:
struct Point
{
int x, y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public int X {
get { return x; }
set { x = value; }
}
}
238
public int Y {
get { return y; }
set { y = value; }
}
2
struct Rectangle
{
Point a, b;
public Rectangle(Point a, Point b) {
this.a = a;
this.b = b;
}
public Point A {
get { return a; }
set { a = value; }
}
public Point B {
get { return b; }
set { b = value; }
}
}
Point p = new Point();
p.X = 100;
p.Y = 100;
Rectangle r = new Rectangle();
r.A = new Point(10, 10);
r.B = p;
, r.A r.B .
7.16.2
x && y x || y (7.2.4),
x op y.
x,
x = x op y, , x .
,
x y
x , x =
(T)(x op y), T x, , x .
, x op y,
x
x. , A()[B()] += C(), A , int[],
B C , int,
A, B, C.
(Microsoft Corp.), 1999-2007. .
239
,
get, set.
.
(. ) x op= y x = (T)(x op y).
,
, sbyte, byte, short, ushort char.
, int,
7.2.6.2. , .
,
x op= y , x op y x = y.
byte b = 0;
char ch = '\0';
int i = 0;
b
b
b
b
+=
+=
+=
+=
1;
1000;
i;
(byte)i;
// Ok
// , b = 1000
// , b = i
// Ok
ch += 1;
ch += (char)1;
// , ch = 1
// Ok
,
.
, .
int? i = 0;
i += 1;
// Ok
+(int?,int?).
7.16.3
+= -= ,
.
( ).
+= -=
(6.1).
,
. +=,
add; -=, remove.
. ,
_ (8.6).
7.17
__, .
:
240
:
-
7.18
_ ,
.
:
null : sbyte,
byte, short, ushort, int, uint, long, ulong, char, float, double, decimal, bool, string
. :
( null).
, .
, , .
checked unchecked
+, , ! ~.
+, , *, /, %, <<, >>, &, |, ^, &&, ||, ==, !=, <, >, <= >=
, , .
?:.
,
, null.
, ,
, . :
class C {
const object i = 5; // :
const object str = hello; // :
}
241
i , .
str ,
.
, , .
, ,
.
,
, ,
,
.
unchecked, ,
, (7.18).
, .
, .
(10.4).
(14.3).
(7.5.10.4), .
(17).
(6.1.8)
int sbyte, byte, short, ushort, uint ulong ,
.
7.19
_ , bool.
_:
_ _if (8.7.1),
_while (8.8.1), _do (8.8.2) _for (8.8.3).
?: (7.13) , _, -
__.
_ , bool
, operator true. ,
.
, bool,
operator true,
operator true .
DBBool 11.4.2 ,
operator true operator false.
242
8.
C# . ,
C C++.
_:
_
_
_
_:
_
_
_
_
_
_try
_checked
_unchecked
_lock
_using
_yield
_ ,
. _
_
.
void F(bool b) {
if (b)
int i = 44;
}
, if
_ if, _. , i
, . , , ,
i .
8.1
. ,
, .
( , ) ,
, . ,
,
.
, ,
. , ,
.
243
void F() {
Console.WriteLine("reachable");
goto Label;
Console.WriteLine("unreachable");
Label:
Console.WriteLine("reachable");
}
Console.WriteLine ,
.
, .
.
, ,
,
. (7.18),
,
. , ,
.
void F() {
const int i = 1;
if (i == 2) Console.WriteLine("unreachable");
}
if ,
== .
false, Console.WriteLine .
i :
void F() {
int i = 1;
if (i == 2) Console.WriteLine("reachable");
}
Console.WriteLine , .
- .
, .
void F(int x) {
Console.WriteLine("start");
if (x < 0) Console.WriteLine("negative");
}
Console.WriteLine :
- Console.WriteLine , F
.
- Console.WriteLine ,
.
if , - Console.WriteLine
.
- Console.WriteLine ,
if false.
244
switch switch ,
switch
. break.
-, ,
. return.
8.2
,
.
:
{ _ }
_ (8.2.1), .
, , .
(8.5).
, , .
, ,
(7.5.2.1).
.
, .
, .
, .
, .
,
.
, yield (8.14), .
- (10.14).
.
return (
yield return ).
(18.1). ,
.
8.2.1
.
(8.2) _switch (8.7.2).
_:
_
_ _
.
, .
,
.
(Microsoft Corp.), 1999-2007. .
245
, .
goto.
,
.
8.3
_ .
_:
;
, , ,
.
.
, , .
while :
bool ProcessMessage() {...}
void ProcessMessages() {
while (ProcessMessage())
;
}
,
:
void F() {
...
if (done) goto exit;
...
exit: ;
}
8.4
_ .
, .
_:
: _
, .
, , , .
,
.
goto (8.9.3), . ,
goto , .
.
246
2
int F(int x) {
if (x >= 0) goto x;
x = -x;
x: return x;
}
; x , .
, ,
.
,
, goto. ( :
goto try, finally,
try, finally ,
goto.)
8.5
_ .
, .
_:
__ ;
__ ;
8.5.1
__ .
__:
__ __
__:
var
__:
__
__ , __
__:
= __
__:
_
__ __
, , var ,
.
__, .
__ , ,
= __,
.
__ var var
, :
.
.
(Microsoft Corp.), 1999-2007. .
247
__
__.
__ __.
__ .
, .
:
var
var
var
var
var
x;
// ,
y = {1, 2, 3}; // ,
z = null;
// , null
u = x => x + 1; // ,
v = v++;
// ,
_ (7.5.2),
(7.16).
(5.3) , .
, __,
, . ,
__, .
.
. ,
.
void F() {
int x = 1, y, z = x * 2;
}
:
void F() {
int x; x = 1;
int y;
int z; z = x * 2;
}
, . :
var
var
var
var
var
i = 5;
s = "Hello";
d = 1.0;
numbers = new int[] {1, 2, 3};
orders = new Dictionary<int,Order>();
248
2
int i = 5;
string s = "Hello";
double d = 1.0;
int[] numbers = new int[] {1, 2, 3};
Dictionary<int,Order> orders = new Dictionary<int,Order>();
8.5.2
__ .
__:
const _
_:
_
_ , _
_:
= _
__ , .
_, .
_ , ,
= _ (7.18), .
_ ,
- (10.4).
_ (7.5.2).
, .
, _,
.
.
.
8.6 -
_ . ,
( ), .
_:
_ ;
_:
_
__
__
__
__
__
. , , x
+ y x == 1, ( ),
.
_ ,
_.
_ , _.
(Microsoft Corp.), 1999-2007. .
249
8.7
.
_:
_if
_switch
8.7.1 if
if .
_if:
if ( _ ) _
if ( _ ) _ else _
else if,
. , if
if (x) if (y) F(); else G();
:
if (x) {
if (y) {
F();
}
else {
G();
}
}
if .
_ (7.19).
true,
. ,
if.
false else,
.
, if.
false else ,
if.
if , if
false.
if ( ) , if
true.
if ,
. , if else
, if true.
8.7.2 switch
switch ,
switch-.
250
_switch:
switch ( ) _switch
_switch:
{ _switch }
_switch:
_switch
_switch _switch
_switch:
_switch _
_switch:
_switch
_switch _switch
_switch:
case _ :
default :
_switch switch, (
switch-) _switch. _switch (,
) _switch, . _switch
_switch _ (8.2.1).
switch switch-. switch sbyte, byte, short, ushort, int, uint, long, ulong, char, string
_, switch.
(6.4) switch : sbyte, byte, short, ushort, int, uint,
long, ulong, char, string.
, .
case ,
(6.1) switch.
, case switch
.
switch default.
switch .
switch-, .
, case switch,
switch-, , case.
, case switch,
switch- default,
, default.
, case switch,
switch- default ,
switch.
switch ,
. .
251
switch (i) {
case 0:
CaseZero();
break;
case 1:
CaseOne();
break;
default:
CaseOthers();
break;
}
, switch .
C C++, switch switch,
switch (i) {
case 0:
CaseZero();
case 1:
CaseZeroOrOne();
default:
CaseAny();
}
. switch
switch, goto case goto
default:
switch (i) {
case 0:
CaseZero();
goto case 1;
case 1:
CaseZeroOrOne();
goto default;
default:
CaseAny();
break;
}
_switch . :
switch (i) {
case 0:
CaseZero();
break;
case 1:
CaseOne();
break;
case 2:
default:
CaseTwo();
break;
}
, case 2:
default: _switch.
C C++, break. ,
252
switch switch
. , switch
, :
switch (i) {
default:
CaseAny();
break;
case 1:
CaseZeroOrOne();
goto default;
case 0:
CaseZero();
goto case 1;
}
switch string. :
void DoCommand(string command) {
switch (command.ToLower()) {
case "run":
DoRun();
break;
case "save":
DoSave();
break;
case "quit":
DoQuit();
break;
default:
InvalidCommand(command);
break;
}
}
(7.9.7), switch
switch , switch-
case.
switch string, case
null.
_ _switch (8.5).
, switch, switch.
(Microsoft Corp.), 1999-2007. .
253
, ,
switch (7.5.2.1).
switch , switch
.
switch- .
switch- , case
switch.
switch- , case,
switch default.
switch ,
.
switch break,
switch.
switch , switch-
default .
switch , switch- ,
case, default .
8.8
.
_:
_while
_do
_for
_foreach
8.8.1 while
while (, )
.
_while:
while ( _ ) _
while .
_ (7.19).
true,
. (,
continue), while.
false,
while.
while , while
false.
while ,
.
while
true.
8.8.2 do
do
.
_do:
do _ while ( _ ) ;
do .
(,
continue), _ (7.19).
true, do.
do.
do break (8.9.1)
do (. . ),
continue (8.9.2) .
do , do .
do ,
.
do break, do.
true.
8.8.3 for
for ,
,
.
_for:
for ( _for ; _for ; _for )
_
_for:
__
__
_for:
_
_for:
__
255
__:
_
__ , _
_for ( ) __ (8.5.1)
_ (8.6), . ,
_for, __
. _for _for.
_for ( ) _ (7.19).
_for ( ) _ (8.6), .
for .
_for,
. .
_for, .
_for true,
.
(, continue),
_for ( ),
, _for (. ).
_for false,
for.
for _for .
for ,
.
8.8.4 foreach
foreach ,
.
_foreach:
foreach ( __ in )
_
foreach .
__ var, ,
, foreach,
. ,
. foreach
256
,
. (
++ --) ref out,
.
foreach ,
. .
X ,
X System.Collections.IEnumerable ( System.Array
). System.Collections.IEnumerable,
System.Collections.IEnumerator,
X.
, X
GetEnumerator.
o
X GetEnumerator
. , ,
, ,
, . ,
-, .
.
, ,
,
, . ,
-,
.
E GetEnumerator ,
, , .
E Current
. , -,
, , ,
.
E MoveNext
. , -,
, , .
. ,
,
, bool,
, .
X, E,
Current.
T, X
System.Collections.Generic.IEnumerable<T>,
,
System.Collections.Generic.IEnumerator<T>, T.
257
T , , .
X System.Collections.IEnumerable,
,
System.Collections.IEnumerator, object.
, .
, :
C, E T. foreach,
foreach (V v in x) _
:
{
E e = ((C)(x)).GetEnumerator();
try {
V v;
while (e.MoveNext()) {
v = (V)(T)e.Current;
_
}
}
finally {
// e
}
}
e x,
. v .
(6.2) T ( ) V ( foreach),
, . x null,
System.NullReferenceException.
foreach ,
(, ), ,
.
finally .
E System.IDisposable,
finally , :
finally {
((System.IDisposable)e).Dispose();
}
, e ,
; e
System.IDisposable .
E , finally :
finally {
}
finally :
finally {
System.IDisposable d = e as System.IDisposable;
258
2
if (d != null) d.Dispose();
}
d . ,
, finally.
foreach .
, 0 Length 1.
,
.
:
using System;
class Test
{
static void Main() {
double[,] values = {
{1.2, 2.3, 3.4, 4.5},
{5.6, 6.7, 7.8, 8.9}
};
foreach (double elementValue in values)
Console.Write("{0} ", elementValue);
Console.WriteLine();
}
}
:
1.2 2.3 3.4 4.5 5.6 6.7 7.8 8.9
int[] numbers = { 1, 3, 5, 7, 9 };
foreach (var n in numbers) Console.WriteLine(n);
n int numbers.
8.9
.
_:
_break
_continue
_goto
_return
_throw
, , .
, , ,
. ,
.
try.
try .
try .
try,
finally, finally
(Microsoft Corp.), 1999-2007. .
259
try. finally,
finally try. ,
finally try.
using System;
class Test
{
static void Main() {
while (true) {
try {
try {
Console.WriteLine("Before break");
break;
}
finally {
Console.WriteLine("Innermost finally block");
}
}
finally {
Console.WriteLine("Outermost finally block");
}
}
Console.WriteLine("After break");
}
}
finally,
try.
:
Before break
Innermost finally block
Outermost finally block
After break
8.9.1 break
break switch, while, do, for
foreach.
_break:
break ;
260
break try,
finally, finally
try. finally,
finally try.
, finally try.
break.
break ,
break .
8.9.2 continue
continue while, do, for
foreach.
_continue:
continue ;
continue
while, do, for foreach. continue
while, do, for foreach, .
while, do, for foreach , continue
.
goto (8.9.3).
continue finally (8.10). continue
finally, continue finally,
.
continue .
continue try,
finally, finally
try. finally,
finally try.
, finally
try.
continue.
continue ,
continue .
8.9.3 goto
goto , .
_goto:
goto ;
goto case _ ;
goto default ;
goto .
- goto
, .
goto ,
.
using System;
(Microsoft Corp.), 1999-2007. .
261
class Test
{
static void Main(string[] args) {
string[,] table = {
{"Red", "Blue", "Green"},
{"Monday", "Wednesday", "Friday"}
};
foreach (string str in args) {
int row, colm;
for (row = 0; row <= 1; ++row)
for (colm = 0; colm <= 2; ++colm)
if (str == table[row,colm])
goto done;
Console.WriteLine("{0} not found", str);
continue;
done:
}
}
}
goto .
goto case
switch (8.7.2), case . goto
case switch, _
(6.1) switch,
switch case
, .
goto default
switch (8.7.2), default. goto default
switch, switch default,
.
goto finally (8.10). goto
finally, goto finally,
.
goto .
goto try,
finally, finally
try. finally,
finally try.
, finally try.
goto.
goto ,
goto .
8.9.4 return
return , -,
return.
_return:
return ;
262
return -,
, . . void, set
, add remove , ,
.
return -, ,
. . , void, get
, .
(6.1) -,
.
return finally (8.10) .
return .
return , ,
-,
. ,
.
return try,
finally, finally try.
finally,
finally try. ,
finally try.
-, .
return ,
return .
8.9.5 throw
throw .
_throw:
throw ;
throw , .
System.Exception, ,
System.Exception, ,
System.Exception ( ). null,
System.NullReferenceException.
throw catch;
, catch.
throw ,
throw .
catch
try, . ,
,
.
catch, .
.
- try, .
S, try
try, .
(Microsoft Corp.), 1999-2007. .
263
try S S
catch, catch
. catch,
, .
catch (8.10) .
catch ,
catch.
, try catch S
S finally, finally. finally
, .
, finally,
.
-,
. , -,
, -.
-
, , ,
. .
8.10 try
try , .
, try ,
try.
_try:
try _catch
try _finally
try _catch _finally
_catch:
__catch __catch
__catch __catch
__catch:
__catch
__catch __catch
__catch:
catch ( _ )
__catch:
catch
_finally:
finally
try:
try, catch;
try, finally;
catch _, System.Exception,
System.Exception ,
System.Exception ( ).
264
catch _, ,
.
, catch. catch
.
,
.
catch ,
catch.
catch, , ,
catch. try
catch, , catch.
,
, System.Exception,
C#. catch . ,
catch
System.Exception ,
.
catch
. catch , catch
try ( ), .
catch.
catch throw (8.9.5)
, catch.
.
using System;
class Test
{
static void F() {
try {
G();
}
catch (Exception e) {
Console.WriteLine("Exception in F: " + e.Message);
e = new Exception("F");
throw;
// re-throw
}
}
static void G() {
throw new Exception("G");
}
static void Main() {
try {
F();
}
catch (Exception e) {
Console.WriteLine("Exception in Main: " + e.Message);
}
}
}
(Microsoft Corp.), 1999-2007. .
265
F , ,
.
, :
Exception in F: G
Exception in Main: G
catch e ,
:
Exception in F: G
Exception in Main: F
try.
try, :
o
try.
try try,
.
o
catch, ,
. catch,
, .
catch . catch
, :
catch ,
;
catch;
catch:
try;
catch try:
o
try.
try.
finally try.
, ,
266
try catch;
finally, .
8.12 lock
lock ,
.
_lock:
lock ( ) _
lock ,
_. lock
(6.1.7); ,
_, .
lock :
lock (x) ...
x _,
System.Threading.Monitor.Enter(x);
try {
...
}
finally {
System.Threading.Monitor.Exit(x);
}
267
, x .
, ,
. , ,
, .
System.Type
. , .
. :
class Cache
{
private static object synchronizationObject = new object();
public static void Add(object x) {
lock (Cache.synchronizationObject) {
...
}
}
public static void Remove(object x) {
lock (Cache.synchronizationObject) {
...
}
}
}
8.13 using
using ,
.
_using:
using ( _ ) _
_:
__
, System.IDisposable,
Dispose. , ,
Dispose, , . Dispose,
.
_ __,
__ System.IDisposable ,
System.IDisposable. _ ,
System.IDisposable ,
System.IDisposable.
, _,
.
( ++ --),
ref out, .
using : , .
try finally.
finally . null, Dispose
.
268
using
using (ResourceType resource = expression) statement
. ResourceType ,
:
{
ResourceType resource = expression;
try {
statement;
}
finally {
((IDisposable)resource).Dispose();
}
}
ResourceType , :
{
ResourceType resource = expression;
try {
statement;
}
finally {
if (resource != null) ((IDisposable)resource).Dispose();
}
}
resource .
using
using (expression) statement
, ResourceType
expression, , resource
.
_ __,
. using
using (ResourceType r1 = e1, r2 = e2, ..., rN = eN) statement
using:
using (ResourceType r1 = e1)
using (ResourceType r2 = e2)
...
using (ResourceType rN = eN)
statement
log.txt .
, .
using System;
using System.IO;
269
class Test
{
static void Main() {
using (TextWriter w = File.CreateText("log.txt")) {
w.WriteLine("This is line one");
w.WriteLine("This is line two");
}
using (TextReader r = File.OpenText("log.txt")) {
string s;
while ((s = r.ReadLine()) != null) {
Console.WriteLine(s);
}
}
}
8.14 yield
yield (8.2)
(10.14.4) (10.14.5),
.
_yield:
yield return ;
yield break ;
yield ; ,
return break. yield
.
yield .
yield ( ) _, _
__ .
yield ( )
.
yield.
delegate IEnumerable<int> D();
270
2
IEnumerator<int> GetEnumerator() {
try {
yield return 1;
//
yield break;
//
}
finally {
yield return 2;
// , yield finally
yield break;
// , yield finally
}
try {
yield
yield
}
catch {
yield
yield
}
return 3;
break;
return 4;
break;
D d = delegate {
yield return 5;
};
// , yield
}
int MyMethod() {
yield return 1;
}
// ,
, ,
Current .
. yield return
try, finally .
MoveNext true ,
.
MoveNext
, .
yield break .
, . MoveNext,
Dispose .
yield ,
yield .
271
9.
C# .
,
, .
using (9.4) .
9.1
_ . 0
_using, 0 _,
0 ___.
_:
_extern_alias _using _
___
C# ,
. C#
. , ,
.
_using _
___ ,
.
_ (17)
. .
.
___
, . :
A.cs:
class A {}
B.cs:
class B {}
,
A B.
, ,
.
9.2
__ namespace,
, ().
__:
namespace _ __ ;
272
_:
_ .
__:
{ _extern_alias _using
___ }
__ _
__.
__ _,
.
__ __,
.
.
( public);
.
__ _using
, ,
. ___
. ,
_using.
_ __
, ..
. :
namespace N1.N2
{
class A {}
}
class B {}
namespace N1
{
namespace N2
{
class A {}
class B {}
}
}
,
(3.3).
namespace N1.N2
{
class A {}
}
namespace N1.N2
{
class B {}
}
273
,
N1.N2.A N1.N2.B.
, ,
.
9.3
_extern_alias , .
.
_extern_alias:
_extern_alias
_extern_alias _extern_alias
_extern_alias:
extern alias ;
_extern_alias _using, _
___
.
, _extern_alias,
, _extern_alias,
. global,
.
_extern_alias
, . ,
_extern_alias ,
, .
, X Y,
:
extern alias X;
extern alias Y;
class Test
{
X::N.A a;
X::N.B b1;
Y::N.B b2;
Y::N.C c;
}
X Y,
. N.B
X.N.B Y.N.B,
X::N.B Y::N.B. ,
, .
9.4 using
using ,
. using
____ (3.8) _ (7.5.2), , ,
using
, .
274
_using:
_using
_using _using
_using:
_using_alias
_using_namespace
_using_alias (9.4.1) .
_using_namespace (9.4.2) .
_using ___
.
_using _using. ,
_using , .
9.4.1 using alias
_using_alias ,
.
_using_alias:
using = ____ ;
,
_using_alias, , _using_alias,
. :
namespace N1.N2
{
class A {}
}
namespace N3
{
using A = N1.N2.A;
}
class B: A {}
N3, A
N1.N2.A, N3.B N1.N2.A.
, R N1.N2 R.A:
namespace N3
{
using R = N1.N2;
}
class B: R.A {}
_using_alias
, _using_alias.
:
namespace N3
{
class A {}
}
275
namespace N3
{
using A = N1.N2.A;
}
// : A
N3 A,
_using_alias . ,
, _using_alias
.
_using_alias
, . ,
_using_alias ,
, .
namespace N3
{
using R = N1.N2;
}
namespace N3
{
class B: R.A {}
}
// : R
_using_alias, R,
, , R
. _using_alias ,
:
using R = N1.N2;
namespace N3
{
class B: R.A {}
}
namespace N3
{
class C: R.A {}
}
, , _using_alias,
.
using R = N1.N2;
namespace N3
{
class R {}
class B: R.A {}
// : R A
R.A B , R N3.R,
N1.N2.
, _using_alias, ,
____, _using_alias,
_using_alias, _using
. , ____ _extern_alias
,
276
//
using R2 = N1;
//
using R3 = N1.N2;
//
using R4 = R2.N2;
// : R2
_using_alias ,
_using_alias. _using_alias ,
E _using_alias.
_using_alias ,
, , ,
.
,
. , :
namespace N1.N2
{
class A {}
}
namespace N3
{
using R1 = N1;
using R2 = N1.N2;
class B
{
N1.N2.A a;
R1.N2.A b;
R2.A c;
}
// N1.N2.A
// N1.N2.A
// N1.N2.A
namespace N2
{
using W = N1.A;
// :
277
using X = N1.A.B;
// :
using Y = N1.A<int>;
// ,
class B: A {}
N3 N1.N2
, N3.B N1.N2.A.
_using_namespace , ,
.
namespace N1.N2
{
class A {}
}
namespace N3
{
using N1;
class B: N2.A {}
// : N2
_using_namespace , N1, ,
N1. , N2.A B
, N2.
_using_alias, _using_namespace ,
. , , _using_namespace,
.
:
namespace N1.N2
{
class A {}
278
2
class B {}
}
namespace N3
{
using N1.N2;
}
class A {}
N3, A N3.A,
N1.N2.A.
, _using_namespace
, ,
.
namespace N1
{
class A {}
}
namespace N2
{
class A {}
}
namespace N3
{
using N1;
using N2;
class B: A {}
// : A
}
N1 N2 A, N3 , A N3
. A,
_using_alias, A. :
namespace N3
{
using N1;
using N2;
using A = N1.A;
class B: A {}
// A N1.A
_using_alias, _using_namespace
,
, .
__, _using_namespace,
, ____, _using_alias.
, _using_namespace
.
9.5
___ __ (9.2),
_ (9.6).
(Microsoft Corp.), 1999-2007. .
279
___:
___
___ ___
___:
__
_
___,
.
9.6
_ _ (10.1), _ (11.1),
_ (13.1), __ (14.1) _
(15.1).
_:
_
_
_
__
_
_
, .
T ,
T. T
, , N.T, N
, .
, , (10.3.8).
,
(3.5.1):
, , public
internal. internal;
9.7
:: ,
.
, .
., ::
extern using.
__ :
__:
:: __
280
__ ____ (3.8)
__ (7.5.4).
__ :
N::I, N I . ( K .)
__
:
N global, I :
o
I, K ,
__ ;
, I, K
, __ ;
, I,
K , __ ,
;
__ .
, (9.2),
__ ( ),
( ), ,
__, , :
o
_using_alias, N , __
;
,
_extern_alias _using_alias, N , :
, N, I, K
, __ ;
, , N,
I, K , __ ;
, , N, I,
K , __ ,
;
__ .
__ .
, ,
, . , N
global, ,
using, global .
9.7.1
extern using. , extern using
(Microsoft Corp.), 1999-2007. .
281
extern using,
, ,
, :: .
namespace N
{
public class A {}
}
public class B {}
namespace N
{
using A = System.IO;
class X
{
A.Stream s1;
A::Stream s2;
// : A
//
}
}
A , A, A
. A A.Stream
. A :: ,
A .
282
10. Classes
, ( ),
(, , , , , ,
) .
, .
10.1 .
_ _ (9.6), .
_:
(/) _ (/) partial (/) class
__ (/)
_ (/) ___ (/) _ ;
(/)
_ (17),
_ (10.1.1), partial, class
, , __ (10.1.3),
_ (10.1.4),
___ (10.1.5), _ (10.1.6)
.
___,
__.
, __,
. ,
, ,
.
10.1.1 .
_ .
_:
_
_ _
_:
new
public
protected
internal
private
abstract
sealed
static
.
new .
10.3.4. new
, ,
.
public, protected, internal private .
, ,
(3.5.1).
abstract, sealed static .
.
abstract
.
.
, new
.
, ,
null
, .
( ) .
,
,
. :
abstract class A
{
public abstract void F();
}
abstract class B: A
{
public void G() {}
}
class C: B
{
public override void F() {
// F
}
}
A F. B
G, F, B
. C F .
C , C ( ) .
.
sealed .
,
.
.
sealed
, .
284
, - ,
-
.
.
static , .
,
.
(10.6.9).
.
sealed abstract.
, ,
, .
_ (10.1.4)
.
object.
(10.3.7). ,
.
protected
protected internal.
.
.
; (10.11.4)
.
,
static ( ).
, ,
static.
10.1.1.3.1 .
____ (3.8)
.
_ (7.5) .
_ = E __ (7.5.4) E.I.
.
,
, (10.3.8) ,
. ,
, new, , is, as, sizeof
.
285
10.1.2 partial.
partial , _
.
,
10.2.
. ,
, .
.
10.1.3 .
, ,
.
, . , (4.4.1)
, .
__:
< _ >
_:
(/) _
_ , (/) _
_:
(3.3) .
,
. .
10.1.4 .
_,
(13), .
_:
: _
: __
: _ , __
__:
_
__ , _
, , (4.4).
,
.
class Extend<V>: V {}
// ,
.
_ _, .
_ _ ,
object.
(. 10.3.3).
286
:
class A {}
class B: A {}
A B, B
A. A ,
object.
, ,
_
_ .
class B<U,V> {...}
class G<T>: B<string,T[]> {...}
G<int> B<string,int[]>.
, (3.5.2).
, public private internal,
.
: System.Array,
System.Delegate, System.MulticastDelegate, System.Enum System.ValueType. ,
System.Attribute
.
.
,
. , B A object. :
class A {...}
class B<T>: A {...}
class C<T>: B<IComparable<T>> {...}
class D<T>: C<T[]> {...}
, . ,
class A: B.C {}
(Microsoft Corp.), 1999-2007. .
287
class B: A
{
public class C {}
}
, A B.C (
), B ( ), A,
.
, , . :
class A
{
class B: A {}
}
B A ( A
), A B ( B
A). .
sealed.
sealed class A {}
class B: A {}
// ,
B , A
sealed.
.
_ , ,
. 13.4.
10.1.5 .
,
___.
___:
___
___
___
_ _:
_ : __
__:
_
_
_
_ , _
_ , _
_ , _
_ , _ , _
_:
_
class
struct
288
_:
_
_
_ , _
_ , _
_:
new ( )
___ where,
, .
where ,
where . get set
, where .
where
: ,
, new().
class
struct. _
_.
, , ,
. , , ,
(. ), .
, , ,
. ,
. , ,
, (4.1.9)
.
_.
.
, ,
,
.
.
,
, .
_ .
sealed.
object. object,
.
, _, :
.
(Microsoft Corp.), 1999-2007. .
289
where.
, .
, ,
(3.5.4), .
, _, .
where.
, ,
, .
T S, S
T.
S T T U, S
U.
, (
), .
. S
T, .
T . T
, S , T,
S , T _.
S _ A T _ B,
A B ( B
A).
S U, U _ A T
_ B,
A B ( B A).
S T .
T System.Object, System.ValueType, System.Enum
.
where ( new()),
new (7.5.10.1). ,
,
( )
(
. 10.1.5).
.
interface IPrintable
{
void Print();
}
290
2
interface IComparable<T>
{
int CompareTo(T value);
}
interface IKeyProvider<T>
{
T GetKey();
}
class Printer<T> where T: IPrintable {...}
class SortedList<T> where T: IComparable<T> {...}
class Dictionary<K,V>
where K: IComparable<K>
where V: IPrintable, IKeyProvider<K>, new()
{
...
}
,
.
class Circular<S,T>
where S: T
where T: S
{
...
}
// ,
.
class Sealed<S,T>
where S: T
where T: struct
{
...
}
// , T
class A {...}
class B {...}
class Incompat<S,T>
where S: A, T
where T: B
{
...
}
// ,
class StructWithClass<S,T,U>
where S: struct, T
where T: U
where U: A
// , A
{
...
}
T .
T ,
object.
(Microsoft Corp.), 1999-2007. .
291
T ,
System.ValueType.
T _ C, _,
C.
T _, _,
(6.4.2)
_.
.
T _ _,
(6.4.2) ,
_ T _.
.
T , _,
object.
T .
T _, .
T _, _ ,
_.
T _, _,
_.
T _ _,
_
_.
, ,
object System.ValueType.
, . :
interface IPrintable
{
void Print();
}
class Printer<T> where T: IPrintable
{
void PrintOne(T x) {
x.Print();
}
}
IPrintable x, T
IPrintable.
10.1.6 .
_ .
_:
{ __opt }
292
10.2 .
.
,
.
, ,
partial. partial
,
class, struct interface void .
.
partial.
,
. partial , ,
;
partial.
,
.
.
partial. - partial,
-.
partial .
10.2.1 .
. ,
. , :
[Attr1, Attr2("hello")]
partial class A {}
[Attr3, Attr2("goodbye")]
partial class A {}
:
[Attr1, Attr2("hello"), Attr3, Attr2("goodbye")]
class A {}
.
10.2.2 .
( public,
protected, internal private), ,
.
, (3.5.1).
new,
, (3.7.1.2).
abstract,
(10.1.1.1). .
sealed,
(10.1.1.2). .
(Microsoft Corp.), 1999-2007. .
293
, .
unsafe
(18.1).
10.2.3 .
,
.
( ).
( where),
, .
, , ,
, ,
. ,
.
, .
:
partial class Dictionary<K,V>
where K: IComparable<K>
where V: IKeyProvider<K>, IPersistable
{
...
}
partial class Dictionary<K,V>
where V: IPersistable, IKeyProvider<K>
where K: IComparable<K>
{
...
}
partial class Dictionary<K,V>
{
...
}
, , ( ),
,
.
10.2.4 .
,
, .
,
System.Object (10.1.4.1).
10.2.5 .
, ,
, .
,
.
.
294
2
partial class C: IA, IB {...}
partial class C: IC {...}
partial class C: IA, IB {...}
C IA, IB IC.
, ,
. ,
.
partial class X
{
int IComparable.CompareTo(object o) {...}
}
partial class X: IComparable
{
...
}
10.2.6 .
(10.2.7), , ,
, .
(3.3), (3.7)
. ;
private, , .
,
partial.
partial class A
{
int x;
partial class A
{
int x;
// , x
// , Inner
// , x
// , Inner
, C#,
. ,
, .
10.2.7 .
.
. ,
,
.
(Microsoft Corp.), 1999-2007. .
295
, private.
void,
out. partial
, void,
.
.
.
, .
, .
,
. ,
,
.
( ),
, .
( ) (
).
(
).
,
.
. ,
,
. void,
. ,
private, ,
.
, , ,
. , ,
, .
.
.
, .
partial .
.
.
. .
M ,
, .
296
(7.5.10.5).
M , ,
(6.5.2).
, M,
(5.3),
.
M (3.1).
,
(, ).
:
partial class Customer
{
string name;
public string Name {
get { return name; }
set {
OnNameChanging(value);
name = value;
OnNameChanged();
}
}
partial void OnNameChanging(string newName);
}
- ,
,
.
class Customer
{
string name;
public string Name {
get { return name; }
, ,
:
297
:
class Customer
{
string name;
public string Name {
get { return name; }
set {
OnNameChanging(value);
name = value;
OnNameChanged();
}
}
void OnNameChanging(string newName)
{
Console.WriteLine( + name + + newName);
}
void OnNameChanged()
{
Console.WriteLine( + name);
}
10.2.8 .
,
, .
using (9.4).
(7.5.2) using
, .
.
namespace N
{
using List = System.Collections.ArrayList;
partial class A
{
List x;
}
// x System.Collections.ArrayList
namespace N
{
using List = Widgets.LinkedList;
}
298
partial class A
{
List y;
}
// y Widgets.LinkedList
10.3 .
, __ ,
.
__:
__
__ __
__:
_
_
_
_
_
_
_
_
_
__
_
.
, , (10.4).
, (10.5).
, , (10.6).
, ,
(10.7).
, , (10.8).
, (
) (10.9).
, ,
(10.9).
, ,
(10.10)
, ,
(10.11).
, ,
(10.12)
, (10.3.8).
, , .
, , , , ,
, .
_ (3.3), __,
_,
. __ .
299
,
. ,
.
, , , ,
.
, ,
. , (3.6) ,
, , , ,
ref out.
, , ,
, , ref out.
,
.
,
.
(10.3.3) .
,
( ).
10.3.1 .
(4.4.3) .
(4.4)
,
. ,
, , . . .
this , .
.
.
class A<T>
{
class B {}
}
// : A<T>
// : A<T>.B
class C<U> {}
class D {}
// : A<T>.C<U>
// : D
10.3.2 .
.
.
,
class Gen<T,U>
{
public T[,] a;
public void G(int i, T t, Gen<U,T> gt) {...}
300
2
public U Prop { get {...} set {...} }
}
Gen<int[],IComparable<string>>
public int[,][] a;
public void G(int i, int[] t, Gen<IComparable<string>,int[]> gt) {...}
public IComparable<string> Prop { get {...} set {...} }
public int H(double d) {...}
a Gen T,
a int,
int[,][].
this (10.3.1)
.
.
(4.4.2) ,
, . :
class C<V>
{
public V f1;
public C<V> f2 = null;
public C(V x) {
this.f1 = x;
this.f2 = this;
}
}
class Application
{
static void Main() {
C<int> x1 = new C<int>(1);
Console.WriteLine(x1.f1);
// 1
10.3.3 .
. ,
, ,
.
.
. C B, B A, C
, B, , A.
.
, .
, ,
, , (3.5). ,
301
,
.
(3.7.1.2)
. ,
, ,
.
,
, (6.1.6)
. ,
- .
, ,
.
, , ,
, .
(10.1.4.1),
__.
, _
____.
class B<U>
{
public U F(long index) {...}
}
class D<T>: B<T[]>
{
public T G(string s) {...}
}
__ - ,
private.
10.3.6 .
, , .
, , , ,
, ,
. , (3.5.4).
10.3.7 .
, .
, ,
( ).
, , , ,
static, . ,
. .
M _ (7.5.4) E.M, E
, M. E .
.
.
(, , , )
, this
.
, , , , ,
static, . (
.) .
M _ (7.5.4) E.M, E
- M. E .
(, , , )
, this
(7.5.7).
:
class Test
{
int x;
static int y;
void F() {
x = 1;
y = 1;
}
// O, this.x = 1
// O, Test.y = 1
303
Test.x = 1;
// ,
Test.y = 1;
// O
}
}
F , _ (7.5.2)
. G ,
_ .
Main , _ (7.5.4) ,
.
10.3.8 .
, , . ,
, .
:
using System;
class A
{
class B
{
static void F() {
Console.WriteLine("A.B.F");
}
}
}
B , A, A
, .
.
(3.8.1) S.N, S ,
N.
public internal,
internal. ,
, , -
:
, ,
(public, protected internal, protected, internal private) , ,
private;
, ,
(public, internal private) , ,
private.
304
2
public class List
{
//
private class Node
{
public object Data;
public Node Next;
public Node(object data, Node next) {
this.Data = data;
this.Next = next;
}
}
private Node first = null;
private Node last = null;
//
public void AddToFront(object o) {...}
public void AddToBack(object o) {...}
public object RemoveFromFront() {...}
public object RemoveFromBack() {...}
}
Node.
(3.7.1) .
new, .
using System;
class Base
{
public static void M() {
Console.WriteLine("Base.M");
}
}
class Derived: Base
{
new public class M
{
public static void F() {
Console.WriteLine("Derived.M.F");
}
}
}
class Test
{
static void Main() {
Derived.M.F();
}
}
M, M, Base.
(Microsoft Corp.), 1999-2007. .
305
this
- _this (7.5.7). ,
this -.
-,
this -
.
using System;
class C
{
int i = 123;
public void F() {
Nested n = new Nested(this);
n.G();
}
public class Nested
{
C this_c;
public Nested(C c) {
this_c = c;
}
public void G() {
Console.WriteLine(this_c.i);
}
}
class Test
{
static void Main() {
C c = new C();
c.F();
}
}
. C Nested this
Nested, C.
-
, -,
- private protected.
using System;
class C
{
private static void F() {
Console.WriteLine("C.F");
}
306
class Test
{
static void Main() {
C.Nested.G();
}
}
C, Nested. Nested G
F, C, F .
,
-.
using System;
class Base
{
protected void F() {
Console.WriteLine("Base.F");
}
}
class Derived: Base
{
public class Nested
{
public void G() {
Derived d = new Derived();
d.F();
//
}
}
}
class Test
{
static void Main() {
Derived.Nested n = new Derived.Nested();
n.G();
}
}
Derived.Nested F, Base,
Derived, Derived.
.
.
, .
, ,
. , ,
-, .
(Microsoft Corp.), 1999-2007. .
307
;
.
, Inner;
:
class Outer<T>
{
class Inner<U>
{
public static void F(T t, U u) {...}
}
static void F(T t) {
Outer<T>.Inner<string>.F(t, "abc");
Inner<string>.F(t, "abc");
//
//
Outer<int>.Inner<string>.F(3, "abc"); //
Outer.Inner<string>.F(t, "abc");
// , Outer
}
}
,
, :
class Outer<T>
{
class Inner<T>
{
public T t;
}
}
// , T Outer
// T Inner
10.3.9
C#
, , ,
, .
, ,
,
.
, .
, ,
(10.3.3) new (10.3.4).
:
C#;
,
C#;
, , ,
,
C#.
(10.13) (10.3.9.1).
308
,
P (10.7) T :
T get_P();
void set_P(T value);
, .
using System;
class A
{
public int P {
get { return 123; }
}
}
class B: A
{
new public int get_P() {
return 456;
}
class Test
{
static void Main() {
B b = new B();
A a = b;
Console.WriteLine(a.P);
Console.WriteLine(b.P);
Console.WriteLine(b.get_P());
}
}
A P , get_P
set_P. B A .
:
123
123
456
,
E (10.8) T :
void add_E(T handler);
void remove_E(T handler);
,
(10.9) T L :
T get_Item(L);
void set_Item(L, T value);
, .
(Microsoft Corp.), 1999-2007. .
309
,
, (10.13), :
void Finalize();
10.4
, : ,
. _
.
_:
_ const _ ;
_:
_
_ _
_:
public
protected
internal
private
_:
_
_ , _
_:
= _
_ (17), new (10.3.4)
(10.3.5).
, _.
, _ static.
.
_ , .
_, . _
, , =,
_ (7.18), .
, , sbyte, byte, short, ushort, int, uint, long,
ulong, char, float, double, decimal, bool, string, _ _.
_ ,
(6.1).
, (3.5.4).
_ (7.5.2) __
(7.5.4).
_. ,
, _.
case, goto case, enum,
.
7.18, _ ,
. null
_, string, new, new
310
, ,
string, null.
,
,
_, readonly (10.5.2).
, ,
, .
class A
{
public const double X = 1.0, Y = 2.0, Z = 3.0;
}
class A
{
public const double X = 1.0;
public const double Y = 2.0;
public const double Z = 3.0;
}
,
.
.
class A
{
public const int X = B.Z + 1;
public const int Y = 10;
}
class B
{
public const int Z = A.Y + 1;
}
10.5
, , . _
.
_:
_ _ ;
_:
_
_ _
311
_:
new
public
protected
internal
private
static
readonly
volatile
_:
_
_ , _
_:
= _
_:
_
_ (17), new (10.3.4),
(10.3.5) static (10.5.1). ,
_ readonly (10.5.2) volatile (10.5.3), .
, _.
.
_ , .
_, . _
, , =
_ (10.5.5), .
, (3.5.4).
_ (7.5.2) __
(7.5.4). (7.16).
,
(7.5.9)
(7.6.5).
, ,
, .
class A
{
public static int X = 1, Y, Z = 100;
}
class A
{
public static int X = 1;
public static int Y;
public static int Z = 100;
}
312
10.5.1
static, , ,
. static,
. (5),
C#, ,
.
,
(4.4.2). , ,
.
.
class C<V>
{
static int count = 0;
public C() {
count++;
}
public static int Count {
get { return count; }
}
}
class Application
{
static void Main() {
C<int> x1 = new C<int>();
Console.WriteLine(C<int>.Count);
// 1
// 1
// 2
. ,
.
__ (7.5.4) E.M, M ,
E , M, M , E
, M.
10.3.7.
10.5.2
_ readonly, , ,
.
, ,
. ( .)
, readonly :
_, (
_);
313
, ;
, .
, readonly
out ref.
Color
Color
Color
Color
Color
Program1 Program2 , .
Program1.Utils.X ,
Console.WriteLine , .
, X Program1 , Console.WriteLine
, Program2 . X , X
Program2 Program1
Program2.
10.5.3 volatile
_ volatile, , ,
c volatile.
, , -volatile
,
, _ (8.12).
, . volatile
:
, volatile,
, , .
volatile,
. volatile :
_;
byte, sbyte, short, ushort, int, uint, char, float, bool, System.IntPtr System.UintPtr;
using System;
using System.Threading;
class Test
{
public static int result;
public static volatile bool finished;
static void Thread2() {
result = 143;
finished = true;
}
static void Main() {
finished = false;
// Thread2()
new Thread(new ThreadStart(Thread2)).Start();
315
// , Thread2 result
// finished true.
for (;;) {
if (finished) {
Console.WriteLine("result = {0}", result);
return;
}
}
:
result = 143
Main , Thread2.
-volatile result, true volatile finished.
, finished true, result.
finished volatile, 143 result.
finished volatile, result
finished, 0 result.
finished volatile .
10.5.4
, , ,
(5.2) . ,
.
using System;
class Test
{
static bool b;
int i;
b = False, i = 0
b, i .
10.5.5
_.
,
.
, .
using System;
316
2
class Test
{
static double x = Math.Sqrt(2.0);
int i = 100;
string s = "Hello";
static void Main() {
Test a = new Test();
Console.WriteLine("x = {0}, i = {1}, s = {2}", x, a.i, a.s);
}
}
x = 1.4142135623731, i = 100, s = Hello
x ,
i s .
, 10.5.4, , ,
. , ,
,
. , ,
,
.
. .
using System;
class Test
{
static int a = b + 1;
static int b = a + 1;
. a b, .
a = 1, b = 2
a b 0 ( int)
. a, b ,
a 1. b, a 1,
b 2.
, , .
(10.12),
.
.
using System;
317
class Test
{
static void Main() {
Console.WriteLine("{0} {1}", B.Y, A.X);
}
class A
{
public static int X = Test.F("Init A");
}
class B
{
public static int Y = Test.F("Init B");
}
:
Init A
Init B
11
:
Init B
Init A
11
X Y ;
. :
using System;
class Test
{
static void Main() {
Console.WriteLine("{0} {1}", B.Y, A.X);
}
class B
{
static B() {}
public static int Y = Test.F("Init B");
}
318
:
Init B
Init A
11
( 10.12) ,
B ( B)
A.
,
(10.11.1) . ,
.
10.11.
.
, this ,
_, .
class A
{
int x = 1;
int y = x + 1;
}
// , this
y ,
.
10.6
, ,
. _:
_:
_ _
_:
_ partial __
_ __
( __ )
___
_:
_
_ _
319
_:
new
public
protected
internal
private
static
virtual
sealed
override
abstract
extern
__:
void
_:
_ .
_:
_ (17)
(10.3.5), new (10.3.4), static (10.6.2), virtual (10.6.3),
override (10.6.4), sealed (10.6.5), abstract (10.6.6) extern (10.6.7).
, :
(10.3.5);
: new override;
abstract,
: static, virtual, sealed extern;
private,
: virtual, override abstract;
sealed,
override;
partial,
: new, public, protected, internal, private, virtual, sealed, override, abstract
extern.
__ ,
. , __
void. partial, void.
_ .
(13.4.1), _ . _
_, . .
320
__ (10.1.3).
, .
extern, __ .
__ (10.6.1).
___
(10.1.5)
__, override.
__ __
, (3.5.4).
abstract extern _ . partial
_ , .
_ , .
, (3.6)
. , , ,
. ,
, ,
.
, .
-, .
, ,
, , , ,
ref out.
_ _
__, _
___, .
.
10.6.1
, , __ .
__:
_
_ , _
_
_:
_
_ , _
_:
_
_:
ref
out
this
_:
params _
, ,
_.
(Microsoft Corp.), 1999-2007. .
321
_ (17),
ref, out this, . _
. this
.
10.6.9.
_ (17), params,
_ .
. _
(12.1).
, .
10.6.1.4.
,
.
, .
.
.
(7.5.5.1)
,
.
_ (7.5.2).
:
, ;
, ref;
, out;
, params.
, . ,
.
.
, (10.14), .
using System;
class Test
{
static void Swap(ref int x, ref int y) {
int temp = x;
x = y;
y = temp;
}
i = 2, j = 1
Swap Main x i, y j. ,
i j.
, ,
.
class A
{
string s;
void F(ref string a, ref string b) {
s = "One";
a = "Two";
b = "Three";
}
void G() {
F(ref s, ref s);
}
F G s a, b. , s, a b
, s.
, out, .
, .
, ,
.
,
out, __
(5.3.3) , .
(Microsoft Corp.), 1999-2007. .
323
, , ,
,
.
, , ,
, .
.
, (10.2.7) (10.14),
.
, .
.
using System;
class Test
{
static void SplitPath(string path, out string dir, out string name) {
int i = path.Length;
while (i > 0) {
char ch = path[i 1];
if (ch == '\\' || ch == '/' || ch == ':') break;
i--;
}
dir = path.Substring(0, i);
name = path.Substring(i);
}
static void Main() {
string dir, name;
SplitPath("c:\\Windows\\System\\hello.txt", out dir, out name);
Console.WriteLine(dir);
Console.WriteLine(name);
}
}
:
c:\Windows\System\
hello.txt
, dir name
SplitPath, .
, params, .
,
. , string[] string[][]
, string[,] . params
ref out.
:
, , ,
(6.1) .
, ;
,
, (6.1)
324
. ,
,
.
,
(10.6.1.1) .
using System;
class Test
{
static void F(params int[] args) {
Console.Write(" {0} :", args.Length);
foreach (int i in args)
Console.Write(" {0}", i);
Console.WriteLine();
}
3 : 1 2 3
4 : 10 20 30 40
0 :
F a . F
int[]
. , F
int[] .
:
F(new int[] {10, 20, 30, 40});
F(new int[] {});
, (7.4.3.1).
, ,
.
using System;
class Test
{
static void F(params object[] a) {
Console.WriteLine("F(object[])");
}
325
F();
F(object[]);
F(object,object);
F(object[]);
F(object[]);
.
, .
,
.
, .
object[],
object.
, object[] object.
.
using System;
class Test
{
static void F(params object[] args) {
foreach (object o in args) {
Console.Write(o.GetType().FullName);
Console.Write(" ");
}
Console.WriteLine();
}
static void Main() {
object[] a = {1, "", 123.456};
object o = a;
F(a);
F((object)a);
F(o);
F((object[])o);
}
}
326
2
System.Int32 System.String System.Double
System.Object[]
System.Object[]
System.Int32 System.String System.Double
F F,
( object[]). ,
F,
. F ,
( object
object[]).
F, .
object[], ,
( object[]).
10.6.2
static. static ,
.
.
this .
,
this (7.5.7).
__ (7.5.4) E.M M
E , M, M ,
M.
10.3.7.
10.6.3
virtual,
. virtual , .
,
, , .
, .
(10.6.4).
,
, .
. ,
N A , C
R ( R C C ),
.
C, N A M
, C. 7.5.5.1.
, M , M.
M ,
M R.
327
, ,
.
M R .
R virtual M,
M.
, R override M,
M.
M R
M R.
:
using System;
class A
{
public void F() { Console.WriteLine("A.F"); }
public virtual void G() { Console.WriteLine("A.G"); }
}
class B: A
{
new public void F() { Console.WriteLine("B.F"); }
}
class Test
{
static void Main() {
B b = new B();
A a = b;
a.F();
b.F();
a.G();
b.G();
}
}
A F G. B
F, F,
G. :
A.F
B.F
B.G
B.G
328
2
class A
{
public virtual void F() { Console.WriteLine("A.F"); }
}
class B: A
{
public override void F() { Console.WriteLine("B.F"); }
}
class C: B
{
new public virtual void F() { Console.WriteLine("C.F"); }
}
class D: C
{
public override void F() { Console.WriteLine("D.F"); }
}
class Test
{
static void Main() {
D d = new D();
A a = d;
B b = d;
C c = d;
a.F();
b.F();
c.F();
d.F();
}
}
C D ,
A, C. , C, ,
A. , D ,
C. D ,
A. :
B.F
B.F
D.F
D.F
,
D , .
10.6.4
override,
.
. .
,
.
, override,
. M, C,
C .
C
. , ,
(Microsoft Corp.), 1999-2007. .
329
M, .
, public, protected,
protected internal internal , C.
,
.
(. ).
.
, ,
.
, .
,
.
. ,
. protected internal
, , ,
protected.
, .
.
:
abstract class C<T>
{
public virtual T F() {...}
public virtual C<T> G() {...}
}
class D: C<string>
{
public override string F() {...}
//
//
// .
//
//
// . C<U>
_ (7.5.8).
330
2
class A
{
int x;
class B: A
{
int y;
base.PrintFields() B PrintFields, A.
_ ,
. B ((A)this).PrintFields(),
PrintFields, B, A.
, PrintFields , ((A)this)
B.
override.
, ,
.
class A
{
public virtual void F() {}
}
class B: A
{
public virtual void F() {}
F()
}
// .
F B override , , F
A. F B A, ,
new.
class A
{
public virtual void F() {}
}
class B: A
{
new private void F() {}
}
class C: B
{
public override void F() {}
}
// A.F B
// . A.F
331
F B F, A.
F B private,
B C. , F C
, F A.
10.6.5
sealed,
. sealed,
override. sealed
.
using System;
class A
{
public virtual void F() {
Console.WriteLine("A.F");
}
public virtual void G() {
Console.WriteLine("A.G");
}
}
class B: A
{
sealed override public void F() {
Console.WriteLine("B.F");
}
class C: B
{
override public void G() {
Console.WriteLine("C.G");
}
}
B : F sealed G
. sealed B
F C.
10.6.6
abstract,
. ,
virtual.
,
. ,
.
, __
.
332
(10.1.1.1).
public abstract class Shape
{
public abstract void Paint(Graphics g, Rectangle r);
}
public class Ellipse: Shape
{
public override void Paint(Graphics g, Rectangle r) {
g.DrawEllipse(r);
}
}
public class Box: Shape
{
public override void Paint(Graphics g, Rectangle r) {
g.DrawRect(r);
}
}
Shape
. Paint ,
. Ellipse Box Shape.
, Paint
.
_ (7.5.8)
.
abstract class A
{
public abstract void F();
}
class B: A
{
public override void F() {
base.F();
}
}
// . base.F
base.F() ,
.
.
.
.
using System;
class A
{
public virtual void F() {
Console.WriteLine("A.F");
}
}
333
abstract class B: A
{
public abstract override void F();
}
class C: B
{
public override void F() {
Console.WriteLine("C.F");
}
}
A , B .
C .
10.6.7
extern, .
, C#.
, __
. .
extern DllImport (17.5),
(DLL).
.
DllImport,
static. extern
DllImport:
using System.Text;
using System.Security.Permissions;
using System.Runtime.InteropServices;
class Path
{
[DllImport("kernel32", SetLastError=true)]
static extern bool CreateDirectory(string name, SecurityAttribute sa);
[DllImport("kernel32", SetLastError=true)]
static extern bool RemoveDirectory(string name);
[DllImport("kernel32", SetLastError=true)]
static extern int GetCurrentDirectory(int bufSize, StringBuilder buf);
[DllImport("kernel32", SetLastError=true)]
static extern bool SetCurrentDirectory(string name);
10.6.8
partial, .
(10.2).
.
. 10.2.7.
10.6.9
this,
. ,
.
this .
334
, :
public static class Extensions
{
public static int ToInt32(this string s) {
return Int32.Parse(s);
}
public static T[] Slice<T>(this T[] source, int index, int count) {
if (index < 0 || count < 0 || source.Length index < count)
throw new ArgumentException();
T[] result = new T[count];
Array.Copy(source, index, result, 0, count);
return result;
}
}
. ,
(7.5.5.2). .
:
static class Program
{
static void Main() {
string[] strings = { "1", "22", "333", "4444" };
foreach (string s in strings.Slice(1, 2)) {
Console.WriteLine(s.ToInt32());
}
}
}
10.6.10
_ .
,
. (8.2),
, .
, void,
return (8.9.4) . , void,
( ),
.
335
void, return
, .
, , . ,
, , .
class A
{
public int F() {}
// .
F, , ,
. G H ,
return,
.
10.6.11
7.4.2.
10.7
, .
: , , . .
. , ,
. ,
. , ,
. , ,
. ,
.
_:
_:
_ _
{ __ }
_:
_
_ _
336
_:
new
public
protected
internal
private
static
virtual
sealed
override
abstract
extern
_:
_ .
_ (17),
(10.3.5), new (10.3.4), static (10.6.2), virtual (10.6.3),
override (10.6.4), sealed (10.6.5), abstract (10.6.6) extern (10.6.7).
, (10.6).
. _ .
, _
. (13.4.1) _
_, . ( ).
, (3.5.4).
__ { }
(10.7.2) . ,
.
,
. ,
ref out.
extern, .
,
__ .
10.7.1
static, .
static , .
.
this .
,
this (7.5.7) .
__ (7.5.4) E.M
M E , M, M
, M.
10.3.7.
337
10.7.2
__ ,
.
__:
___get ___set
___set ___get
___get:
__ get __
___set:
__ set __
__:
protected
internal
private
protected internal
internal protected
__:
___get,
___set .
get set, __
__.
__ .
__
.
, override,
__ ,
(get set), .
, override,
__
.
__ ,
. :
o
public,
__.
protected internal,
__ internal, protected private.
internal protected,
__ private.
private,
__ .
abstract extern __
. , ,
. (get set),
338
(10.7.3). __ ,
, , ,
.
get ,
. ,
, get
(7.1.1). get
, 10.6.10. , return
get ,
. , get .
set void
, . set
value. (7.16)
++ -- (7.5.9, 7.6.5), set (
+
+ --), (7.16.1). set
void , 10.6.10. ,
return set . set
value,
.
get set
.
, (get set), .
, get, .
.
, set, .
.
.
public class Button: Control
{
private string caption;
public string Caption {
get {
return caption;
}
set {
if (caption != value) {
caption = value;
Repaint();
}
}
}
339
set ,
get .
get set
.
.
class A
{
private string name;
// .
// .
.
, ,
. , , ,
.
,
.
class A
{
public int P {
set {...}
}
}
class B: A
{
new public int P {
get {...}
}
}
P B P A .
,
B b = new B();
b.P = 1;
// . B.P
((A)b).P = 1; // . A.P
340
b.P ,
P B P A. ,
P .
. :
class Label
{
private int x, y;
private string caption;
public Label(int x, int y, string caption) {
this.x = x;
this.y = y;
this.caption = caption;
}
public int X {
get { return x; }
}
public int Y {
get { return y; }
}
public Point Location {
get { return new Point(x, y); }
}
Label int (x y)
. X Y,
Location Point. Label
Point, ,
:
class Label
{
private Point location;
private string caption;
public Label(int x, int y, string caption) {
this.location = new Point(x, y);
this.caption = caption;
}
public int X {
get { return location.x; }
}
public int Y {
get { return location.y; }
}
341
x y public , Label
.
,
. ,
,
.
,
.
get ,
, get
.
class Counter
{
private int next;
public int Next {
get { return next++; }
}
}
Next . ,
,
.
get ,
get , .
get
.
get ,
.
,
. :
using System.IO;
public class Console
{
private static TextReader reader;
private static TextWriter writer;
private static TextWriter error;
public static TextReader In {
get {
if (reader == null) {
reader = new StreamReader(Console.OpenStandardInput());
}
return reader;
}
}
342
2
public static TextWriter Out {
get {
if (writer == null) {
writer = new StreamWriter(Console.OpenStandardOutput());
}
return writer;
}
}
TextWriter .
In Error, .
10.7.3
, ,
, .
:
public class Point {
public int X { get; set; } //
public int Y { get; set; } //
}
:
public class Point {
private int x;
private int y;
public int X { get { return x; } set { x = value; } }
public int Y { get { return y; } set { y = value; } }
}
,
. ,
.
. ,
:
343
,
,
. ,
.
10.7.4
__, (3.5.2)
__.
__,
.
__ (7.3)
(7.4.3).
.
.
(7.1.1)
get.
(7.16.1)
set.
(7.16.2)
++ -- (7.5.9, 7.6.5)
(get set).
A.Text B.Text ,
set. , B.Count M,
A.Count.
class A
{
public string Text {
get { return "!"; }
set { }
}
class B: A
{
private string text = "!";
private int count = 0;
new public string Text {
get { return text; }
protected set { text = value; }
}
new protected int Count {
get { return count; }
set { count = value; }
}
}
344
2
class M
{
static void Main() {
B b = new B();
b.Count = 12;
// set A.Count
int i = b.Count;
// get A.Count
b.Text = " ?";
// . set B.Text
string s = b.Text;
// get B.Text
}
}
, ,
__.
, __:
public interface I
{
string Prop { get; }
}
public class C: I
{
public Prop {
get { return ""; }
//
internal set {...}
// , I.Prop
set
}
}
10.7.5 , ,
virtual , .
virtual .
.
abstract , ,
.
,
.
, __ .
, abstract override, ,
.
.
(10.1.1.1).
,
, override.
. .
.
,
, .
( ),
.
( ),
.
345
sealed.
.
.
, , ,
,
, . , ,
10.6.3, 10.6.4, 10.6.5 10.6.6,
.
get ,
, .
set void,
, , , .
abstract class A
{
int y;
public virtual int X {
get { return 0; }
}
public virtual int Y {
get { return y; }
set { y = value; }
}
}
X , Y , Z
. Z ,
A .
, A:
class B: A
{
int z;
public override int X {
get { return base.X + 1; }
}
public override int Y {
set { base.Y = value < 0? 0: value; }
}
X, Y Z
. , ,
. get X set
Y base .
Z . , B
346
, B
.
override,
. ,
. :
public class B
{
public virtual int P {
protected set {...}
get {...}
}
}
public class D: B
{
public override int P {
protected set {...}
get {...}
}
}
// protected
//
10.8
, .
.
_:
_:
_ event
;
_ event _
{ ___ }
_:
_
_ _
_:
new
public
protected
internal
private
static
virtual
sealed
override
abstract
extern
__ :
___add ___remove
___remove ___add
___add:
add
(Microsoft Corp.), 1999-2007. .
347
___remove:
remove
_ (17),
(10.3.5), new (10.3.4), static (10.6.2), virtual (10.6.3),
override (10.6.4), sealed (10.6.5), abstract (10.6.6) extern (10.6.7).
, (10.6).
_ (4.2) ,
(3.5.4).
___.
, , ,
(10.8.1).
.
, ___,
_.
, _.
_ abstract
___, .
extern, .
,
extern ___ .
+= -= (7.16.3).
. ,
.
+= -= ,
.
, .
x += y x -= y ( x ,
x) void ( x x
).
.
Button:
public delegate void EventHandler(object sender, EventArgs e);
public class Button: Control
{
public event EventHandler Click;
}
public class LoginDialog: Form
{
Button OkButton;
Button CancelButton;
348
2
public LoginDialog() {
OkButton = new Button(...);
OkButton.Click += new EventHandler(OkButtonClick);
CancelButton = new Button(...);
CancelButton.Click += new EventHandler(CancelButtonClick);
}
void OkButtonClick(object sender, EventArgs e) {
// OkButton.Click
}
LoginDialog
Button Click.
10.8.1 ,
, ,
. abstract extern
____.
, . (15),
, .
, null.
public delegate void EventHandler(object sender, EventArgs e);
public class Button: Control
{
public event EventHandler Click;
protected void OnClick(EventArgs e) {
if (Click != null) Click(this, e);
}
public void Reset() {
Click = null;
}
}
Click Button. ,
, . OnClick Button
"" Click. ,
,
. , , null.
Button Click +=
=,
b.Click += new EventHandler();
Click,
b.Click = new EventHandler();
Click.
, ,
,
(Microsoft Corp.), 1999-2007. .
349
. ,
(8.12) (7.5.10.6)
.
, :
class X
{
public event D Ev;
}
, :
class X
{
private D __Ev; //
public event D Ev {
add {
lock(this) { __Ev = __Ev + value; }
}
remove {
lock(this) { __Ev = __Ev - value; }
}
}
X Ev __Ev. "__Ev"
; .
:
class X
{
public static event D Ev;
}
, :
class X
{
private static D __Ev; //
public static event D Ev {
add {
lock(typeof(X)) { __Ev = __Ev + value; }
}
remove {
lock(typeof(X)) { __Ev = __Ev - value; }
}
}
10.8.2
___,
Button. ,
.
____
.
350
____ ,
.
___add
___remove. add
remove, . , ___add,
, , ,
___remove, ,
.
___add ___remove
void.
value. ,
. ,
+=, add, -=,
remove.
. ___add
___remove void,
10.6.10. , return .
value,
.
class Control: Component
{
//
static readonly object mouseDownEventKey = new object();
static readonly object mouseUpEventKey = new object();
// ,
protected Delegate GetEventHandler(object key) {...}
// ,
protected void AddEventHandler(object key, Delegate handler) {...}
// ,
protected void RemoveEventHandler(object key, Delegate handler) {...}
// MouseDown
public event MouseEventHandler MouseDown {
add { AddEventHandler(mouseDownEventKey, value); }
remove { RemoveEventHandler(mouseDownEventKey, value); }
}
// MouseUp
public event MouseEventHandler MouseUp {
add { AddEventHandler(mouseUpEventKey, value); }
remove { RemoveEventHandler(mouseUpEventKey, value); }
}
// MouseUp
protected void OnMouseUp(MouseEventArgs args) {
MouseEventHandler handler;
handler = (MouseEventHandler)GetEventHandler(mouseUpEventKey);
if (handler != null)
handler(this, args);
}
}
(Microsoft Corp.), 1999-2007. .
351
Control . AddEventHandler
, GetEventHandler ,
, RemoveEventHandler
. , ,
null ,
.
10.8.3
static, .
static, .
, this
.
,
this (7.5.7) .
__ (7.5.4) E.M, M
, E , M, M , E
, M.
10.3.7.
10.8.4 , ,
virtual , .
virtual .
abstract , ,
.
.
,
____.
, abstract override, ,
.
.
(10.1.1.1).
, override.
.
.
.
,
, .
sealed.
.
.
new
.
, , ,
, ,
, . , , 10.6.3,
352
10.9
, , .
_:
_:
_ _
{ __ }
_:
_
_ _
_:
new
public
protected
internal
private
virtual
sealed
override
abstract
extern
_:
this [ __ ]
_ . this [ -- ]
_ (17)
(10.3.5) new (10.3.4), virtual (10.6.3), override (10.6.4),
sealed (10.6.5), abstract (10.6.6) extern (10.6.7).
, (10.6),
, static
.
virtual, override abstract , .
abstract override ,
.
, .
,
this.
_, . this. ,
, .
__ .
(10.6.1), ,
, ref out .
, __
, (3.5.4).
353
, ;
_ (7.5.2) __ (7.5.4),
__ (7.5.6.2);
static , ;
get , get
, ;
set value,
set ,
, value;
,
, ;
base.P, P .
base[E], E
, .
, , 10.7.2 10.7.3,
, .
extern,
. ,
__ .
BitArray,
.
using System;
class BitArray
{
int[] bits;
int length;
public BitArray(int length) {
if (length < 0) throw new ArgumentException();
bits = new int[((length - 1) >> 5) + 1];
this.length = length;
}
354
2
public int Length {
get { return length; }
}
BitArray , bool[] (
, ),
, bool[].
CountPrimes BitArray
1 :
class CountPrimes
{
static int Count(int max) {
BitArray flags = new BitArray(max + 1);
int count = 1;
for (int i = 2; i <= max; i++) {
if (!flags[i]) {
for (int j = i * 2; j <= max; j += i) flags[j] = true;
count++;
}
}
return count;
}
, BitArray , bool[].
26 10 .
AZ,
09.
using System;
355
class Grid
{
const int NumRows = 26;
const int NumCols = 10;
int[,] cells = new int[NumRows, NumCols];
public int this[char c, int col] {
get {
c = Char.ToUpper(c);
if (c < 'A' || c > 'Z') {
throw new ArgumentException();
}
if (col < 0 || col >= NumCols) {
throw new IndexOutOfRangeException();
}
return cells[c - 'A', col];
}
set {
c = Char.ToUpper(c);
if (c < 'A' || c > 'Z') {
throw new ArgumentException();
}
if (col < 0 || col >= NumCols) {
throw new IndexOutOfRangeException();
}
cells[c - 'A', col] = value;
}
10.9.1
7.4.2.
10.10
, ,
. _:
_:
_ _ _
_:
_
_ _
_:
public
static
extern
_:
__
__
__
__:
operator __ ( )
356
__:
+ - ! ~ ++ -- true false
__:
operator __ ( ,
)
__:
+
*
/
%
&
|
^
<<
right_shift
==
!=
>
<
>=
<=
__:
implicit operator ( )
explicit operator ( )
_:
: (10.10.1),
(10.10.2) (10.10.3).
extern, .
, _
. _ ,
, .
, , 10.6.10.
:
: public static;
-.
ref out ;
, ,
, (3.5.4);
,
.
(Microsoft Corp.), 1999-2007. .
357
, , ,
. ,
, , , ,
, . , new
.
7.2.
6.4.
10.10.1
, T
, :
+, -, ! ~ T T?
;
++ T T?
, ;
true false T T?
bool.
//
// -
// 4 x 0
358
, , 1 ,
, (7.6.5)
(7.5.9). C++,
.
.
10.10.2
, T
, :
- ,
T T?, ;
<< >> ,
T T?, int int?, .
operator == operator !=
10.10.3
, (6.4),
.
, implicit,
, .
, , .
6.1.
, explicit,
, .
, 6.2.
,
, , .
S T, S T ,
, S0 T0 , S0 T0 S
T . S
T, :
S0 T0 ;
S0, T0 ,
;
S0, T0 _;
359
, , S T T
S.
, S T,
, ,
.
class C<T> {...}
class D<T>: C<T>
{
public static implicit operator C<int>(D<T> value) {...}
//
//
object T, ,
( , ,
object).
, ,
, , . :
(6.1) S T,
( ) S T ;
(6.2) S T,
S T .
S T .
, object, , Convertible<T> ,
. .
360
2
void F(int i, Convertible<int> n) {
i = n;
//
i = (int)n;
//
n = i;
//
n = (Convertible<int>)i;
//
object
, :
void F(object o, Convertible<object> n) {
o = n;
//
o = (object)n;
//
n = o;
//
n = (Convertible<object>)o;//
}
_.
, -
_, _
, _.
. ( ,
, .)
implicit explicit .
, implicit explicit
.
, ,
.
(,
) (, ),
.
using System;
public struct Digit
{
byte value;
public Digit(byte value) {
if (value < 0 || value > 9) throw new ArgumentException();
this.value = value;
}
public static implicit operator byte(Digit d) {
return d.value;
}
public static explicit operator Digit(byte b) {
return new Digit(b);
}
361
Digit byte ,
, byte Digit , Digit
byte.
10.11
, ,
. _:
_:
_ _
_
_:
_
_ _
_:
public
protected
internal
private
extern
_:
( __ )
_
_:
: base ( _ )
: this ( _ )
_:
_ (17),
(10.3.5) extern (10.6.7).
.
_ ,
. , .
__
, __ (10.6).
(3.6) ,
(7.4.2) .
, __ ,
, (3.5.4).
_
, _ .
10.11.1.
extern,
.
, _ .
_ ,
.
void (10.6.10).
362
. , ,
, .
,
(10.11.4).
__ (7.5.10.1)
_.
10.11.1
( object)
_. ,
, _:
base(_)
.
_ 7.4.3.
,
, (10.11.4),
.
, ;
this(_)
.
_ 7.4.3.
, .
, .
, ,
.
,
base(). ,
C(...) {...}
C(...): base() {...}
, _
, . ,
. .
class A
{
public A(int x, int y) {}
}
class B: A
{
public B(int x, int y): base(x + y, x - y) {}
}
.
this ,
_,
.
363
10.11.2
base(...), ,
_ , .
,
.
.
10.11.3
,
.
-
, .
using System;
class A
{
public A() {
PrintFields();
}
public virtual void PrintFields() {}
}
class B: A
{
int x = 1;
int y;
public B() {
y = -1;
}
public override void PrintFields() {
Console.WriteLine("x = {0}, y = {1}", x, y);
}
}
new B() B, :
x = 1, y = 0
x 1,
. y 0 ( int),
y .
,
_.
using System;
using System.Collections;
class A
{
int x = 1, y = -1, count;
public A() {
count = 0;
}
364
2
public A(int n) {
count = n;
}
}
class B: A
{
double sqrt2 = Math.Sqrt(2.0);
ArrayList items = new ArrayList(100);
int max;
public B(): this(100) {
items.Add("default");
}
public B(int n): base(n 1) {
max = n;
}
}
,
(base this). , ,
(,
, ).
using System.Collections;
class A
{
int x, y, count;
public A() {
x = 1;
y = -1;
object();
count = 0;
}
public A(int n) {
x = 1;
y = -1;
object();
count = n;
}
//
//
// object()
//
//
// object()
}
class B: A
{
double sqrt2;
ArrayList items;
int max;
// B(int)
365
10.11.4
,
.
.
, , .
, .
. ,
protected C(): base() {}
C .
class Message
{
object sender;
string text;
}
,
. ,
class Message
{
object sender;
string text;
}
10.11.5
T ,
T T T. ,
,
. :
public class Trig
{
private Trig() {}
//
366
Trig , .
.
,
.
10.11.6
this(...)
.
class Text
{
public Text(): this(0, 0, null) {}
public Text(int x, int y): this(x, y, null) {}
. this(...)
, .
:
Text t1 = new Text();
Text t2 = new Text(5, 10);
Text t3 = new Text(5, 20, "Hello");
// , Text(0, 0, null)
// , Text(5, 10, null)
10.12
, ,
.
__:
__:
__ ( )
__
__:
extern static
static extern
__:
__ (17)
extern (10.6.7).
__ ,
. ,
.
extern,
.
, __
.
__ , ,
367
, . _
void (10.6.10).
.
.
, :
Main (3.1), ,
Main.
(10.5.1) .
(5.2). (10.4.5.1)
. .
using System;
class Test
{
static void Main() {
A.F();
B.F();
}
}
class A
{
static A() {
Console.WriteLine("Init A");
}
public static void F() {
Console.WriteLine("A.F");
}
}
class B
{
static B() {
Console.WriteLine("Init B");
}
public static void F() {
Console.WriteLine("B.F");
}
}
:
Init A
A.F
Init B
B.F
368
A A.F,
B B.F.
,
.
using System;
class A
{
public static int X;
static A() {
X = B.Y + 1;
}
class B
{
public static int Y = A.X + 1;
static B() {}
X = 1, Y = 2
Main B.Y,
B. Y A,
A.X. A
X Y, . A.X
1. A
,
Y, 2.
,
-,
(10.1.5). , ,
:
class Gen<T> where T: struct
{
static Gen() {
if (!typeof(T).IsEnum) {
throw new ArgumentException("T enum");
}
}
}
10.13
, ,
. _:
369
_:
extern ~ ( ) _
_:
_ (17).
_ ,
. , .
extern,
.
, _ .
_ , .
_ _
void (10.6.10).
. , , ,
.
, ,
.
, .
, .
,
. ,
, .
. , ,
, . 3.9.
using System;
class A
{
~A() {
Console.WriteLine(" A");
}
}
class B: A
{
~B() {
Console.WriteLine(" B");
}
}
class Test
{
static void Main() {
B b = new B();
b = null;
GC.Collect();
GC.WaitForPendingFinalizers();
}
}
370
2
B
A
,
.
Finalize System.Object.
C# ( )
. ,
class A
{
override protected void Finalize() {}
public void F() {
this.Finalize();
}
//
//
.
, .
, :
class A
{
void Finalize() {}
}
//
, Finalize System.Object.
, , . 16.3.
10.14
(7.4), (8.2), .
,
(10.14.1)
(10.14.2). _, _
__, , ,
.
, ref out
.
10.14.1
System.Collections.IEnumerator
System.Collections.Generic.IEnumerator<T>.
IEnumerator IEnumerator<T>
.
10.14.2
System.Collections.IEnumerable
System.Collections.Generic.IEnumerable<T>.
IEnumerable IEnumerable<T>
.
371
10.14.3 yield
. yield
.
, IEnumerator<T> IEnumerable<T>, T.
10.14.4
, ,
, .
. ,
, ,
MoveNext . :
System.IDisposable.
( ) ,
.
,
,
. , ,
, , , ,
(2.4.2).
, .
MoveNext, Current Dispose
IEnumerable IEnumerable<T>, .
, IEnumerator.Reset.
System.NotSupportedException.
MoveNext
MoveNext .
MoveNext Current
. , MoveNext,
MoveNext:
before, MoveNext:
o
running;
( this)
, ;
, (
).
running, MoveNext .
suspended, MoveNext:
372
running;
( this)
, .
, , ,
MoveNext;
,
yield return, , ,
( ).
MoveNext , :
yield return, yield break,
, .
, , , yield
Current ;
.
( this) , yield return.
yield return try, finally
;
suspended;
MoveNext true , ,
.
after;
MoveNext false , , .
:
o
after;
MoveNext false , , .
:
o
finally
;
after;
MoveNext.
Current
Current yield return .
suspended, Current ,
MoveNext.
before, running after, Current .
(Microsoft Corp.), 1999-2007. .
373
, object, Current
IEnumerable Current
IEnumerator<T> object.
Dispose
Dispose
after.
running, Dispose .
suspended, Dispose:
o
running;
after.
after, Dispose .
10.14.5
, ,
, .
. GetEnumerator
, , ,
MoveNext .
:
IEnumerable IEnumerable<T>, T .
( ) ,
.
,
,
. , ,
, , , ,
(2.4.2).
, . ,
IEnumerator IEnumerator<T>,
, .
GetEnumerator .
GetEnumerator , ,
. ,
.
GetEnumerator
GetEnumerator IEnumerable
IEnumerable<T>. GetEnumerator ,
.
374
,
, , 10.14.4.
10.14.6
C#. , , ,
Microsoft C#, .
Stack<T> GetEnumerator .
.
using System;
using System.Collections;
using System.Collections.Generic;
class Stack<T>: IEnumerable<T>
{
T[] items;
int count;
public void Push(T item) {
if (items == null) {
items = new T[4];
}
else if (items.Length == count) {
T[] newItems = new T[count * 2];
Array.Copy(items, 0, newItems, 0, count);
items = newItems;
}
items[count++] = item;
}
public T Pop() {
T result = items[--count];
items[count] = default(T);
return result;
}
GetEnumerator ,
, .
class Stack<T>: IEnumerable<T>
{
...
public IEnumerator<T> GetEnumerator() {
return new __Enumerator1(this);
}
375
MoveNext . , i
, MoveNext.
1 10.
FromTo .
using System;
using System.Collections.Generic;
class Test
{
static IEnumerable<int> FromTo(int from, int to) {
while (from <= to) yield return from++;
}
376
2
static void Main() {
IEnumerable<int> e = FromTo(1, 10);
foreach (int x in e) {
foreach (int y in e) {
Console.Write("{0,3} ", x * y);
}
Console.WriteLine();
}
}
}
FromTo ,
, .
using
using
using
using
System;
System.Threading;
System.Collections;
System.Collections.Generic;
class Test
{
...
static IEnumerable<int> FromTo(int from, int to) {
return new __Enumerable1(from, to);
}
class __Enumerable1:
IEnumerable<int>, IEnumerable,
IEnumerator<int>, IEnumerator
{
int __state;
int __current;
int __from;
int from;
int to;
int i;
public __Enumerable1(int __from, int to) {
this.__from = __from;
this.to = to;
}
public IEnumerator<int> GetEnumerator() {
__Enumerable1 result = this;
if (Interlocked.CompareExchange(ref __state, 1, 0) != 0) {
result = new __Enumerable1(__from, to);
result.__state = 1;
}
result.from = result.__from;
return result;
}
IEnumerator IEnumerable.GetEnumerator() {
return (IEnumerator)GetEnumerator();
}
377
object IEnumerator.Current {
get { return __current; }
}
public bool MoveNext() {
switch (__state) {
case 1:
if (from > to) goto case 2;
__current = from++;
__state = 1;
return true;
case 2:
__state = 2;
return false;
default:
throw new InvalidOperationException();
}
}
public void Dispose() {
__state = 2;
}
void IEnumerator.Reset() {
throw new NotSupportedException();
}
}
, ,
, .
GetEnumerator . GetEnumerator
, , .
, ,
. Interlocked.CompareExchange
.
from to . from
, __from,
, from .
MoveNext InvalidOperationException,
__state 0.
GetEnumerator.
. Tree<T> GetEnumerator
. .
using System;
using System.Collections.Generic;
class Tree<T>: IEnumerable<T>
{
T value;
Tree<T> left;
Tree<T> right;
378
2
public Tree(T value, Tree<T> left, Tree<T> right) {
this.value = value;
this.left = left;
this.right = right;
}
class Program
{
static Tree<T> MakeTree<T>(T[] items, int left, int right) {
if (left > right) return null;
int i = (left + right) / 2;
return new Tree<T>(items[i],
MakeTree(items, left, i - 1),
MakeTree(items, i + 1, right));
}
static Tree<T> MakeTree<T>(params T[] items) {
return MakeTree(items, 0, items.Length - 1);
}
// :
// 1 2 3 4 5 6 7 8 9
// C
static void Main() {
Tree<int> ints = MakeTree(1, 2, 3, 4, 5, 6, 7, 8, 9);
foreach (int i in ints) Console.Write("{0} ", i);
Console.WriteLine();
GetEnumerator ,
, .
class Tree<T>: IEnumerable<T>
{
...
public IEnumerator<T> GetEnumerator() {
return new __Enumerator1(this);
}
379
380
}
}
finally {
if (__state < 0) Dispose();
}
return false;
, foreach,
__left __right . __state ,
Dispose().
, foreach .
381
11.
, , .
, . , ,
.
,
, .
, .
, .
-,
,
, .
4.1.4, C# , int, double bool,
. , , C#
.
().
11.1
_ _ (), :
_:
(/) _ (/) partial (/) struct
__ (/)
_ (/) ____
(/) _ ;(/)
_ (17),
_ (11.1.1), partial,
struct, , ,
__ (10.1.3),
_ (11.1.2) ),
____ (10.1.5), _ (11.1.4)
().
11.1.1
_ :
_:
_
_ _
_:
new
public
protected
internal
private
382
.
,
(10.1).
11.1.2 partial
partial , _ .
, 10.2.
11.1.3
_,
, .
_:
: __
13.4.
11.1.4
_ .
_:
{ __ (/) }
11.2
, __ ,
System.ValueType.
__:
__
__ __
__:
_
_
_
_
_
_
_
_
__
_
, 11.3, , 10.13
10.14, .
11.3
:
(11.3.1).
System.ValueType (11.3.2).
383
(11.3.3).
,
,
null (11.3.4).
object (11.3.5).
this (7.5.7).
(11.3.7).
(11.3.8).
(11.3.9).
11.3.1
(4.1) . ,
(4.2) .
,
, . B
A, , A B
. X Y
, X Y.
, ,
. ,
struct Node
{
int data;
Node next; // , Node
}
, Node .
.
struct A { B b; }
struct B { C c; }
struct C { A a; }
, A, B C .
,
, .
( ref out)
. ,
, null.
struct Point
{
public int x, y;
384
Point a = new Point(10, 10);
Point b = a;
a.x = 100;
System.Console.WriteLine(b.x);
10. a b
, b a.x.
Point , 100, a b
.
11.3.2
System.ValueType, ,
, object.
, .
.
abstract sealed.
,
protected protected internal.
- abstract virtual,
override ,
System.ValueType.
11.3.3
.
, , .
- .
- ref out.
,
,
. , .
7.16.1.
11.3.4
5.2,
.
null. ,
null, ,
, null.
Point
385
Point ,
x y (0) .
,
(4.1.2). , ,
.
, ,
,
null.
,
.
using System;
struct KeyValuePair
{
string key;
string value;
. , KeyValuePair , key
value null, .
11.3.5
object ,
, .
object
(, ).
, . object ,
, .
object
. ,
. , ,
, .
, System.Object
(, Equals, GetHashCode ToString),
. ,
.
:
using System;
struct Counter
{
int value;
386
class Program
{
static void Test<T>() where T: new() {
T x = new T();
Console.WriteLine(x.ToString());
Console.WriteLine(x.ToString());
Console.WriteLine(x.ToString());
}
:
1
2
3
, ToString
, , x.ToString()
.
-. , , ICounter Increment,
. ICounter ,
Increment ,
Increment, .
using System;
interface ICounter
{
void Increment();
}
struct Counter: ICounter
{
int value;
public override string ToString() {
return value.ToString();
}
void ICounter.Increment() {
value++;
}
387
class Program
{
static void Test<T>() where T: ICounter, new() {
T x = new T();
Console.WriteLine(x);
x.Increment();
// x
Console.WriteLine(x);
((ICounter)x).Increment();
// x
Console.WriteLine(x);
}
static void Main() {
Test<Counter>();
}
}
Increment x.
Increment, x. ,
:
0
1
1
. 4.3.
11.3.6 this
- this
. , this
, -, this
- .
this out ,
- this ref
. this , ,
-, this this
ref out.
11.3.7
11.3.4, ,
,
null.
.
. .
,
struct Point
{
public int x = 1; // ,
public int y = 1; // ,
}
, .
11.3.8
, .
,
388
,
, null (4.1.2).
. :
struct Point
{
int x, y;
Point p1 = new Point();
Point p2 = new Point(0, 0);
Point, x y .
base(...).
, this
out , , out, this
(5.3) .
, this ref
, ref, this
. :
struct Point
{
int x, y;
public int X {
set { x = value; }
}
public int Y {
set { y = value; }
}
public Point(int x, int y) {
X = x;
// , this
Y = y;
// , this
}
}
- ( set X Y)
, . ,
Point , , .
11.3.9
389
11.3.10
,
.
:
- .
(11.3.4)
. .
11.4
struct ,
, .
11.4.1
DBInt integer,
int, , .
.
using System;
public struct DBInt
{
// Null DBInt.
public static readonly DBInt Null = new DBInt();
// true, DBInt
,
// .
false,
// DBInt ,
0.
int value;
bool defined;
// . DBInt
.
DBInt(int value) {
this.value = value;
this.defined = true;
}
// IsNull true, DBInt
.
public bool IsNull { get { return !defined; } }
// Value DBInt 0,
// DBInt .
public int Value { get { return value; } }
// int DBInt.
390
2
public static implicit operator DBInt(int x) {
return new DBInt(x);
}
// DBInt int. ,
// DBInt .
public static explicit operator int(DBInt x) {
if (!x.defined) throw new InvalidOperationException();
return x.value;
}
public static DBInt operator +(DBInt x) {
return x;
}
public static DBInt operator -(DBInt x) {
return x.defined ? -x.value : Null;
}
public static DBInt operator +(DBInt x, DBInt y) {
return x.defined && y.defined? x.value + y.value: Null;
}
public static DBInt operator -(DBInt x, DBInt y) {
return x.defined && y.defined? x.value - y.value: Null;
}
public static DBInt operator *(DBInt x, DBInt y) {
return x.defined && y.defined? x.value * y.value: Null;
}
public static DBInt operator /(DBInt x, DBInt y) {
return x.defined && y.defined? x.value / y.value: Null;
}
public static DBInt operator %(DBInt x, DBInt y) {
return x.defined && y.defined? x.value % y.value: Null;
}
public static DBBool operator ==(DBInt x, DBInt y) {
return x.defined && y.defined? x.value == y.value: DBBool.Null;
}
public static DBBool operator !=(DBInt x, DBInt y) {
return x.defined && y.defined? x.value != y.value: DBBool.Null;
}
public static DBBool operator >(DBInt x, DBInt y) {
return x.defined && y.defined? x.value > y.value: DBBool.Null;
}
public static DBBool operator <(DBInt x, DBInt y) {
return x.defined && y.defined? x.value < y.value: DBBool.Null;
}
public static DBBool operator >=(DBInt x, DBInt y) {
return x.defined && y.defined? x.value >= y.value: DBBool.Null;
}
391
11.4.2
DBBool .
DBBool.True, DBBool.False DBBool.Null, Null
.
.
using System;
public struct DBBool
{
// DBBool.
public static readonly DBBool Null = new DBBool(0);
public static readonly DBBool False = new DBBool(-1);
public static readonly DBBool True = new DBBool(1);
// , 1, 0 1 False, Null True.
sbyte value;
// . 1,
0 1.
DBBool(int value) {
this.value = (sbyte)value;
}
// DBBool. true,
// DBBool , false.
public bool IsNull { get { return value == 0; } }
public bool IsFalse { get { return value < 0; } }
public bool IsTrue { get { return value > 0; } }
// bool DBBool. True
DBBool.True,
// false DBBool.False.
public static implicit operator DBBool(bool x) {
return x? True: False;
}
// DBBool bool. ,
// DBBool Null,
true false.
392
2
public static explicit operator bool(DBBool x) {
if (x.value == 0) throw new InvalidOperationException();
return x.value > 0;
}
// . Null, Null,
// True False.
public static DBBool operator ==(DBBool x, DBBool y) {
if (x.value == 0 || y.value == 0) return Null;
return x.value == y.value? True: False;
}
// . Null, Null,
// True False.
public static DBBool operator !=(DBBool x, DBBool y) {
if (x.value == 0 || y.value == 0) return Null;
return x.value != y.value? True: False;
}
// . True,
False, Null,
// Null, False,
True.
public static DBBool operator !(DBBool x) {
return new DBBool(-x.value);
}
// AND. False,
False,
// Null, Null,
True.
public static DBBool operator &(DBBool x, DBBool y) {
return new DBBool(x.value < y.value? x.value: y.value);
}
// OR. True,
True,
// Null, Null,
False.
public static DBBool operator |(DBBool x, DBBool y) {
return new DBBool(x.value > y.value? x.value: y.value);
}
// . True,
True,
// False.
public static bool operator true(DBBool x) {
return x.value > 0;
}
// . True,
False,
// False.
393
394
12.
, ,
. , ,
, ,
.
, , .
, . , ,
.
. ,
.
,
. ,
.
: N
0 N 1 .
.
, .
, .
12.1
__, _:
_:
__ _
__:
_:
_
_ _
_:
[ _ (/) ]
_:
,
_ ,
__ , _.
_ _: _ ,
, , .
,
_:
T[R] R (__) T.
T[R][R1]...[RN] R T[R1]...[RN].
_
__ . int[][,,][,]
int.
null
.
12.1.1 System.Array
System.Array .
(6.1.6) System.Array,
(6.2.4) System.Array . , System.Array
_. _,
_.
System.Array null
.
12.1.2 IList
T[] System.Collections.Generic.IList<T> (
IList<T>) . T[]
IList<T> .
S T S[] IList<T>
S[] IList<T> (6.1.6).
S T S[] IList<T> (6.2.4).
:
using System.Collections.Generic;
class Test
{
static void Main() {
string[] sa = new string[5];
object[] oa1 = new object[5];
object[] oa2 = sa;
//
// ,
//
//
12.2
__ (7.5.10.4)
, _ (12.6).
. ,
.
. System.Array ,
.
, __,
(5.2).
12.3
__ (7.5.6.1)
A[I1, I2, ..., IN], A , IX
int, uint, long, ulong ,
. ,
, .
foreach (8.8.4).
12.4
, System.Array.
12.5
A B (6.1.6)
(6.2.4) A B, A[R]
B[R], R _ ( ).
. ,
A[R] B[R] ,
B A.
, ,
(7.16.1). :
class Test
{
static void Fill(object[] array, int index, int count, object value) {
for (int i = index; i < index + count; i++) array[i] = value;
}
array[i] Fill ,
, , value, null
, array. Main
Fill ,
(Microsoft Corp.), 1999-2007. .
397
System.ArrayTypeMismatchException
array[i]. , int
string.
__. ,
int[] object[] .
12.6
(10.5),
(8.5.1) (7.5.10.4):
_:
{ __ (/) }
{ __ , }
__:
_
__ , _
_:
_
,
{ } ,.
( ) .
.
.
.
,
int[] a = {0, 2, 4, 6, 8};
int[] a = new int[] {0, 2, 4, 6, 8};
,
.
, .
. ,
int[] 5,
:
a[0] = 0; a[1] = 2; a[2] = 4; a[3] = 6; a[4] = 8;
,
. ,
, .
.
. ,
int[,] b = {{0, 1}, {2, 3}, {4, 5}, {6, 7}, {8, 9}};
int[,] b = new int[5, 2];
398
:
b[0,
b[1,
b[2,
b[3,
b[4,
0]
0]
0]
0]
0]
=
=
=
=
=
0;
2;
4;
6;
8;
b[0,
b[1,
b[2,
b[3,
b[4,
1]
1]
1]
1]
1]
=
=
=
=
=
1;
3;
5;
7;
9;
,
, ,
. :
int i = 3;
int[] x = new int[3] {0, 1, 2};
//
int[] y = new int[i] {0, 1, 2};
// , i
int[] z = new int[3] {0, 1, 2, 3}; // ,
y ,
. z
,
.
399
13.
. , ,
.
, .
, , .
. ,
, .
13.1
_ _ (9.6), .
_:
(/) _ (/) partial (/) interface
__ (/)
_ (/) ___ (/)
_ ;(/)
_ (17),
_ (13.1.1), partial,
interface , ,
__ (10.1.3), _ (13.1.2),
___ (10.1.5)
_ (13.1.4), .
13.1.1
_ :
_:
_
_ _
_:
new
public
protected
internal
private
.
new , .
, (. 10.3.4).
public, protected, internal private .
(3.5.1).
13.1.2 partial
partial , _
.
,
10.2.
13.1.3
,
.
,
, .
_::
: __
_
_ .
(3.5.4). , private
internal _ public
.
.
. ,
, .
.
interface IControl
{
void Paint();
}
interface ITextBox: IControl
{
void SetText(string text);
}
interface IListBox: IControl
{
void SetItems(string[] items);
}
interface IComboBox: ITextBox, IListBox {}
_:
{ __ (/) }
13.2
, , ,
.
__:
__
__ __
__:
__
__
__
__
, .
, , .
, , , , ,
.
public.
. ,
abstract, public, protected, internal, private,
virtual, override static.
,
public delegate void StringListEvent(IStringList sender);
public interface IStringList
{
void Add(string s);
int Count { get; }
event StringListEvent Changed;
}
, : ,
, .
_ (3.3), __,
_,
. __ :
, .
(3.6) ,
, , ,
, ref out.
,
.
,
.
403
.
, .
, .
,
. ,
new, ,
. 3.7.1.2.
new , ,
. ,
new.
, object , , -
(13.2). , object
(7.3.1).
13.2.1
__:
__:
(/) new (/)
__
( __ (/) )
____ (/) ;
, __,
__ , (10.6).
,
.
13.2.2
__:
__:
(/) new (/) { ___ }
___:
(/) get ;
(/) set ;
(/) get ; (/) set ;
(/) set ; (/) get ;
,
(10.7).
(10.7.2) ,
. , , :
, .
13.2.3
__:
__:
(/) new (/) event ... ;
404
,
(10.8).
13.2.4
__:
__:
(/) new (/) this [ __ ]
{ ___ }
, __
, (10.9).
(10.9) ,
. , , :
, .
13.2.5
(7.5.4)
(7.5.6.2) I.M I[A], I , M ,
, A .
, (
),
(7.3), (7.5.6.2) (7.5.5.1)
:
.
, ( )
.
.
.
interface IList
{
int Count { get; set; }
}
interface ICounter
{
void Count(int i);
}
interface IListCounter: IList, ICounter {}
class C
{
void Test(IListCounter x) {
x.Count(1);
x.Count = 1;
((IList)x).Count = 1;
((ICounter)x).Count(1);
}
}
//
//
// , IList.Count.set
// , ICounter.Count
,
(7.3) Count IListCounter .
(Microsoft Corp.), 1999-2007. .
405
, x
.
,
.
interface IInteger
{
void Add(int i);
}
interface IDouble
{
void Add(double d);
}
interface INumber: IInteger, IDouble {}
class C
{
void Test(INumber n) {
n.Add(1);
// IInteger.Add
n.Add(1.0);
// IDouble.Add
((IInteger)n).Add(1); // IInteger.Add
((IDouble)n).Add(1);
// IDouble.Add
}
}
n.Add(1) IInteger.Add
, 7.4.3. n.Add(1.0)
IDouble.Add. ,
, .
interface IBase
{
void F(int i);
}
interface ILeft: IBase
{
new void F(int i);
}
interface IRight: IBase
{
void G();
}
interface IDerived: ILeft, IRight {}
class A
{
void Test(IDerived d) {
d.F(1);
// ILeft.F
((IBase)d).F(1);
// IBase.F
((ILeft)d).F(1);
// ILeft.F
((IRight)d).F(1);
// IBase.F
}
}
406
13.3
.
, , .
, . ,
interface IControl
{
void Paint();
}
interface ITextBox: IControl
{
void SetText(string text);
}
Clone System.ICloneable.Clone.
13.4
. ,
,
. :
interface ICloneable
{
object Clone();
}
interface IComparable
{
int CompareTo(object other);
}
407
, ,
. ,
. :
interface IControl
{
void Paint();
}
interface ITextBox: IControl
{
void SetText(string text);
}
class TextBox: ITextBox
{
public void Paint() {...}
public void SetText(string text) {...}
}
,
13.4.2.
13.4.1
. , ,
, . :
interface IList<T>
{
T[] GetElements();
}
interface IDictionary<K,V>
{
V this[K key];
void Add(K key, V value);
}
class List<T>: IList<T>, IDictionary<int,T>
{
T[] IList<T>.GetElements() {...}
T IDictionary<int,T>.this[int index] {...}
408
2
void IDictionary<int,T>.Add(int index, T value) {...}
}
IDictionary<int,T>.this IDictionary<int,T>.Add
.
.
.
, , , -
Close,
Dispose IDisposable :
interface IDisposable
{
void Dispose();
}
class MyFile: IDisposable
{
void IDisposable.Dispose() {
Close();
}
public void Close() {
//
System.GC.SuppressFinalize(this);
}
}
, .
,
.
,
abstract, virtual, override static, .
.
,
. , ,
.
:
,
. ,
, ,
.
.
,
.
,
, , ,
. ,
(Microsoft Corp.), 1999-2007. .
409
IComparable.CompareTo ,
IComparable Shape
ICloneable.
class Shape: ICloneable
{
object ICloneable.Clone() {...}
}
class Ellipse: Shape
{
object ICloneable.Clone() {...}
}
//
ICloneable.Clone Ellipse ,
ICloneable Ellipse.
, . ,
interface IControl
{
void Paint();
}
interface ITextBox: IControl
{
void SetText(string text);
}
class TextBox: ITextBox
{
void IControl.Paint() {...}
}
Paint IControl.Paint.
13.4.2
, ,
. ,
. , ,
:
interface I<T>
{
void F();
}
410
2
class X<U,V>: I<U>, I<V>
{
void I<U>.F() {...}
void I<V>.F() {...}
}
, :
I<int> x = new X<int,int>();
x.F();
, ,
:
L , ,
C.
L , L.
C L
L , C .
.
X L I<U> I<V>.
, , U V
, .
, :
interface I<T>
{
void F();
}
class Base<U>: I<U>
{
void I<U>.F() {}
}
class Derived<U,V>: Base<U>, I<V> //
void I<V>.F() {}
}
, , Derived<U,V>
I<U>, I<V>.
I<int> x = new Derived<int,int>();
x.F();
, ,
, (
),
.
411
, ,
.
interface I<A,B,C>
{
void F<T>(T t) where T: A;
void G<T>(T t) where T: B;
void H<T>(T t) where T: C;
}
class C: I<object,C,string>
{
public void F<T>(T t) {...}
//
public void G<T>(T t) where T: C {...}
//
public void H<T>(T t) where T: string {...} //
}
void I<object,C,string>.H<T>(T t) {
string s = t; //
H<T>(t);
}
. , t s , T
T: string, .
13.4.4
,
.
.
C
, C. I.M,
I , M,
S, C C
:
412
S , I M,
I.M.
, S , ,
M, I.M .
, I.M.
, S ,
, , ,
.
,
C, . ,
, .
A B
.
A B , A B , .
A B , A B , A , B
(A ,
).
A B , A B .
A B , A B , A
, B (A ,
).
:
, ,
.
,
.
interface ICloneable
{
object Clone();
}
class C: ICloneable
{
object ICloneable.Clone() {...}
public object Clone() {...}
}
interface IControl
{
void Paint();
}
413
interface IForm
{
void Paint();
}
class Page: IControl, IForm
{
public void Paint() {...}
}
class C: IDerived
{
int IBase.P { get {...} }
int IDerived.P() {...}
}
class C: IDerived
{
public int P { get {...} }
}
class C: IDerived
{
int IBase.P { get {...} }
public int P() {...}
}
,
.
interface IControl
{
void Paint();
}
interface ITextBox: IControl
{
void SetText(string text);
}
414
2
interface IListBox: IControl
{
void SetItems(string[] items);
}
class ComboBox: IControl, ITextBox, IListBox
{
void IControl.Paint() {...}
void ITextBox.SetText(string text) {...}
void IListBox.SetItems(string[] items) {...}
}
IControl, ,
IControl, ITextBox, IControl,
IListBox. . ,
ITextBox IListBox
IControl, ComboBox : IControl, ITextBox
IListBox.
.
interface Interface1
{
void F();
}
class Class1
{
public void F() {}
public void G() {}
}
class Class2: Class1, Interface1
{
new public void G() {}
}
415
Control.Paint();
TextBox.Paint();
Control.Paint();
Control.Paint();
,
.
,
interface IControl
{
void Paint();
}
class Control: IControl
{
public virtual void Paint() {...}
}
class TextBox: Control
{
public override void Paint() {...}
}
Control c = new Control();
TextBox t = new TextBox();
IControl ic = c;
IControl it = t;
c.Paint();
//
t.Paint();
//
ic.Paint();
//
it.Paint();
//
Control.Paint();
TextBox.Paint();
Control.Paint();
TextBox.Paint();
,
.
, ,
. :
interface IControl
{
void Paint();
}
class Control: IControl
{
void IControl.Paint() { PaintControl(); }
protected virtual void PaintControl() {...}
}
416
2
class TextBox: Control
{
protected override void PaintControl() {...}
}
, Control,
IControl.Paint PaintControl.
13.4.6
, ,
.
,
. ,
, .
,
interface IControl
{
void Paint();
}
class Control: IControl
{
void IControl.Paint() {...}
}
class MyControl: Control, IControl
{
public void Paint() {}
}
417
IMethods Derived
Derived.F, Base.IMethods.G, Derived.IMethods.H Base.I.
.
. :
interface IBase
{
void F();
}
interface IDerived: IBase
{
void G();
}
class C: IDerived
{
void IBase.F() {...}
void IDerived.G() {...}
}
class D: C, IDerived
{
public void F() {...}
}
IDerived IBase,
IBase.F D.F.
13.4.7
, , ,
, . ,
. :
interface IMethods
{
void F();
void G();
}
abstract class C: IMethods
{
public abstract void F();
public abstract void G();
}
IMethods F G ,
C , .
, ,
. :
interface IMethods
{
void F();
void G();
}
418
2
abstract class C: IMethods
{
void IMethods.F() { FF(); }
void IMethods.G() { GG(); }
protected abstract void FF();
}
C , ,
FF GG, IMethods.
419
14.
(4.1),
.
:
enum Color
{
Red,
Green,
Blue
}
14.1
.
enum , ,
.
_:
_ enum
_ _ ;
_:
: _
_:
{ __ }
{ __ , }
,
. ,
. byte,
sbyte, short, ushort, int, uint, long ulong. , char
. ,
, int.
:
enum Color: long
{
Red,
Green,
Blue
}
long.
long, , ,
long, int,
.
14.2
_ , ,
:
_:
_
_ _
_:
new
public
protected
internal
private
,
.
,
(10.1.1). ,
abstract sealed.
.
14.3
,
. .
__:
__
__ , __
__:
= _
.
.
. :
enum Color: uint
{
Red = -1,
Green = -2,
Blue = -3
}
, -1, -2 -3
uint.
.
:
enum Color
{
Red,
Green,
Blue,
Max = Blue
}
422
, Blue Max
.
.
_, ,
,
. ,
:
,
, ;
.
, ,
.
:
using System;
enum Color
{
Red,
Green = 10,
Blue
}
class Test
{
static void Main() {
Console.WriteLine(StringFromColor(Color.Red));
Console.WriteLine(StringFromColor(Color.Green));
Console.WriteLine(StringFromColor(Color.Blue));
}
static string StringFromColor(Color c) {
switch (c) {
case Color.Red:
return String.Format("Red = {0}", (int) c);
case Color.Green:
return String.Format("Green = {0}", (int) c);
case Color.Blue:
return String.Format("Blue = {0}", (int) c);
default:
return " ";
}
}
}
. :
Red = 0
Green = 10
Blue = 11
Red (
);
(Microsoft Corp.), 1999-2007. .
423
Green 10;
Blue , ,
.
, ,
. ,
, .
,
.
:
enum Circular
{
A = B,
B
}
, A B . A
B , B A .
.
.
.
.
- ,
.
14.4 System.Enum
System.Enum (
), , System.Enum,
. (4.3.1)
System.Enum, (4.3.2) System.Enum
.
, System.Enum _. , _,
_. System.Enum
System.ValueType (4.1.1), , , object.
System.Enum null
.
14.5
;
(6.2.2), ,
. , ,
. ,
,
.
( ,
: . 14.3). ,
E v, (E)v.
424
System.Enum (
System.ValueType object). ,
.
425
15.
, , C++, Pascal Modula
. C++,
-, C++, , .
, System.Delegate.
, ,
, . , ,
. ,
, .
.
,
, ; (15.1)
. .
15.1
_ _ (9.6), .
_:
_ delegate __
__
( __ )
___ ;
_:
_
_ _
_:
new
public
protected
internal
private
,
.
new , ;
, ,
10.3.4.
public, protected, internal private .
, ,
(3.5.1).
.
__ ,
__ .
__ .
C# , . ,
. ,
(7.9.8).
:
delegate int D1(int i, double d);
class A
{
public static int M1(int a, double b) {...}
}
class B
{
delegate int D2(int c, double d);
public static int M1(int f, double g) {...}
public static void M2(int k, double l) {...}
public static int M3(int g) {...}
}
D1 D2 A.M1 B.M1,
;
, . D1 D2
B.M2, B.M3 B.M4,
.
,
.
.
,
. :
delegate bool Predicate<T>(T value);
class X
{
static bool F(int i) {...}
static bool G(string s) {...}
}
, System.Delegate
.
, , .
(15.2) , ,
. , ,
: ,
, .
+ (7.7.4) += (7.16.2).
- (7.7.5) -= (7.16.2).
(7.9.8).
:
delegate void D(int x);
class C
{
public static void M1(int i) {...}
public static void M2(int i) {...}
}
class Test
{
static void Main() {
D cd1 = new D(C.M1);
D cd2 = new D(C.M2);
D cd3 = cd1 + cd2;
D cd4 = cd3 + cd1;
D cd5 = cd4 + cd3;
}
//
//
// M1
// M1
// M1
M1
M2
+ M2
+ M2 + M1
+ M2 + M1 + M1 + M2
cd1 cd2 .
cd3 , M1 M2, .
cd4 M1, M2 M1, . , cd5
M1, M2, M1, M1 M2, . (
) . 15.4.
15.2
M D, :
D M , D
ref out, M;
( ref out)
(6.1.1) (6.1.6)
D M;
ref out D , M;
M D.
429
15.3
__ (7.5.10.5)
. :
, __,
( null) ,
__,
:
delegate void D(int x);
class C
{
public static void M1(int i) {...}
public void M2(int i) {...}
}
class Test
{
static void Main() {
D cd1 = new D(C.M1);
C t = new C();
D cd2 = new D(t.M2);
D cd3 = new D(cd2);
}
}
//
//
//
.
, ,
;
.
15.4
C# .
, ,
, , , ,
. ( . 7.5.5.3).
, ,
, ,
, .
, ,
, , .
, .
(10.6.1.2),
; , ,
, .
,
.
,
, ,
, , , .
430
, null,
System.NullReferenceException.
, , , :
using System;
delegate void D(int x);
class C
{
public static void M1(int i) {
Console.WriteLine("C.M1: " + i);
}
public static void M2(int i) {
Console.WriteLine("C.M2: " + i);
}
public void M3(int i) {
Console.WriteLine("C.M3: " + i);
}
}
class Test
{
static void Main() {
D cd1 = new D(C.M1);
cd1(-1);
// M1
D cd2 = new D(C.M2);
cd2(-2);
// M2
D cd3 = cd1 + cd2;
cd3(10);
// M1, M2
cd3 += cd1;
cd3(20);
// M1, M2, M1
C c = new C();
D cd4 = new D(c.M3);
cd3 += cd4;
cd3(30);
// M1, M2, M1, M3
cd3 -= cd1;
cd3(40);
// M1
// M1, M2, M3
cd3 -= cd4;
cd3(50);
// M1, M2
cd3 -= cd2;
cd3(60);
// M1
cd3 -= cd2;
cd3(60);
//
// M1
cd3 -= cd1;
// , cd3 null
//
// System.NullReferenceException
cd3(70);
cd3 -= cd1;
//
}
}
cd3 += cd1;, .
. , ,
.
(Microsoft Corp.), 1999-2007. .
431
432
-1
-2
10
10
20
20
20
30
30
30
30
40
40
40
50
50
60
60
16.
C# ,
, , .
C# C++, :
C# ,
System.Exception. C++
;
C# finally (8.10) ,
, .
C++ ;
C# , ,
null,
.
16.1
.
throw (8.9.5) .
, throw.
,
C#, ,
. , (7.7.2)
System.DivideByZeroException, . ,
, . 16.4.
16.2 System.Exception
System.Exception .
, :
Message , string,
, ,
catch InnerException. , NULL,
, .
, , .
System.Exception.
16.3
try (8.10).
, catch,
, .
try, catch
try . , , ,
try, .
, catch,
,
. catch, ,
.
catch ,
catch. catch,
finally try
, , .
catch , :
catch (10.12)
, System.TypeInitializationException
, .
System.TypeInitializationException , ;
catch ,
, . .
, , .
,
( ).
( object) , .
16.4
C#.
434
2
System.ArithmeticException
,
,
System.DivideByZeroException
System.OverflowException.
System.ArrayTypeMismatchException
,
.
System.DivideByZeroException
System.IndexOutOfRangeException
System.InvalidCastException
System.NullReferenceException
,
, ,
.
System.OutOfMemoryException
( new).
System.OverflowException
checked.
System.StackOverflowException
-
;
.
System.TypeInitializationException
,
catch
.
435
17.
C# ,
. ,
_ public, protected, internal private.
C# ,
.
. ,
HelpAttribute, (
),
.
(17.1),
(17.1.2). C#
(17.2)
(17.3).
17.1
, System.Attribute,
. ,
. Attribute.
, .
17.1.1
AttributeUsage (17.4.1) , .
AttributeUsage (17.1.2),
, . :
using System;
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Interface)]
public class SimpleAttribute: Attribute
{
...
}
SimpleAttribute,
_ _. :
[Simple] class Class1 {...}
[Simple] interface Interface1 {...}
Simple.
SimpleAttribute, Attribute,
Simple. ,
:
[SimpleAttribute] class Class1 {...}
[SimpleAttribute] interface Interface1 {...}
AuthorAttribute. :
[Author("Brian Kernighan"), Author("Dennis Ritchie")]
class Class1
{
...
}
Author.
AttributeUsage , Inherited,
, ,
. Inherited True,
. Inherited False,
. , True.
X, AttributeUsage, :
using System;
class X: Attribute {...}
:
using System;
[AttributeUsage(
AttributeTargets.All,
AllowMultiple = false,
Inherited = true)
]
class X: Attribute {...}
17.1.2
.
. , ,
438
.
:
using System;
[AttributeUsage(AttributeTargets.Class)]
public class HelpAttribute: Attribute
{
public HelpAttribute(string url) {
...
}
public string Topic {
get {...}
set {...}
}
//
//
HelpAttribute, url
Topic. Url ,
, .
:
[Help("http://www.mycompany.com/.../Class1.htm")]
class Class1
{
...
}
[Help("http://www.mycompany.com/.../Misc.htm", Topic = "Class2")]
class Class2
{
...
}
17.1.3
, :
: bool, byte, char, double, float, int, long, sbyte, short, string, uint,
ulong ushort;
object;
System.Type;
, , , (
), (17.2);
, ,
.
439
17.2
.
, .
(
) _ (9.6), __ (10.1.5),
__ (13.2), __ (11.2),
__ (14.3), __ (10.7.2),
____ (10.8.1) __ (10.6.1).
. ,
, .
,
, . , [A][B], [B][A], [A, B] [B, A]
.
_:
__
__:
__
__ __
__:
[ ____ _ ]
[ ____ _ , ]
____:
___ :
___:
assembly
module
:
_
_:
_
_ _
_:
[ ___ _ ]
[ ___ _ , ]
___:
__ :
__:
field
event
method
param
property
return
type
_:
_ ,
440
:
_ _
_:
_
_:
( __ )
( __ , __ )
( __ )
__:
_
__ , _
_:
__
__:
_
__ , _
_:
= __
__:
_
. ( ) .
__;
, , __,
, .
.
_ . _ _,
. . :
class Class1 {}
[Class1] class Class2 {} //
, Class1
, Class1 .
.
___.
,
____.
, ___
(
). , ___
, .
:
, , ,
. ,
___.
___ assembly ,
(Microsoft Corp.), 1999-2007. .
441
; ___ module ,
;
, , ,
. ___
. ___ type ,
; ___ return
, ;
, , ,
. ___
. ___ method ,
; ___ return ,
;
, , ,
. ___
. ___ method
, ; ___
return , ;
, , ,
, ( )
add remove. ___
. ___ event
, ; ___
field , ;
___ method , ;
, get ,
, .
___ .
___ method , ;
___ return ,
;
, set ,
, .
___ .
___ method , ;
___ param ,
; ___ return ,
;
, add remove ,
, .
___ .
___ method , ;
___ param ,
; ___ return ,
.
___ , .
, , type:
[type: Author("Brian Kernighan")]
class Class1 {}
442
2
[Author("Dennis Ritchie")]
class Class2 {}
___. ,
param :
[param: Author("Brian Kernighan")]
class Class1 {}
//
Attribute. _ _
, . ,
, . _
,
(2.4.2), ,
. :
using System;
[AttributeUsage(AttributeTargets.All)]
public class X: Attribute
{}
[AttributeUsage(AttributeTargets.All)]
public class XAttribute: Attribute
{}
[X]
class Class1 {}
// :
[XAttribute]
class Class2 {}
// XAttribute
[@X]
class Class3 {}
[@XAttribute]
class Class4 {}
// X
// XAttribute
X XAttribute. [X] ,
X, XAttribute.
. [XAttribute]
( , XAttributeAttribute).
X,
XAttribute, :
using System;
[AttributeUsage(AttributeTargets.All)]
public class XAttribute: Attribute
{}
[X]
class Class1 {}
// XAttribute
[XAttribute]
class Class2 {}
// XAttribute
[@X]
class Class3 {}
// : "X"
. :
using System;
443
[AttributeUsage(AttributeTargets.Class)]
public class HelpStringAttribute: Attribute
{
string value;
public HelpStringAttribute(string value) {
this.value = value;
}
public string Value {
get {...}
}
}
[HelpString(" Class1")]
[HelpString(" Class1")]
public class Class1 {}
,
HelpString, ,
Class1.
E __, :
E (17.1.3);
E :
o
System.Type;
__.
:
using System;
[AttributeUsage(AttributeTargets.Class)]
public class TestAttribute: Attribute
{
public int P1 {
get {...}
set {...}
}
public Type P2 {
get {...}
set {...}
}
public object P3 {
get {...}
set {...}
}
}
[Test(P1 = 1234, P3 = new int[] {1, 3, 5}, P2 = typeof(float))]
class MyClass {}
_typeof (7.5.11), ,
, ,
.
.
444
2
class A: Attribute
{
public A(Type t) {...}
}
class G<T>
{
[A(typeof(T))] T t;
}
class X
{
[A(typeof(List<int>))] int x;
[A(typeof(List<>))] int y;
}
// ,
// ,
// ,
17.3
, .
, .
,
.
,
, .
17.3.1
T, __ P
__ N :
__
new T(P). ,
C T, ;
C , ;
_ Arg N:
Name _ Arg;
Name
T. T , .
:
T, C T, __ P
__ N.
17.3.2
T, C T,
__ P __ N. ,
:
__
new T(P) C, .
, O T;
_ Arg N :
445
Value __ Arg;
Name O, Value;
Name O. Value;
O, T,
__ P __ N.
17.4
. :
System.AttributeUsageAttribute (17.4.1), ,
System.Diagnostics.ConditionalAttribute (17.4.2),
System.ObsoleteAttribute (17.4.3), .
17.4.1 AttributeUsage
AttributeUsage .
, AttributeUsage, System.Attribute,
. .
namespace System
{
[AttributeUsage(AttributeTargets.Class)]
public class AttributeUsageAttribute: Attribute
{
public AttributeUsageAttribute(AttributeTargets validOn) {...}
public virtual bool AllowMultiple { get {...} set {...} }
public virtual bool Inherited { get {...} set {...} }
public virtual AttributeTargets ValidOn { get {...} }
}
public enum AttributeTargets
{
Assembly = 0x0001,
Module
= 0x0002,
Class
= 0x0004,
Struct
= 0x0008,
Enum
= 0x0010,
Constructor = 0x0020,
Method
= 0x0040,
Property = 0x0080,
Field
= 0x0100,
Event
= 0x0200,
Interface = 0x0400,
Parameter
= 0x0800,
Delegate = 0x1000,
ReturnValue = 0x2000,
446
2
All = Assembly | Module | Class | Struct | Enum | Constructor |
Method | Property | Field | Event | Interface | Parameter |
Delegate | ReturnValue
}
}
17.4.2 Conditional
Conditional
.
namespace System.Diagnostics
{
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class,
AllowMultiple = true)]
public class ConditionalAttribute: Attribute
{
public ConditionalAttribute(string conditionString) {...}
public string ConditionString { get {...} }
}
}
, Conditional, . Conditional
.
, , , .
, ; ( ) .
:
_ _.
Conditional ,
;
void;
override.
virtual.
Conditional;
.
.
, ,
__. :
#define DEBUG
using System;
using System.Diagnostics;
class Class1
{
[Conditional("DEBUG")]
public static void M() {
Console.WriteLine("Executed Class1.M");
}
}
447
class Class2
{
public static void Test() {
Class1.M();
}
}
class2.cs:
#define DEBUG
class Class2
{
public static void G() {
Class1.F();
}
}
// F
class3.cs:
#undef DEBUG
class Class3
{
public static void H() {
Class1.F();
}
}
// F
448
2
class Class1
{
[Conditional("DEBUG")]
public virtual void M() {
Console.WriteLine("Class1.M ");
}
}
class2.cs:
using System;
class Class2: Class1
{
public override void M() {
Console.WriteLine("Class2.M ");
base.M();
// base.M !
}
}
class3.cs:
#define DEBUG
using System;
class Class3
{
public static void Test() {
Class2 c = new Class2();
c.M();
}
}
// M
Class2 M, . ,
, DEBUG,
. , Class2.M .
_ .
(17.1), Conditional,
. ,
, Conditional. :
using System;
using System.Diagnostics;
[Conditional("ALPHA")]
[Conditional("BETA")]
public class TestAttribute : Attribute {}
TestAttribute ,
ALPHA BETA.
(17.2) ,
,
.
,
. :
test.cs:
(Microsoft Corp.), 1999-2007. .
449
using System;
using System.Diagnostics;
[Conditional(DEBUG)]
public class TestAttribute : Attribute {}
class1.cs:
#define DEBUG
[Test]
// TestAttribute
class Class1 {}
class2.cs:
#undef DEBUG
[Test]
// TestAttribute
class Class2 {}
, Obsolete,
. , ,
450
false.
, true.
:
[Obsolete(" ; B")]
class A
{
public void F() {}
}
class B
{
public void F() {}
}
class Test
{
static void Main() {
A a = new A();
a.F();
}
}
//
A Obsolete. A Main
, ;
B.
17.5
. Microsoft .NET C#.
17.5.1 COM Win32
.NET , C#
, COM DLL
Win32. , DllImport static extern, ,
DLL Win32.
System.Runtime.InteropServices,
.NET.
.NET IndexerName
.NET
.NET. IndexerName ,
Item. IndexerName
.
namespace System.Runtime.CompilerServices.CSharp
{
[AttributeUsage(AttributeTargets.Property)]
public class IndexerNameAttribute: Attribute
{
public IndexerNameAttribute(string indexerName) {...}
public string Value { get {...} }
451
18.
C#, , C C++ ,
. C#
, .
C# , C C++. C#
, ,
. ,
C C++.
C C++
C#, .
, , , ,
. C#
.
,
, .
C
C#.
, .
unsafe, ,
.
18.1
C# .
unsafe ,
_:
, , unsafe,
( ,
) ;
, , , , , , ,
unsafe,
;
_ .
.
. (...)
, .
_:
...
unsafe
_:
...
unsafe
_:
...
unsafe
_:
...
unsafe
_:
...
unsafe
_:
...
unsafe
_:
...
unsafe
_:
...
unsafe
_:
...
unsafe
_:
...
unsafe
_:
...
unsafe
_:
extern unsafe ~ ( ) _
unsafe extern ~ ( ) _
__:
extern unsafe static
unsafe extern static
extern static unsafe
unsafe static extern
static extern unsafe
static unsafe extern
_:
...
_
_:
unsafe
public unsafe struct Node
{
public int Value;
public Node* Left;
public Node* Right;
}
454
unsafe, ,
. Left Right
. :
public struct Node
{
public int Value;
public unsafe Node* Left;
public unsafe Node* Right;
}
unsafe .
, ,
unsafe .
public class A
{
public unsafe virtual void F() {
char* p;
...
}
}
public class B: A
{
public override void F() {
base.F();
...
}
}
unsafe F A F
, . F
B unsafe, , , F
B .
,
public unsafe class A
{
public virtual void F(char* p) {...}
}
public class B: A
{
public unsafe override void F(char* p) {...}
}
, F ,
. ,
A, unsafe , B.
18.2
(4) _, _
_. _ typeof (7.5.10.6)
, .
:
...
_
(Microsoft Corp.), 1999-2007. .
455
_ _ void
*:
_:
_ *
void *
_:
, * , _ .
, .
( ),
, .
, ,
_.
_ , _
_ . , _
:
sbyte, byte, short, ushort, int, uint, long, ulong, char, float, double, decimal bool;
_;
_;
_,
_.
: ()
, .
:
byte*
byte
char*
char
int**
int
int*[]
int
void*
.
C C++, , C# *
,
. :
int* pi, pj; // NOT int *pi, *pj;
, T* T.
* (18.5.1) . ,
P int*, *P int, ,
P.
456
, null.
null . null
.
void* . ,
void*,
. void* (
).
. ,
object object.
, (4.3) .
.
18.4.
_ volatile (10.5.3).
ref out,
,
, ,
, , . .
using System;
class Test
{
static int value = 20;
unsafe static void F(out int* pi1, ref int* pi2) {
int i = 10;
pi1 = &i;
, . ,
int,
int,
, , null:
457
* (18.5.1);
-> (18.5.2);
[] (18.5.3);
& (18.5.4);
++ (18.5.5);
+
(18.5.6);
stackalloc (18.7);
fixed ,
(18.6).
18.3
(18.5.4) fixed (18.6) :
.
,
. ( ,
, .)
, .
( .)
& (18.5.4) .
,
fixed (18.6),
fixed.
, :
, _ (7.5.2),
,
;
, __ (7.5.4) V.I, V
_;
, ____ (18.5.1)
*P, ____ (18.5.2) P->I
____ (18.5.3) P[E].
.
458
, .
, ref out ,
, , . ,
, , ,
.
18.4
(6.1)
:
_ void*.
null _.
, (6.2)
:
_ _;
, (6.3.1)
:
_ void*.
.
, ,
.
,
, , .
, : A
B, C,
A C.
,
:
char c = 'A';
char* pc = &c;
void* pv = pc;
int* pi = (int*)pv;
int i = *pi;
//
*pi = 123456;
//
.
. ,
double :
using System;
459
class Test
{
unsafe static void Main() {
double d = 123.456e23;
unsafe {
byte* pb = (byte*)&d;
for (int i = 0; i < sizeof(double); ++i)
Console.Write("{0:X2} ", *pb++);
Console.WriteLine();
}
}
}
, .
. 32-
64-
, uint
ulong .
18.5
,
. ,
, _
(7.5.2), __ (7.5.4), _ (7.5.5) __ (7.5.6)
.
____ (7.5)
_ (7.6) :
____:
...
____
____
_sizeof
_:
...
____
_
.
.
18.5.1
____ (*)
_.
____:
* _
*
, . *P, P
T*, T. * void*
.
* null .
, ,
System.NullReferenceException.
460
, *
. *
(. 18.4)
.
,
*P, (5.3.1).
18.5.2
____ _, "->"
.
____:
_ ->
P->I, P ,
void*, I , P.
P->I , (*P).I.
(*) . 18.5.1. ( .) .
7.5.4.
using System;
struct Point
{
public int x;
public int y;
public override string ToString() {
return "(" + x + "," + y + ")";
}
}
class Test
{
static void Main() {
Point point;
unsafe {
Point* p = &point;
p->x = 10;
p->y = 20;
Console.WriteLine(p->ToString());
}
}
}
-> .
P->I (*P).I, Main :
461
class Test
{
static void Main() {
Point point;
unsafe {
Point* p = &point;
(*p).x = 10;
(*p).y = 20;
Console.WriteLine((*p).ToString());
}
}
}
18.5.3
____ ____,
, [" "].
____:
____ [ ]
P[E], P ,
void*, E , int,
uint, long ulong.
P[E] , *(P + E).
(*) . 18.5.1. (+)
. 18.5.6.
class Test
{
static void Main() {
unsafe {
char* p = stackalloc char[256];
for (int i = 0; i < 256; i++) p[i] = (char)i;
}
}
}
for.
P[E] *(P + E), :
class Test
{
static void Main() {
unsafe {
char* p = stackalloc char[256];
for (int i = 0; i < 256; i++) *(p + i) = (char)i;
}
}
}
,
. , C C++.
18.5.4
_ (&), _.
462
_:
& _
E T (18.3),
&E , E. T*,
. , E
, E E
. fixed
(18.6), . 7.5.4,
,
readonly, , . , , .
.
& , , &
, ,
, .
, .
using System;
class Test
{
static void Main() {
int i;
unsafe {
int* p = &i;
*p = 123;
}
Console.WriteLine(i);
}
}
i &i, p.
*p i,
, , .
& ,
. , API
, API. API
,
.
18.5.5
++ -- (7.5.9 7.6.5)
, void*. , T*
:
T* ++(T* x);
T* --(T* x);
, x + 1 x - 1, (18.5.6). ,
T* ++ sizeof(T) ,
, -- sizeof(T) , .
,
, .
(Microsoft Corp.), 1999-2007. .
463
18.5.6
+ - (7.7.4 7.7.5)
, void*. , T* :
T*
T*
T*
T*
+(T*
+(T*
+(T*
+(T*
x,
x,
x,
x,
T*
T*
T*
T*
+(int x, T* y);
+(uint x, T* y);
+(long x, T* y);
+(ulong x, T* y);
T*
T*
T*
T*
(T*
(T*
(T*
(T*
x,
x,
x,
x,
int y);
uint y);
long y);
ulong y);
int y);
uint y);
long y);
ulong y);
:
p - q = -14
q - p = 14
,
, , .
18.5.7
==, !=, <, >, <= => (7.9)
. :
bool ==(void* x, void* y);
bool !=(void* x, void* y);
464
2
bool <(void* x, void* y);
bool >(void* x, void* y);
bool <=(void* x, void* y);
bool >=(void* x, void* y);
void*,
.
, , .
18.5.8 sizeof
sizeof , . ,
sizeof, _ (18.2).
_sizeof:
sizeof ( _ )
sizeof int.
sizeof , .
sizeof(sbyte)
sizeof(byte)
sizeof(short)
sizeof(ushort)
sizeof(int)
sizeof(uint)
sizeof(long)
sizeof(ulong)
sizeof(char)
sizeof(float)
sizeof(double)
sizeof(bool)
sizeof
, .
.
, .
, , .
, ,
, .
18.6 fixed
_ (8)
, fixed, ,
.
(Microsoft Corp.), 1999-2007. .
465
_:
...
_fixed
_fixed:
fixed ( _ __fixed ) _
__fixed:
__fixed
__fixed , __fixed
__fixed:
= __fixed
__fixed:
& __
__fixed _
,
__fixed. , fixed,
__fixed, ,
_ fixed. , fixed,
. ,
( ++ --)
ref out.
__fixed :
&, __ (5.3.3)
(18.3) T , T*
, fixed.
fixed;
_ T , T*
, fixed.
,
fixed. fixed
, null ;
string , char*
, fixed.
,
fixed. fixed ,
null;
_ __,
, ,
, fixed.
(18.7.2),
fixed.
, __fixed, fixed ,
, ,
fixed. , ,
__fixed, ,
466
fixed ,
.
, , fixed,
. , ,
fixed, API, ,
API .
( ).
.
class Test
{
static int x;
int y;
unsafe static void F(int* p) {
*p = 1;
}
static void Main() {
Test t = new Test();
int[] a = new int[10];
unsafe {
fixed (int* p = &x) F(p);
fixed (int* p = &t.y) F(p);
fixed (int* p = &a[0]) F(p);
fixed (int* p = a) F(p);
}
}
}
fixed.
, ,
.
&, .
fixed .
, a &a[0] fixed ,
a.
fixed string:
class Test
{
static string name = "xx";
unsafe static void F(char* p) {
for (int i = 0; p[i] != '\0'; ++i)
Console.WriteLine(p[i]);
}
static void Main() {
unsafe {
fixed (char* p = name) F(p);
fixed (char* p = "xx") F(p);
}
}
}
(Microsoft Corp.), 1999-2007. .
467
, 0 Length 1.
, ,
. fixed, p a,
p p + a.Length - 1 .
p[0] p[a.Length - 1] .
.
:
using System;
class Test
{
static void Main() {
int[,,] a = new int[2,3,4];
unsafe {
fixed (int* p = a) {
for (int i = 0; i < a.Length; ++i) //
p[i] = i;
}
}
:
[0,0,0]
[0,1,0]
[0,2,0]
[1,0,0]
[1,1,0]
[1,2,0]
=
=
=
=
=
=
0 [0,0,1] =
4 [0,1,1] =
8 [0,2,1] =
12 [1,0,1] =
16 [1,1,1] =
20 [1,2,1] =
1 [0,0,2] = 2 [0,0,3] = 3
5 [0,1,2] = 6 [0,1,3] = 7
9 [0,2,2] = 10 [0,2,3] = 11
13 [1,0,2] = 14 [1,0,3] = 15
17 [1,1,2] = 18 [1,1,3] = 19
21 [1,2,2] = 22 [1,2,3] = 23
class Test
{
unsafe static void Fill(int* p, int count, int value) {
for (; count != 0; count--) *p++ = value;
}
static void Main() {
int[] a = new int[100];
unsafe {
fixed (int* p = a) Fill(p, 100, -1);
}
}
}
fixed , ,
.
:
468
2
unsafe struct Font
{
public int ;
public fixed char name[32];
}
class Test
{
unsafe static void PutString(string s, char* buffer, int bufSize) {
int len = s.Length;
if (len > bufSize) len = bufSize;
for (int i = 0; i < len; i++) buffer[i] = s[i];
for (int i = len; i < bufSize; i++) buffer[i] = (char)0;
}
Font f;
fixed , ,
.
char*, , ,
null. fixed, p s,
p p + s.Length - 1 ,
p + s.Length null ( '\0').
. , ,
, ,
, .
null API,
C. , , null.
null , ,
null char*.
18.7
C
, API.
18.7.1
,
.
.
(18.1).
469
__:
___
___:
___ fixed
__
--- ;
___:
___
___
___
___:
new
public
protected
internal
private
unsafe
__:
___:
___
___
___
___:
[ _ ]
(17), new
(10.2.2), (10.2.3) unsafe
(18.1). ,
. ,
.
static.
(-),
. sbyte,
byte, short, ushort, int, uint, long, ulong, char, float, double bool.
,
. ,
, , [ ].
,
. int,
.
.
,
,
. ,
470
2
unsafe struct A
{
public fixed int x[5], y[10], z[100];
}
unsafe struct A
{
public fixed int x[5];
public fixed int y[10];
public fixed int z[100];
}
18.7.2
(7.3) , .
_ (7.5.2)
__ (7.5.4).
,
this.I, I .
E.I E I
, E.I
:
E.I ,
;
E , ;
, E (18.3), E.I
__fixed (18.6), ;
E ,
I E. S*, S
I, .
. ,
.
,
.
unsafe struct Font
{
public int size;
public fixed char name[32];
}
class Test
{
unsafe static void PutString(string s, char* buffer, int bufSize) {
int len = s.Length;
if (len > bufSize) len = bufSize;
for (int i = 0; i < len; i++) buffer[i] = s[i];
for (int i = len; i < bufSize; i++) buffer[i] = (char)0;
}
471
18.7.3
(5.3),
.
, ,
, ,
(5.2).
.
18.8
(8.5.1)
, .
__:
_stackalloc
_stackalloc:
stackalloc _ [ ]
_ , ,
.
. ,
, _,
.
stackalloc T[E] , T
(18.2), E int. E * sizeof(T)
( T*) . E ,
. E , ,
. ,
System.StackOverflowException.
.
catch finally (8.10).
, stackalloc.
, ,
. alloca, ,
C C++.
using System;
472
2
class Test
{
static string IntToString(int value) {
int n = value >= 0? value: -value;
unsafe {
char* buffer = stackalloc char[16];
char* p = buffer + 16;
do {
*--p = (char)(n % 10 + '0');
n /= 10;
} while (n != 0);
if (value < 0) *--p = '-';
return new string(p, 0, (int)(buffer + 16 - p));
}
}
static void Main() {
Console.WriteLine(IntToString(12345));
Console.WriteLine(IntToString(-999));
}
}
stackalloc IntToString 16
. .
18.9
stackalloc, C#
, .
.
Memory,
C#:
using System;
using System.Runtime.InteropServices;
public unsafe class Memory
{
// .
// API HeapXXX .
static int ph = GetProcessHeap();
// Private .
private Memory() {}
// .
// .
public static void* Alloc(int size) {
void* result = HeapAlloc(ph, HEAP_ZERO_MEMORY, size);
if (result == null) throw new OutOfMemoryException();
return result;
}
// src dst.
// .
473
[DllImport("kernel32")]
static extern int HeapSize(int hHeap, int flags, void* block);
Memory:
474
2
class Test
{
static void Main() {
unsafe {
byte* buffer = (byte*)Memory.Alloc(256);
try {
for (int i = 0; i < 256; i++) buffer[i] = (byte)i;
byte[] array = new byte[256];
fixed (byte* p = array) Memory.Copy(buffer, p, 256);
}
finally {
Memory.Free(buffer);
}
for (int i = 0; i < 256; i++) Console.WriteLine(array[i]);
}
}
}
256 Memory.Alloc
, 0 255. 256
Memory.Copy . ,
Memory.Free
.
475
A.
C#
XML-. ,
, XML
, . ,
, .
( , ) (
, , ). XML
. ( , , C#.)
.
,
.
,
, ,
XML.
A.1
, ,
XML , .
, ( ///),
, ( /**).
( , )
( , , ), .
(17.2) ,
, .
:
___:
/// _
____:
/** ___ */
___ _
/// ___,
___, _
XML-.
____ ,
_, ,
_
____,
XML-. _ ,
.
.
/// <summary> <c>Point</c>
/// .</summary>
///
public class Point
{
/// <summary> <c>draw</c> .</summary>
void draw() {}
}
XML
(http://www.w3.org/TR/REC-xml). XML
, .
,
A.2. :
<param> . ,
,
. ,
.
, .
, . ,
. , cref,
using, .
(. . " List<T>"),
XML. (. . "List{T}") escape-
XML (. . "List<T>").
<summary>
.
<include> XML-.
,
(, ).
.
A.2
, XML.
. (, .)
478
Appendix A
<c>
A.2.1
<code>
A.2.2
<example>
A.2.3
<exception>
A.2.4
<include>
A.2.5
XML
<list>
A.2.6
<para>
A.2.7
<param>
A.2.8
<paramref>
A.2.9
<permission>
A.2.10
<summary>
A.2.11
<returns>
A.2.12
<see>
A.2.13
<seealso>
A.2.14
See Also
<summary>
A.2.15
<value>
A.2.16
<typeparam>
<typeparamref
>
A.2.1 <c>
,
, .
<code> (A.2.2).
:
<c></c>
.
/// <summary> <c>Point</c>
/// .</summary>
479
A.2.2 <code>
. <c> (A.2.1).
:
<code> </code>
.
///
///
///
///
///
///
///
///
///
///
<summary>
x y.
<example>:
<code>
Point p = new Point(3,5);
p.Translate(-1,3);
</code>
<c>p</c> (2,8).
</example>
</summary>
A.2.3 <example>
, ,
. <code> (A.2.2).
:
<example></example>
.
. <code> (A.2.2).
A.2.4 <exception>
, .
:
<exception cref=""></exception>
cref=""
. , ,
.
, .
.
480
Appendix A
public class DataBaseOperations
{
/// <exception cref="MasterFileFormatCorruptException"></exception>
/// <exception cref="MasterFileLockedOpenException"></exception>
public static void ReadRecord(int flag) {
if (flag == 1)
throw new MasterFileFormatCorruptException();
else if (flag == 2)
throw new MasterFileLockedOpenException();
//
}
}
A.2.5 <include>
XML- .
XML-, XPath
, XML . <include>
XML .
:
<include file="_" path="xpath" />
file="_"
XML-. ,
.
path="xpath"
"docs.xml" :
<?xml version="1.0"?>
<extradoc>
<class name="IntList">
<summary>
.
</summary>
</class>
<class name="StringList">
<summary>
.
</summary>
</class>
</extradoc>
, :
/// <summary>
/// .
/// </summary>
public class IntList { }
(Microsoft Corp.), 1999-2007. .
481
A.2.6 <list>
.
<listheader> . (
.)
<item>.
, . ,
.
:
<list type="bullet" | "number" | "table">
<listheader>
<term></term>
<description></description>
</listheader>
<item>
<term></term>
<description></description>
</item>
<item>
<term></term>
<description></description>
</item>
</list>
, .
, .
.
public class MyClass
{
/// <summary> :
/// <list type="bullet">
/// <item>
/// <description> 1.</description>
/// </item>
/// <item>
/// <description> 2.</description>
/// </item>
/// </list>
/// </summary>
public static void Main () {
// ...
}
}
A.2.7 <para>
, <summary> (A.2.11) <returns>
(A.2.12), .
:
482
Appendix A
<para></para>
.
.
/// <summary> Point.
/// <para>
///
/// Point.</para></summary>
public static void Main() {
// ...
}
A.2.8 <param>
, .
:
<param name=""></param>
.
.
/// <summary>
/// .</summary>
/// <param name="xor"> x.</param>
/// <param name="yor"> y.</param>
public void Move(int xor, int yor) {
X = xor;
Y = yor;
}
A.2.9 <paramref>
, .
, .
:
<paramref name=""/>
483
///
///
///
///
<summary> Point
(<paramref name="xor"/>,<paramref name="yor"/>).</summary>
<param name="xor"> x .</param>
<param name="yor"> y .</param>
A.2.10 <permission>
.
:
<permission cref=""></permission>
cref=""
. , ,
.
.
.
/// <permission cref="System.Security.PermissionSet">
/// .</permission>
public static void Test() {
// ...
}
A.2.11 <summary>
. ( <summary> (A.2.15)
.)
:
<summary></summary>
.
.
/// <summary> <c>Point</c>
/// .</summary>
///
public class Point
{
// ...
}
A.2.12 <returns>
.
484
Appendix A
:
<returns></returns>
.
.
/// <summary> .</summary>
/// <returns>, (x,y),
/// , .</returns>
public override string ToString() {
return "(" + X + "," + Y + ")";
}
A.2.13 <see>
. <seealso> (A.2.14)
, See Also.
:
<see cref=""/>
cref=""
. , ,
.
.
/// <summary>
/// .</summary>
/// <see cref="Translate"/>
public void Move(int xor, int yor) {
X = xor;
Y = yor;
}
/// <summary>
/// x y.
/// </summary>
/// <see cref="Move"/>
public void Translate(int xor, int yor) {
X += xor;
Y += yor;
}
A.2.14 <seealso>
See Also. <see> (A.2.13)
.
:
<seealso cref=""/>
cref=""
(Microsoft Corp.), 1999-2007. .
485
. , ,
.
.
/// <summary> ,
/// .</summary>
/// <seealso cref="operator=="/>
/// <seealso cref="operator!="/>
public override bool Equals(object o) {
// ...
}
A.2.15 <summary>
. <summary> (A.2.11)
.
:
<summary></summary>
.
.
/// <summary> Point
(0,0).</summary>
public Point() : this(0,0) {
}
A.2.16 <value>
.
:
<value> </value>
.
.
/// <value> <c>X</c> x .</value>
public int X
{
get { return x; }
set { x = value; }
}
A.2.17 <typeparam>
, , ,
.
:
<typeparam name=""></typeparam>
486
Appendix A
.
.
/// <summary> .</summary>
/// <typeparam name="T">, .</typeparam>
public class MyList<T> {
...
}
A.2.18 <typeparamref>
, .
, .
:
<typeparamref name=""/>
.
.
/// <summary> <typeparamref
name=T> />> .</summary>
/// <param name="string"> </param>
public List<T> FetchData<T>(string query) {
...
}
A.3
,
.
.
,
.
, , ,
.
A.3.1
:
;
,
. :
487
( , )
( )
( , , ,
)
;
. ,
,
.
, .
, .
, # (U+0023). (,
);
, .
, . .
, CLI, :
o
, ,
:
, , ,
;
out ref @. ,
params, ;
, , [ _ : , ,
_ : ], ,
, , .
, .
, , : .
[] :
, , void, *
. void System.Void;
, , ,
`,
;
, , ,
`` `, ;
, ,
, {,
}.
488
Appendix A
A.3.2
C# ,
, :
, :
enum Color { Red, Blue, Green }
namespace Acme
{
interface IProcess {...}
struct ValueType {...}
class Widget: IProcess
{
public class NestedClass {...}
public interface IMenuItem {...}
public delegate void Del(int i);
public enum Direction { North, South, East, West }
}
class MyList<T>
{
class Helper<U,V> {...}
}
"T:Color"
"T:Acme.IProcess"
"T:Acme.ValueType"
"T:Acme.Widget"
"T:Acme.Widget.NestedClass"
"T:Acme.Widget.IMenuItem"
"T:Acme.Widget.Del"
"T:Acme.Widget.Direction"
T:Acme.MyList`1
T:Acme.MyList`1.Helper`2
:
namespace Acme
{
struct ValueType
{
private int total;
}
489
}
"F:Acme.ValueType.total"
"F:Acme.Widget.NestedClass.value"
"F:Acme.Widget.message"
"F:Acme.Widget.defaultColor"
"F:Acme.Widget.PI"
"F:Acme.Widget.monthlyAverage"
"F:Acme.Widget.array1"
"F:Acme.Widget.array2"
"F:Acme.Widget.pCount"
"F:Acme.Widget.ppValues"
;
namespace Acme
{
class Widget: IProcess
{
static Widget() {...}
public Widget() {...}
}
}
"M:Acme.Widget.#cctor"
"M:Acme.Widget.#ctor"
"M:Acme.Widget.#ctor(System.String)"
;
namespace Acme
{
class Widget: IProcess
{
~Widget() {...}
}
}
"M:Acme.Widget.Finalize"
;
namespace Acme
{
struct ValueType
{
public void M(int i) {...}
}
490
Appendix A
class Widget: IProcess
{
public class NestedClass
{
public void M(int i) {...}
}
public
public
public
public
public
public
public
}
class MyList<T>
{
public void Test(T t) { }
}
class UseList
{
public void Process(MyList<int> list) { }
public MyList<T> GetValues<T>(T inputValue) { return null; }
}
}
"M:Acme.ValueType.M(System.Int32)"
"M:Acme.Widget.NestedClass.M(System.Int32)"
"M:Acme.Widget.M0"
"M:Acme.Widget.M1(System.Char,System.Single@,Acme.ValueType@)"
"M:Acme.Widget.M2(System.Int16[],System.Int32[0:,0:],System.Int64[][])"
"M:Acme.Widget.M3(System.Int64[][],Acme.Widget[0:,0:,0:][])"
"M:Acme.Widget.M4(System.Char*,Color**)"
"M:Acme.Widget.M5(System.Void*,System.Double*[0:,0:][])"
"M:Acme.Widget.M6(System.Int32,System.Object[])"
M:Acme.MyList`1.Test(`0)
M:Acme.UseList.Process(Acme.MyList{System.Int32})
M:Acme.UseList.GetValues``(``0)
;
namespace Acme
{
class Widget: IProcess
{
public int Width { get {...} set {...} }
public int this[int i] { get {...} set {...} }
public int this[string s, int i] { get {...} set {...} }
}
}
"P:Acme.Widget.Width"
"P:Acme.Widget.Item(System.Int32)"
"P:Acme.Widget.Item(System.String,System.Int32)"
491
namespace Acme
{
class Widget: IProcess
{
public event Del AnEvent;
}
}
"E:Acme.Widget.AnEvent"
;
namespace Acme
{
class Widget: IProcess
{
public static Widget operator+(Widget x) {...}
}
}
"M:Acme.Widget.op_UnaryPlus(Acme.Widget)"
: op_UnaryPlus, op_UnaryNegation,
op_LogicalNot, op_OnesComplement, op_Increment, op_Decrement, op_True op_False.
;
namespace Acme
{
class Widget: IProcess
{
public static Widget operator+(Widget x1, Widget x2) {...}
}
}
"M:Acme.Widget.op_Addition(Acme.Widget,Acme.Widget)"
: op_Addition, op_Subtraction,
op_Multiply, op_Division, op_Modulus, op_BitwiseAnd, op_BitwiseOr, op_ExclusiveOr,
op_LeftShift, op_RightShift, op_Equality, op_Inequality, op_LessThan, op_LessThanOrEqual,
op_GreaterThan op_GreaterThanOrEqual.
"~",
;
namespace Acme
{
class Widget: IProcess
{
public static explicit operator int(Widget x) {...}
public static implicit operator long(Widget x) {...}
}
}
"M:Acme.Widget.op_Explicit(Acme.Widget)~System.Int32"
"M:Acme.Widget.op_Implicit(Acme.Widget)~System.Int64"
A.4
A.4.1 C#
Point:
492
Appendix A
namespace Graphics
{
/// <summary> <c>Point</c> .
/// </summary>
public class Point
{
/// <summary> <c>x</c>
/// x .</summary>
private int x;
/// <summary> <c>y</c>
/// y .</summary>
private int y;
/// <value> <c>X</c> x .</value>
public int X
{
get { return x; }
set { x = value; }
}
/// <value> <c>Y</c> y .</value>
public int Y
{
get { return y; }
set { y = value; }
}
/// <summary> Point
/// (0,0).</summary>
public Point() : this(0,0) {}
/// <summary> Point
/// (<paramref name="xor"/>,<paramref name="yor"/>).</summary>
/// <param><c>xor</c> x Point.</param>
/// <param><c>xor</c> y Point.</param>
public Point(int xor, int yor) {
X = xor;
Y = yor;
}
/// <summary>
/// .</summary>
/// <param><c>xor</c> x.</param>
/// <param><c>yor</c> y.</param>
/// <see cref="Translate"/>
public void Move(int xor, int yor) {
X = xor;
Y = yor;
}
493
/// <summary>
/// x y.
/// <example>:
/// <code>
/// Point p = new Point(3,5);
/// p.Translate(-1,3);
/// </code>
/// <c>p</c> (2,8).
/// </example>
/// </summary>
/// <param><c>xor</c> x.</param>
/// <param><c>yor</c> y.</param>
/// <see cref="Move"/>
public void Translate(int xor, int yor) {
X += xor;
Y += yor;
}
/// <summary> , Point
/// .</summary>
/// <param><c>o</c> ,
.
/// </param>
/// <returns>True, Point
/// ; False.</returns>
/// <seealso cref="operator=="/>
/// <seealso cref="operator!="/>
public override bool Equals(object o) {
if (o == null) {
return false;
}
if (this == o) {
return true;
}
if (GetType() == o.GetType()) {
Point p = (Point)o;
return (X == p.X) && (Y == p.Y);
}
return false;
494
Appendix A
/// <summary> , Point
/// .</summary>
/// <param><c>p1</c> Point.</param>
/// <param><c>p2</c> Point.</param>
/// <returns>True, Point
/// ; False.</returns>
/// <seealso cref="Equals"/>
/// <seealso cref="operator!="/>
public static bool operator==(Point p1, Point p2) {
if ((object)p1 == null || (object)p2 == null) {
return false;
}
if (p1.GetType() == p2.GetType()) {
return (p1.X == p2.X) && (p1.Y == p2.Y);
}
}
return false;
}
}
A.4.2 XML
,
Point:
<?xml version="1.0"?>
<doc>
<assembly>
<name>Point</name>
</assembly>
<members>
<member name="T:Graphics.Point">
<summary> <c>Point</c>
.
</summary>
</member>
(Microsoft Corp.), 1999-2007. .
495
<member name="F:Graphics.Point.x">
<summary> <c>x</c>
x .</summary>
</member>
<member name="F:Graphics.Point.y">
<summary> <c>y</c>
y .</summary>
</member>
<member name="M:Graphics.Point.#ctor">
<summary> Point
(0,0).</summary>
</member>
<member name="M:Graphics.Point.#ctor(System.Int32,System.Int32)">
<summary> Point
(<paramref name="xor"/>,<paramref name="yor"/>).</summary>
<param><c>xor</c> x Point.</param>
<param><c>yor</c> y Point.</param>
</member>
<member name="M:Graphics.Point.Move(System.Int32,System.Int32)">
<summary>
.</summary>
<param><c>xor</c> x.</param>
<param><c>yor</c> y.</param>
<see cref="M:Graphics.Point.Translate(System.Int32,System.Int32)"/>
</member>
<member
name="M:Graphics.Point.Translate(System.Int32,System.Int32)">
<summary>
x y.
<example>:
<code>
Point p = new Point(3,5);
p.Translate(-1,3);
</code>
<c>p</c> (2,8).
</example>
</summary>
<param><c>xor</c> x.</param>
<param><c>yor</c> y.</param>
<see cref="M:Graphics.Point.Move(System.Int32,System.Int32)"/>
</member>
<member name="M:Graphics.Point.Equals(System.Object)">
<summary> , Point
.</summary>
<param><c>o</c> ,
.
</param>
<returns>True, Point
; False.</returns>
<seealso
cref="M:Graphics.Point.op_Equality(Graphics.Point,Graphics.Point)"/>
<seealso
cref="M:Graphics.Point.op_Inequality(Graphics.Point,Graphics.Point)"/>
</member>
496
Appendix A
<member name="M:Graphics.Point.ToString">
<summary> .</summary>
<returns>,
(x,y),
, .</returns>
</member>
<member
name="M:Graphics.Point.op_Equality(Graphics.Point,Graphics.Point)">
<summary> , Point
.</summary>
<param><c>p1</c> Point.</param>
<param><c>p2</c> Point.</param>
<returns>True, Point
; False.</returns>
<seealso cref="M:Graphics.Point.Equals(System.Object)"/>
<seealso
cref="M:Graphics.Point.op_Inequality(Graphics.Point,Graphics.Point)"/>
</member>
<member
name="M:Graphics.Point.op_Inequality(Graphics.Point,Graphics.Point)">
<summary> , Point
.</summary>
<param><c>p1</c> Point.</param>
<param><c>p2</c> Point.</param>
<returns>True, Point
; False.</returns>
<seealso cref="M:Graphics.Point.Equals(System.Object)"/>
<seealso
cref="M:Graphics.Point.op_Equality(Graphics.Point,Graphics.Point)"/>
</member>
<member name="M:Graphics.Point.Main">
<summary> Point.
<para> ,
Point.</para></summary>
</member>
<member name="P:Graphics.Point.X">
<value> <c>X</c>
x .</value>
</member>
<member name="P:Graphics.Point.Y">
<value> <c>Y</c>
y .</value>
</member>
</members>
</doc>
497
B.
,
, .
, .
B.1
:
_
_:
__
_ __
__:
_ _
__
_:
_
_ _
_:
B.1.1
_:
(U+000D)
(U+000A)
(U+000D), (U+000A)
(U+0085)
(U+2028)
(U+2029)
B.1.2
:
_
__
_:
// _
_:
_
_ _
_:
__
__:
(U+000D)
(U+000A)
(U+0085)
(U+2028)
(U+2029)
__:
/* ___ /
___:
___
___ ___
___:
/
_____
:
*
*
_____:
(/) (*)
B.1.3
:
Zs
(U+0009)
(U+000B)
(U+000C)
B.1.4
:
_
_
_
_
_
___
B.1.5
__:
\u _ _ _
_
\U _ _ _
_ _ _
_ _
B.1.6
:
_
@ ___
500
Appendix C
_:
___, _
___:
__ __
__:
_ ( , U+005F)
__:
__
__ __
__:
_
__
:
Lu, Ll, Lt, Lm, Lo Nl
__, Lu, Ll, Lt,
Lm, Lo Nl
_:
Mn Mc
__, Mn Mc
_:
Nd
__, Nd
:
Pc
__, Pc
__:
Cf
__, Cf
501
B.1.7
_: :
abstract
byte
class
delegate
event
fixed
if
internal
new
override
readonly
short
struct
try
unsafe
volatile
as
base
bool
break
case
catch
char
checked
const
continue
decimal
default
do
double
else
enum
explicit
extern
false
finally
float
for
foreach
goto
implicit
in
int
interface
is
lock
long
namespace
null
object
operator
out
params
private
protected public
ref
return
sbyte
sealed
sizeof
stackalloc static
string
switch
this
throw
true
typeof
uint
ulong
unchecked
ushort
using
virtual
void
while
B.1.8
:
_
_
_
_
_
_null
_:
true
false
_:
__
__
__:
_ __
_:
_
_ _
_: :
0 1 2 3 4 5 6 7 8 9
__: :
U u L l UL Ul uL ul LU Lu lU lu
__:
0x _ __
0X _ __
_:
_
_ _
502
Appendix C
_: :
0 1 2 3 4 5 6 7 8 9 A B C D E F a b c d e f
_:
_ . _ _
__
. _ _ __
_ _ __
_ __
_:
e _
E _
: :
+ -
__: :
F f D d M m
_:
' '
:
_
__
__
__
_:
(') (U+0027), (\) (U+005C)
__
__: :
\' \" \\ \0 \a \b \f \n \r \t \v
__:
\x _ _
_ _
_:
__
__
__:
" ___ "
___:
___
___ ___
___:
____
__
__
__
____:
(") (U+0022), (\) (U+005C)
__
503
__:
@" ___ "
___:
___
___ ___
___:
____
__
____:
"
__:
""
_null:
null
B.1.9
___: :
{
+
=
->
&=
}
<
==
|=
[
*
>
!=
^=
]
/
?
<=
<<
(
%
??
>=
<<=
)
&
::
+=
=>
.
|
++
-=
,
^
-*=
:
!
&&
/=
;
~
||
%=
_:
>|>
__:
>|>=
B.1.10
_:
_
__
_
_
_
_pragma
_:
___ true false
_:
__
__:
__
__ || __
__:
__
__ && __
504
Appendix C
__:
__
__ ==
__
__ !=
__
__:
__
! __
__:
true
false
_
( _ )
_:
# define _ __
# undef _ __
__:
_ _
__:
__if __elif __else _endif
__if:
# if _ __
__
__elif:
__elif
__elif __elif
__elif:
# elif _ __
__
__else:
# else __
__
_endif:
# endif __
__:
_
_
_:
__
__
__:
_ _
_
_:
__ _
505
__:
_ (#)
_:
# error _
# warning _
_:
_
_ _
_:
__ __ __
__:
# region _
__:
# endregion _
_:
# line _ __
_:
_ _
_
default
hidden
_:
" __ "
__:
__
__ __
__:
_ "
_pragma:
# pragma __pragma
__
__pragma:
___pragma
___pragma:
warning _
warning _ _
_:
disable
restore
_:
_
_ , _
506
Appendix C
B.2
B.2.1
__:
____
_:
____
____:
__
____ . __
__
B.2.2
:
_
_
_
_:
_struct
_
_struct:
_
_
_
_:
_
bool
_:
_
___
decimal
_:
sbyte
byte
short
ushort
int
uint
long
ulong
char
___:
float
double
_:
non-nullable-value-type ?
___:
507
_:
_
_:
_
_
_
_
_:
_
object
string
_:
_
_:
__ _
__:
_:
_
_ _
_:
[ _ ]
_:
,
_ ,
_:
_
__:
< _ >
_:
_
_ , _
_:
_:
B.2.3
_:
B.2.4
_:
_ ,
508
Appendix C
ref _
out _
_:
primary-no-array-creation-expression
array-creation-expression
____:
_
__
___
_
___
__this
___
__
__
__
__
___
_typeof
_checked
_unchecked
___
__
_:
__
__:
( )
___:
_ . __
_ __
__
: :
bool
byte
object sbyte
char
short
decimal
string uint
double float
ulong
int
ushort
long
_:
_ ( _ )
___:
____ [ _ ]
_:
_ ,
__this:
this
___:
base .
base [ _ ]
(Microsoft Corp.), 1999-2007. .
509
__:
_ ++
__:
_ -__:
new ( _ )
___
new ___
___:
_
_
_:
{ __ }
{ __ , }
__:
_
__ , _
_:
= _
_:
___
_:
{ __ }
{ __ , }
__:
_
__ , _
_:
__
{ _ }
__:
new __ [ _ ] _
_
new _ _
new _ _
__:
new _ ( )
___:
new __
__:
{ __ }
{ __ , }
__:
_
__ , _
510
Appendix C
_:
_
___
=
_typeof:
typeof ( )
typeof ( __ )
typeof ( void )
__:
__
:: __
__ .
__
__:
< >
:
,
,
_checked:
checked ( )
_unchecked:
unchecked ( )
___:
default ( )
_:
_
+ _
- _
! _
~ _
__
__
___
__:
++ _
__:
-- _
___:
( ) _
_:
_
_ * _
_ / _
_ % _
_:
_
_ + _
_ _
(Microsoft Corp.), 1999-2007. .
511
_:
_
_ << _
_ _ _
_:
_
_
_
_
_
_
_
< _
> _
<= _
>= _
is
as
_:
_
_ == _
_ != _
_:
_
_ & _
__:
_
__ ^ _
__:
__
__ | __
__:
__
__ && __
__:
__
__ || __
___null:
__
__ ?? ___null
_:
___null
___null ? :
_:
__ => __
__:
delegate ___
__:
___
___
___:
( ____ )
512
Appendix C
____
___
____ , ___
___:
___
___:
ref
out
___:
( ____ )
___
____
___
____ ,
___
___:
__:
_:
_from _
_from:
from in
_:
__ _select__group
_
__:
__
__ __
__:
_from
_let
_where
_join
_join-into
_orderby
_let:
let =
_where:
where _
_join:
join in on equals
_join-into:
join in on equals
into
(Microsoft Corp.), 1999-2007. .
513
_orderby:
orderby
:
,
:
_
_:
ascending
descending
_select__group:
_select
_group
_select:
select
_group:
group by
_:
into _
:
_ _
_:
=
+=
-=
*=
/=
%=
&=
|=
^=
<<=
___
:
__
__:
_
_
_
_:
_:
514
Appendix C
B.2.5
:
_
_
_
_:
_
_
_
_
_
_try
_checked
_unchecked
_lock
_using
_yield
:
{ _ }
_:
_
_:
;
_:
:
_:
__ ;
__ ;
__:
__ __
__:
var
__:
__
__ , __
__:
= __
__:
__:
const _
515
_:
_
_ , _
_:
= _
_:
_ ;
_:
_
__
__
__
__
__
_:
_if
_switch
_if:
if ( _ ) _
if ( _ ) _ else _
_switch:
switch ( ) _switch
_switch:
{ _switch }
_switch:
_switch
_switch _switch
_switch
_switch _
_switch:
_switch
_switch _switch
_switch:
case _ :
default :
_:
_while
_do
_for
_foreach
_while:
while ( _ ) _
_do:
do _ while ( _ ) ;
516
Appendix C
_for:
for ( _for ; _for ; _for )
_
_for:
__
__
_for:
_for:
__
__:
_
__ , _
_foreach:
foreach ( __ in )
_
_jump:
_break
_continue
_goto
_return
_throw
_break:
break ;
_continue:
continue ;
_goto:
goto ;
goto case _ ;
goto default ;
_return:
return ;
_throw:
throw ;
_try:
try _catch
try _finally
try _catch _ finally
_catch:
__catch __catch
__catch __catch
__catch:
__catch
__catch __catch
__catch:
catch ( _ )
(Microsoft Corp.), 1999-2007. .
517
__catch:
catch
_finally:
finally
_checked:
checked
_unchecked:
unchecked
_lock:
lock ( ) _
_using:
using ( _ ) _
_:
__
_yield:
yield return ;
yield break ;
B.2.6
_:
__ _using
_
___
__:
namespace _ __ ;
_:
_ .
__:
{ __ _using
___ }
__:
__
__ __
__:
extern alias ;
_using:
_using
_using _using
_using:
_using__
_using___
_using___:
using = ____ ;
518
Appendix C
_using___:
using __ ;
___:
___
___ ___
___:
__
_
_:
_
_
_
_
_
__:
:: __
B.2.7
_:
_ partial class
__
_ ___
_ ;
_:
_
_ _
_:
new
public
protected
internal
private
abstract
sealed
static
__:
< _ >
_:
_
_ , _
_:
_:
: _
: __
: _ , __
519
__:
_
__ , _
___:
___
___
___
___:
where _ : __
__:
_
_
__
_ , _
_ , __
_ , __
_ , _ , __
_:
_
class
struct
_:
_
_
_ , _
_ , _
__:
new ( )
_:
{ __ }
__:
__
__ __
__:
_
_
_
_
_
_
_
_
_
__
_
_:
_ const
_ ;
520
Appendix C
_:
_
_ _
_:
new
public
protected
internal
private
_:
_
_ , _
_:
= _
_:
_ _ ;
_:
_
_ _
_:
new
public
protected
internal
private
static
readonly
volatile
_:
_
_ , _
_:
= _
_:
_
_:
_ _
_:
_ partial
__ _ __
( __ )
___
_:
_
_ _
521
_:
new
public
protected
internal
private
static
virtual
sealed
override
abstract
extern
__:
void
_:
_ .
_:
__:
_
_ , _
_
_:
_
_ , _
_:
_
_:
ref
out
this
_:
params _
_:
_ _
{ __ }
_:
_
_ _
522
Appendix C
_:
new
public
protected
internal
private
static
virtual
sealed
override
abstract
extern
_:
_ .
__:
___get ___set
___set ___get
___get:
__ get __
___set:
__ set __
__:
protected
internal
private
protected internal
internal protected
__:
_:
_ event
_ ;
_ event _
{ ____ }
_:
_
_ _
523
_:
new
public
protected
internal
private
static
virtual
sealed
override
abstract
extern
____:
___add ___remove
___remove ___add
___add:
add
___remove:
remove
_:
_ _
{ __ }
_:
_
_ _
_:
new
public
protected
internal
private
virtual
sealed
override
abstract
extern
_:
this [ __ ]
_ . this [ __ ]
_:
_ _ _
_:
_
_ _
_:
public
static
extern
524
Appendix C
_:
__
__
__
__:
operator __ ( )
__:
+ - ! ~ ++ -- true false
__:
operator __ ( ,
)
__:
+
*
/
%
&
|
^
<<
_
==
!=
>
<
>=
<=
__:
implicit operator ( )
explicit operator ( )
_:
_:
_ _
_
_:
_
_ _
_:
public
protected
internal
private
extern
_:
( __ )
_
(Microsoft Corp.), 1999-2007. .
525
_:
: base ( _ )
: this ( _ )
_:
__:
__ ( )
__
__:
extern static
static extern
__:
_:
extern ~ ( ) _
_:
B.2.8
_:
_ partial struct
__
_
___ _ ;
_:
_
_ _
_:
new
public
protected
internal
private
_:
: __
_:
{ __ }
__:
__
__ __
526
Appendix C
__:
_
_
_
_
_
_
_
_
__
_
B.2.9
_:
__ _
__:
_:
_
_ _
_:
[ _ ]
_:
,
_ ,
_:
{ __ }
{ __ , }
__:
_
__ , _
_:
_
B.2.10
_:
_ partial interface
__
_ ___
_ ;
_:
_
_ _
527
_:
new
public
protected
internal
private
_:
: __
_:
{ __ }
__:
__
__ __
__:
__
__
__
__
__:
new __
__
( __ )
___ ;
__:
new
{ ___ }
___:
get ;
set ;
get ; set ;
set ; get ;
__:
new event ;
__:
new this [ __ ]
{ ___ }
B.2.11
_:
_ enum
_ _ ;
_:
: _
_:
{ __ }
{ __ , }
528
Appendix C
_:
_
_ _
_:
new
public
protected
internal
private
__:
__
__ __
__:
= _
B.2.12
_:
_ delegate
__ __
( __ )
___ ;
_:
_
_ _
_:
new
public
protected
internal
private
B.2.13
_:
__
__:
__
__ __
__:
[ ___ _ ]
[ ___ _ , ]
___:
__ :
__:
assembly
module
:
_
(Microsoft Corp.), 1999-2007. .
529
_:
_
_ _
_:
[ __ _ ]
[ __ _ , ]
__:
_ :
_:
field
event
method
param
property
return
type
_:
_ ,
:
_ _
_:
_
_:
( ___ )
( ___ , __ )
( __ )
___:
__
___ , __
__:
__
__:
_
__ , _
_:
= __
__:
B.3
_:
...
unsafe
_:
...
unsafe
530
Appendix C
_:
...
unsafe
_:
...
unsafe
_:
...
unsafe
_:
...
unsafe
_:
...
unsafe
_:
...
unsafe
_:
...
unsafe
_:
...
unsafe
_:
...
unsafe
_:
extern unsafe ~ ( )
_
unsafe extern ~ ( )
_
__:
extern unsafe static
unsafe extern static
extern static unsafe
unsafe static extern
static extern unsafe
static unsafe extern
_:
...
_unsafe
_unsafe:
unsafe
:
...
_
(Microsoft Corp.), 1999-2007. .
531
_:
_ *
void *
_:
____:
...
____
____
_sizeof
_:
...
____
____:
* _
____:
_ ->
____:
____ [ ]
_addressof:
& _
_sizeof:
sizeof ( _ )
_:
...
_
_:
fixed ( _ __ )
_
__:
__
__ , __
__:
= __
__:
& __
__:
___
___:
___ fixed
__
___ ;
532
Appendix C
___:
___
___
___
___:
new
public
protected
internal
private
unsafe
__:
___:
___
___
___
___:
[ _ ]
__:
_stackalloc
_stackalloc:
stackalloc _ [ ]
533
C.
. The Unicode Standard, Version 3.0. Addison-Wesley, Reading,
Massachusetts, 2000, ISBN 0-201-616335-5.
IEEE. IEEE .
ANSI/IEEE 754-1985. - http://www.ieee.org.
ISO/IEC. C++. ANSI/ISO/IEC 14882:1998.