Вы находитесь на странице: 1из 784

32.973.2-018.

1
004.43
41

41

., ., ., .
C#. Computers Science. 4- . .: , 2012. 784 .: .
ISBN 978-5-459-00283-6
C#, , C#, Delphi Turbo Pascal,
, C# Microsoft.
,
, , , .
, MSDN, C#.
C# 4.0, , ,
. C#
, .NET. , , , , , , , .
32.973.2-018.1
004.43

Addison-Wesley Longman. .
.
, , ,
. , ,

, .

ISBN 978-0321741769 .
ISBN 978-5-459-00283-6

Addison-Wesley,2011
, 2012
,
, 2012


. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
2. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
3. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
4. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
5. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179
6. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201
7. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236
8. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 393
9. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 441
10. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 454
11. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 589
12. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 607
13. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 614
14. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 642
15. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 648
16. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 657
17. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 663
18. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 683
. . . . . . . . . . . . . . . . . . . . . 713
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 736
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 773


. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
1.1. , . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .30
1.2. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .31
1.3. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .33
1.4. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .39
1.5. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .41
1.6. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .45
1.6.1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .46
1.6.2. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .47
1.6.3. - . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .48
1.6.4. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .49
1.6.5. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .50
1.6.6. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .51
1.6.6.1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .52
1.6.6.2. . . . . . . . . . . . . . . . . . . . . .55
1.6.6.3. . . . . . . . . . . . . . . . . .56
1.6.6.4. , . . . . .57
1.6.6.5. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .60
1.6.7. . . . . . . . . . . . . . . . . . . . . . . .62
1.6.7.1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .64
1.6.7.2. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .64
1.6.7.3. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .67
1.6.7.4. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .68
1.6.7.5. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .69
1.6.7.6. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .70
1.7. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .71
1.8. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .73
1.9. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .75
1.10. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .77
1.11. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .79
1.12. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .80

2. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
2.1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .83
2.2. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .83
2.2.1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .83
2.2.2. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .85
2.2.3. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .85
2.3. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .85
2.3.1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .86
2.3.2. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .86
2.3.3. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .88
2.4. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .88
2.4.1. Unicode . . . . . . . . . . . . . . . . . . . . . .88
2.4.2. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .89
2.4.3. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .92
2.4.4. () . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .93
2.4.4.1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .93
2.4.4.2. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .93
2.4.4.3. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .95
2.4.4.4. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .96
2.4.4.5. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .97
2.4.4.6. null . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .100
2.4.5. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .100
2.5. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .101
2.5.1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .102
2.5.2. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .103
2.5.3. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .103
2.5.4. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .105
2.5.5. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .108
2.5.6. region . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .109
2.5.7. line. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .109
2.5.8. pragma . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .110
2.5.8.1. pragma warning . . . . . . . . . . . . . . . . . . . . . . . . .111
3. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
3.1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .113
3.2. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .114
3.3. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .115
3.4. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .118
3.4.1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .119
3.4.2. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .119
3.4.3. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .120
3.4.4. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .120
3.4.5. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .120
3.4.6. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .120
3.4.7. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .120
3.5. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .121
3.5.1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .121
3.5.2. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .124
3.5.3. protected . . . . . . . . . . . . . . . . . . .127
3.5.4. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .129

3.6. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .130
3.7. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .133
3.7.1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .136
3.7.1.1. . . . . . . . . . . . . . . . . . . . . . . . . . .137
3.7.1.2. . . . . . . . . . . . . . . . . . . . . . .138
3.8. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .140
3.8.1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .143
3.9. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .144
3.10. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .149

4. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
4.1. - . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .151
4.1.1. System.ValueType. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .152
4.1.2. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .153
4.1.3. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .154
4.1.4. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .155
4.1.5. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .156
4.1.6. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .158
4.1.7. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .161
4.1.8. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .162
4.1.9. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .163
4.1.10. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .163
4.2. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .164
4.2.1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .165
4.2.2. object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .166
4.2.3. dynamic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .166
4.2.4. string . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .166
4.2.5. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .166
4.2.6. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .166
4.2.7. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .167
4.3. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .167
4.3.1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .168
4.3.2. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .170
4.4. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .172
4.4.1. - . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .173
4.4.2. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .173
4.4.3. . . . . . . . . . . . . . . . . . . . . . . . . .174
4.4.4. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .174
4.5. - . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .175
4.6. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .176
4.7. dynamic. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .177
5. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179
5.1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .179
5.1.1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .180
5.1.2. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .180
5.1.2.1. . . . . . . . . . . . . . . . . . . . . . . .180
5.1.2.2. . . . . . . . . . . . . . . . . . . . .181
5.1.3. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .181

5.1.4. -. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .181
5.1.5. - . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .182
5.1.6. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .183
5.1.7. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .183
5.2. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .185
5.3. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .186
5.3.1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .187
5.3.2. . . . . . . . . . . . . . . . . . . . . . . . . . . .187
5.3.3. . . . . . . . . . . .188
5.3.3.1. . . . . . . . . . . . . . . . . . . . . . . . . . .188
5.3.3.2. , checked unchecked . . . . . . . . . . . . . . . . . . . .189
5.3.3.3. -. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .189
5.3.3.4. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .189
5.3.3.5. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .190
5.3.3.6. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .190
5.3.3.7. while . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .190
5.3.3.8. do . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .191
5.3.3.9. for . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .191
5.3.3.10. break, continue goto . . . . . . . . . . . . . . .191
5.3.3.11. throw . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .191
5.3.3.12. return . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .192
5.3.3.13. try-catch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .192
5.3.3.14. try-fnally . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .192
5.3.3.15. try-catch-finally. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .193
5.3.3.16. foreach . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .194
5.3.3.17. using . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .194
5.3.3.18. lock . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .194
5.3.3.19. yield . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .194
5.3.3.20. . . . . . . . . . . . . . . . . .195
5.3.3.21.
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .195
5.3.3.22. . . . . . . . .195
5.3.3.23. . . . . . . . . . . . . . . . . . . . . .196
5.3.3.24. &&- . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .196
5.3.3.25. ||. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .197
5.3.3.26. ! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .198
5.3.3.27. ?? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .198
5.3.3.28. ?: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .198
5.3.3.29. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .199
5.4. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .200
5.5. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .200
6. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201
6.1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .202
6.1.1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .202
6.1.2. . . . . . . . . . . . . . . . . . . . .203
6.1.3. . . . . . . . . . . . . . . . . . . . . . . . . . . . .203
6.1.4. . . . . . . . . . . . . . . . . . . . . . . . .204
6.1.5. null . . . . . . . . . . . . . . . . . . . . . . . . . . . . .205

10

6.1.6. . . . . . . . . . . . . . . . . . . . . . . . . .205
6.1.7. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .207
6.1.8. dynamic . . . . . . . . . . . . . . . . . . . . . . . . . . . . .207
6.1.9. . . . . . . . . . . . . . . . . . . .208
6.1.10. - . . . . . . . . . . . . . . . . . . . . . . .209
6.1.11. . . . . . . .210
6.1.12. . . . . . . . . . . . . .210
6.2. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .210
6.2.1. . . . . . . . . . . . . . . . . . . . . . .210
6.2.2. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .213
6.2.3. . . . . . . . . . . . . . . . . . . . . . . . . . .213
6.2.4. . . . . . . . . . . . . . . . . . . . . . . . . . . .214
6.2.5. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .215
6.2.6. dynamic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .216
6.2.7. - . . . . . . . . . . . . . . . . . . . . . . . . . .217
6.2.8. . . . . . . . . . .218
6.3. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .219
6.3.1. . . . . . . . . . . . . . . . . . . . . . . . . .219
6.3.2. . . . . . . . . . . . . . . . . . . . . . . . . . . .219
6.4. , . . . . . . . . . . . . . . . . . . . . .220
6.4.1. , . . . .220
6.4.2. (lifted) . . . . . . . . . . . . . . . . . . . . . . . . .220
6.4.3. . . . . . . . . . .221
6.4.4. . . . . . . . .222
6.4.5. . . . . . . . . . .223
6.5. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .225
6.5.1. . . .227
6.5.2.
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .228
6.5.3. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .228
6.6. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .232
7. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236
7.1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .236
7.1.1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .238
7.2. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .239
7.2.1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .240
7.2.2. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .240
7.2.3. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .242
7.3. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .243
7.3.1. . . . . . . . . . . . . . . . . . . . . . . . .243
7.3.2. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .245
7.3.3. . . . . . . . . . . . . . . . . . . . . .247
7.3.4. . . . . . . . . . . . . . . . . . . . .247
7.3.5. . . . . . . . . . . . . . . . . . .248
7.3.6. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .248
7.3.6.1. . . . . . . . . . . . . . . . . . . . . . . . . . .249
7.3.6.2. . . . . . . . . . . . . . . . . . . . . . . . . .249
7.3.7. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .250

11

7.4. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .251
7.4.1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .254
7.5. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .254
7.5.1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .257
7.5.1.1. . . . . . . . . . . . . . . . . . . . . . . . . . . . .258
7.5.1.2.
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .260
7.5.2. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .262
7.5.2.1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .264
7.5.2.2. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .265
7.5.2.3. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .265
7.5.2.4. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .265
7.5.2.5. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .265
7.5.2.6. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .266
7.5.2.7. . . . . . . . . . . . . . . . . . . . . . . . .266
7.5.2.8. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .266
7.5.2.9. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .267
7.5.2.10. . . . . . . . . . . . . . . . . . . . . . . . . . . .268
7.5.2.11. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .269
7.5.2.12. . . . . . . . . . . . . . . .270
7.5.2.13. . . . . .271
7.5.2.14.
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .272
7.5.3. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .272
7.5.3.1. . . . . . . . . . . . . . . . . . . .273
7.5.3.2. . . . . . . . . . . . . . . . . . . . . . . . .274
7.5.3.3. . . . . . . . . . . . . . . . . . . . . . . .276
7.5.3.4. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .276
7.5.3.5. . . . . . . . . . . . . . . . . . . . . . .276
7.5.3.6. . . . . . . . . . . . . . . . . . . . . . . .277
7.5.4.
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .277
7.5.5. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .278
7.5.5.1. . . . . . . . . . . . . . . . . . . . .280
7.6. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .280
7.6.1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .281
7.6.2. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .281
7.6.2.1. . . . . . . . . . . . . . . . . . . . . . . . . .283
7.6.3. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .285
7.6.4. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .285
7.6.4.1. . . . . . . . . . . . . . . . .288
7.6.4.2. . . . . . . . . . . . . . . . . . . . . . . . .288
7.6.5. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .289
7.6.5.1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .290
7.6.5.2. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .294
7.6.5.3. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .299
7.6.6. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .299
7.6.6.1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .300
7.6.6.2. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .301
7.6.7. this- . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .302

12

7.6.8. Base-. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .303


7.6.9. . . . . . . . . . .304
7.6.10. new . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .306
7.6.10.1. . . . . . . . . . . . . . . . . . . . . . . . . . . .306
7.6.10.2. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .308
7.6.10.3. . . . . . . . . . . . . . . . . . . . . . . . . . . . .311
7.6.10.4. . . . . . . . . . . . . . . . . . . . . . . . . . .313
7.6.10.5. . . . . . . . . . . . . . . . . . . . . . . . . . .315
7.6.10.6. . . . . . . . . . . . . . . .317
7.6.11. typeof . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .319
7.6.12. checked unchecked . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .322
7.6.13. . . . . . . . . . . . . . . . . . . . . . . . . . . .325
7.6.14. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .325
7.7. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .325
7.7.1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .326
7.7.2. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .326
7.7.3. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .327
7.7.4. . . . . . . . . . . . . . . . . . . . . . . . . . . .327
7.7.5. . . . . . . . . . . .328
7.7.6. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .329
7.8. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .331
7.8.1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .331
7.8.2. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .333
7.8.3. . . . . . . . . . . . . . . . . . . . . . . . . . . .334
7.8.4. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .336
7.8.5. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .339
7.9. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .341
7.10. . . . . . . . . . . . . . . . . . . . . .342
7.10.1. . . . . . . . . . . . . . . . . .343
7.10.2. . . . . . . . . . . . .344
7.10.3. . . . . . . . . . . . . . . . . . . . .346
7.10.4. . . . . . . . . . . . . . . . . . . . . . .346
7.10.5. . . . . . . . . . . . . . . . . . . . . . . .346
7.10.6. . . . . . . . . . . . . . . . . . . . . .347
7.10.7. . . . . . . . . . . . . . . . . . . . . . .349
7.10.8. . . . . . . . . . . . . . . . . . . . . . .349
7.10.9. null . . . . . . . . . . . . . . . . . . . . . . . . . .350
7.10.10. is . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .350
7.10.11. as . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .351
7.11. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .353
7.11.1. . . . . . . . . . . . . . . . .353
7.11.2. . . . . . . . . . . . . . . . . . . . . . . .354
7.11.3. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .354
7.11.4. . . . . . . . . . . . . . . . . . .354
7.12. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .355
7.12.1. . . . . . . . . . . . . . . . . . . . . .356
7.12.2. . . .356
7.13. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .357
7.14. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .359
7.15. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .361

13

7.15.1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .363
7.15.2. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .364
7.15.3. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .365
7.15.4. . . . . . . . . . . . . . . .366
7.15.5. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .366
7.15.5.1. . . . . . . . . . . . . . . . . . . . . . .366
7.15.5.2. . . . . . . . . . . . . . . . .367
7.15.6. . . . . . . . . . . . . . . . . . . .370
7.16. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .370
7.16.1. . . . . . . . . . . . . . . . . . . . . . . .372
7.16.2. . . . . . . . . . . . . . . . . . . . . . . . . . .373
7.16.2.1. select GroupBy . . . . . . . . . . .374
7.16.2.2. . . . . . . . . .375
7.16.2.3. . . . . . . . . . . . . . . . . . . . . .376
7.16.2.4. from, let, where, join orderby. . . . . . . . . . . . . . . .376
7.16.2.5. select . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .380
7.16.2.6. GroupBy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .380
7.16.2.7. . . . . . . . . . . . . . . . . . . . . . . . . . . .380
7.16.3. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .382
7.17. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .384
7.17.1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .385
7.17.2. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .388
7.17.3. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .389
7.18. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .390
7.19. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .390
7.20. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .392
8. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 393
8.1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .393
8.2. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .396
8.2.1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .396
8.3. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .397
8.4. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .399
8.5. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .400
8.5.1. . . . . . . . . . . . . . . . . . . . . . . . . . . . .400
8.5.2. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .403
8.6. - . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .404
8.7. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .405
8.7.1. if. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .405
8.7.2. switch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .406
8.8. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .411
8.8.1. while . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .411
8.8.2. do . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .412
8.8.3. for . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .413
8.8.4. foreach . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .414
8.9. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .420
8.9.1. break . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .421
8.9.2. continue . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .422
8.9.3. goto . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .423

14

8.9.4. return. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .425


8.9.5. throw . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .425
8.10. try . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .427
8.11. checked unchecked . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .432
8.12. lock . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .432
8.13. using. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .434
8.14. yield . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .438
9. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 441
9.1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .441
9.2. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .442
9.3. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .444
9.4. using. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .445
9.4.1. Using- . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .445
9.4.2. Using- . . . . . . . . . . . . . . . . . . . . . . . . . . .448
9.5. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .450
9.6. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .450
9.7. . . . . . . . . . . . . . . . . . . . . . . .451
9.7.1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .453
10. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 454
10.1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .454
10.1.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .454
10.1.1.1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .455
10.1.1.2. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .456
10.1.1.3. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .457
10.1.2. partial. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .458
10.1.3. - . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .458
10.1.4. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .459
10.1.4.1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .459
10.1.4.2. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .462
10.1.5. - . . . . . . . . . . . . . . . . . . . . . . . . . . . . .462
10.1.6. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .468
10.2. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .468
10.2.1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .469
10.2.2. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .470
10.2.3. - . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .470
10.2.4. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .471
10.2.5. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .471
10.2.6. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .472
10.2.7. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .473
10.2.8. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .477
10.3. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .478
10.3.1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .479
10.3.2. . . . . . . . . . . . . . . . . . . . . . . . . . .480
10.3.3. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .482
10.3.4. new . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .483
10.3.5. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .484
10.3.6. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .484

15

10.3.7. . . . . . . . . . . . . . . . . .484
10.3.8. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .486
10.3.8.1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .486
10.3.8.2. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .486
10.3.8.3. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .487
10.3.8.4. this- . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .488
10.3.8.5.
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .489
10.3.8.6. . . . . . . . . . . . . . . . . . .490
10.3.9. . . . . . . . . . . . . . . . . . . . . . . . . .491
10.3.9.1. , . . . . . . .492
10.3.9.2. , . . . . . . .493
10.3.9.3. , . .493
10.3.9.4. , . .493
10.4. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .493
10.5. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .496
10.5.1. . . . . . . . . . . . . . . . . . . . . . . . . . .497
10.5.2. , . . . . . . . . . . . . . . . . . . . . . . . . . . .498
10.5.2.1. ,
, . . . . . . . . . . . . . . . . . . . . . . . . . . . .499
10.5.2.2. ,
. . . . . . . . . . . . . . . . . . . . . . . . . . .500
10.5.3. - . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .501
10.5.4. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .503
10.5.5. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .503
10.5.5.1. . . . . . . . . . . . . . . . . . . . . . .504
10.5.5.2. . . . . . . . . . . . . . . . . . . . . . .506
10.6. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .506
10.6.1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .509
10.6.1.1. - . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .511
10.6.1.2. -. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .511
10.6.1.3. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .512
10.6.1.4. - . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .514
10.6.2. . . . . . . . . . . . . . . . . . . . . .517
10.6.3. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .517
10.6.4. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .520
10.6.5. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .523
10.6.6. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .524
10.6.7. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .526
10.6.8. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .527
10.6.9. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .527
10.6.10. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .529
10.6.11. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .530
10.7. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .530
10.7.1. . . . . . . . . . . . . . . . . . .531
10.7.2. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .532
10.7.3. . . . . . . . . . . . . . . . . . . . . . . . .538
10.7.4. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .540
10.7.5. , ,
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .541

16

10.8. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .544
10.8.1. , . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .546
10.8.2. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .548
10.8.3. . . . . . . . . . . . . . . . . . . .549
10.8.4. , ,
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .550
10.9. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .551
10.9.1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .555
10.10. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .555
10.10.1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .557
10.10.2. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .558
10.10.3. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .559
10.11. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .563
10.11.1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .564
10.11.2. . . . . . . . . . . . . . . . . . . . .565
10.11.3. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .565
10.11.4. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .568
10.11.5. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .568
10.11.6.
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .569
10.12. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .570
10.13. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .573
10.14. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .575
10.14.1. - . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .575
10.14.2. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .576
10.14.3. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .576
10.14.4. -. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .576
10.14.4.1. MoveNext . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .577
10.14.4.2. Current. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .579
10.14.4.3. Dispose . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .579
10.14.5. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .579
10.14.5.1. GetEnumerator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .580
10.14.6. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .580
11. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 589
11.1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .590
11.1.1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .590
11.1.2. partial. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .591
11.1.3. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .591
11.1.4. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .591
11.2. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .591
11.3. . . . . . . . . . . . . . . . . . . . . . . . . . . . .592
11.3.1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .592
11.3.2. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .594
11.3.3. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .594
11.3.4. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .595
11.3.5. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .595
11.3.6. this . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .598
11.3.7. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .598

17

11.3.8. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .599
11.3.9. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .600
11.3.10. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .600
11.4. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .601
11.4.1. . . . . . . . . . . . . . . . . . . . . . . . . . .601
11.4.2. boolean . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .604
12. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 607
12.1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .607
12.1.1. System.Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .608
12.1.2. IList . . . . . . . . . . . . . . . . . . . . . . .608
12.2. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .610
12.3. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .610
12.4. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .610
12.5. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .610
12.6. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .612
13. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 614
13.1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .614
13.1.1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .615
13.1.2. partial. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .615
13.1.3. . . . . . . . . . . . . . . . . . . . . . . . . .616
13.1.3.1. . . . . . . . . . . . . . . . . . . . . . . . . . . . .616
13.1.3.2. . . . . . . . . . . . . . . . . . . . . . . . . .617
13.1.4. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .618
13.1.5. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .619
13.2. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .620
13.2.1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .621
13.2.2. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .622
13.2.3. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .622
13.2.4. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .623
13.2.5. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .623
13.3. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .625
13.4. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .626
13.4.1. . . . . . . . . . . . . . . . . . . . . .628
13.4.2. . . . . . . . . . . . . . . . . . . . .631
13.4.3. . . . . . . . . . . . . . . . . . . . . . . . . . . . .632
13.4.4. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .633
13.4.5. . . . . . . . . . . . . . . . . . . . . . . .637
13.4.6. . . . . . . . . . . . . . . . . . . . . . . . . . . .638
13.4.7. . . . . . . . . . . . . . . . . . . . . . . . . . . . .640
14. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 642
14.1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .642
14.2. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .643
14.3. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .644
14.4. System.Enum . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .646
14.5. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .647

18

15. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 648
15.1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .649
15.2. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .652
15.3. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .652
15.4. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .653
16. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 657
16.1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .658
16.2. System.Exception . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .659
16.3. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .659
16.4. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .661
17. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 663
17.1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .664
17.1.1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .664
17.1.2. . . . . . . . . . . . . . . . . . . . . . .666
17.1.3. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .667
17.2. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .667
17.3. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .674
17.3.1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .674
17.3.2. . . . . . . . . . .674
17.4. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .675
17.4.1. AttributeUsage. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .675
17.4.2. Conditional. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .676
17.4.2.1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .676
17.4.2.2. . . . . . . . . . . . . . . . . . . . . . . . . . . . .679
17.4.3. Obsolete. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .680
17.5. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .682
17.5.1. COM Win32 . . . . . . . . . . . . . . . . .682
17.5.2. .NET. . . . . . . . . . . . . . . . . . . . . .682
17.5.2.1. IndexerName . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .682
18. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 683
18.1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .684
18.2. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .686
18.3. . . . . . . . . . . . . . . . . . . . . . .690
18.4. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .690
18.4.1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .692
18.5. . . . . . . . . . . . . . . . . . . . . . . . . . . . .693
18.5.1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .694
18.5.2. . . . . . . . . . . . . . . . . . . . .694
18.5.3. . . . . . . . . . . . . . . . . . . . . . . . . . . . .696
18.5.4. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .696
18.5.5. . . . . . . . . . . . . . . . . . . . . . . . . . . .698
18.5.6. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .698
18.5.7. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .699
18.5.8. sizeof . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .699
18.6. fixed . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .700

19

18.7. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .705
18.7.1. . . . . . . . . . . . . . . . . .706
18.7.2.
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .707
18.7.3. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .708
18.8. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .709
18.9. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .710
. . . . . . . . . . . . . . . . . . . . . 713
.1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .713
.2. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .715
.2.1. <c> . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .715
A.2.2. <code> . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .716
A.2.3. <example> . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .716
A.2.4. <exception> . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .716
A.2.5. <include> . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .717
A.2.6. <list> . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .718
A.2.7. <para> . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .719
A.2.8. <param> . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .719
A.2.9. <paramref>. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .720
A.2.10. <permission> . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .720
A.2.11. <remark> . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .721
A.2.12. <returns> . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .721
A.2.13. <see> . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .721
A.2.14. <seealso>. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .722
A.2.15. <summary> . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .722
A.2.16. <value>. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .723
A.2.17. <typeparam> . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .723
A.2.18. <typeparamref> . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .723
A.3. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .724
A.3.1. ID-. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .724
A.3.2. ID- . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .725
A.4. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .729
A.4.1. C#. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .729
A.4.2. XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .732
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 736
B.1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .736
.1.1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .736
.1.2. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .736
.1.3. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .737
.1.4. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .737
.1.5. Unicode . . . . . . . . . . . . . . . . . . . . .738
.1.6. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .738
.1.7. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .739
.1.8. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .739
.1.9. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .741
.1.10. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .741
.2. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .744
.2.1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .744

20

.2.2. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .744
.2.3. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .746
.2.4. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .746
.2.5. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .753
.2.6. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .757
.2.7. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .758
.2.8. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .765
.2.9. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .766
.2.10. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .766
.2.11. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .767
.2.12. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .768
.2.13. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .768
.3. . . . . . . . . . . . . . . . . . . . . .770
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 773


.NET 2000 .
.NET
XML-.
, , C#.
.NET C# ,
.NET
. .NET
, : , ,
System.ValueType. ? ,
API ,
, , .
. C# ,
.NET. , ,
.NET , (
), ,
, C#, . , C# .NET,
C# 3.0, , -,

. C# 4.0, ,
, (Common Language Runtime, CLR).
C# , , .
, C# 3.0, ,
. , , ,
.

. C# 3.0.
. Java:
Lisp; C++.
Lisp. C# 3.0, , C#
(). C# 4.0
Lisp ( JavaScript, Python, Ruby . .),

22

,
.
C#, , (BNF),
, C#. ,
, . ,
, .NET,
, - , .
!

, , 2010


C# 12 , 1998 , , -
.NET,
. C# .
.
, .
, .
C#. , .
, , C# 4.0,
, ,
. C# ,
.NET. LINQ , ,
, C# 4.0
C#
, Python, Ruby JavaScript.

C#. ,
,
, . ,
C#.
C# .
C# 1.0 (Anders Hejlsberg), (Scott
Wiltamuth), (Peter Golde), (Peter Golde)
(Eric Gunnerson). C# 2.0: ,
, (Peter Hallam), (Shon Katzenberger),
(Todd Proebsting) (Anson Horton).
, C#
.NET Gyro, (Don Syme)
(Andrew Kennedy) Microsoft Research.
C# 3.0: , (Erik Meijer), (Matt
Warren), (Mads Torgersen),
(Dinesh Kulkarni). C# 4.0 , , , (Eric Lippert),
( Jim Hugunin), (Lucian Wischik) (Neal Gafter).

24

,
C#, .
; ,
, .
C# , . ,
C#, , .




, , 2010


. C#
Microsoft Technical Fellow1. Microsoft 1996
Borland,
Delphi Turbo Pascal.
C# Microsoft;
.
Visual Studio Team Professional
Microsoft. ,
OLE Automation, Visual Basic, Visual Basic for Applications, VBScript,
JScript, Visual J++ Visual C#.
C# Microsoft. Microsoft (European Computer Manufactures Association, ECMA),
C#,
.
Microsoft.

Microsoft, ,
. . . .


(Brad Abrams)
(CRL), , .NET Framework Microsoft,
WCF WF.
.NET Framework 1998 , , (Base Class Library, BCL) .NET.
1997 . :
Framework Design Guidelines, Second Edition (Addison-Wesley, 2009) .NET Framework
Standard Library Annotated Reference (Volumes 1 and 2) (Addison-Wesley, 2006).
( Joseph Albahari) C# 4.0 in
a Nutshell (OReilly, 2007), C# 3.0 Pocket Reference(OReilly, 2008) LINQ Pocket
Reference (OReilly, 2008). , . LINQPad,
LINQ.
(Krzysztof Cwalina) .NET Framework Microsoft.
(Application Program Interface, API)
. , .NET Framework.
FrameworkDesign Guidelines (Addison-Wesley, 2005).
http://blogs.msdn.com/kcwalina.
(Jesse Liberty) ( Silverlight Geek)
Microsoft,
.
. http://JesseLiberty.com
WPF, Windows Phone 7 Silverlight. , - Citi
AT&T.
@JesseLiberty.
(Eric Lippert) ,
C# Microsoft.
, Visual Basic, VBScript, JScript, C#, Visual Studio Tools For Office.
http://blogs.
msdn.com/EricLippert.
(Christian Nagel)
Microsoft MVP. , Professional C# 4 with
.NET 4 (Wrox, 2010) Enterprise Services with the.NET Framework (Addison-Wes-

27

ley, 2005). CN innovation thinktecture, Microsoft .NET.


http://www.cninnovation.com.
(Vladimir Reshetnikov) Microsoft MVP Visual C#.

Microsoft .NET C#.
http://nikov-thoughts.blogspot.com.
(Marek Safar) Novell C#. Mono C#. http://mareksafar.blogspot.com.
(Chris Sells)
Business Platform ( SQL Server) Microsoft.
, Programming WPF (OReilly, 2007), Windows Forms 2.0
Programming (Addison-Wesley, 2006) ATL Internals (Addison-Wesley, 1999).
Microsoft.

http://www.sellsbrothers.com.
(Peter Sestoft) , , .
C#
(Europian Computer Manufacturers Association, ECMA) c 2003 2006 . C# Precisely
(MIT Press, 2004) Java Precisely (MIT Press, 2005).
http://www.itu.dk/people/sestoft.
( Jon Skeet) C# in Depth (Manning, 2010) C# MVP.
Google,
C#. http://msmvps.com/jon.skeet Stack Overfow (http://stackoverfow.com).
(Bill Wagner) SRT Solutions,
Microsoft C# MVP.
. Effective C# (Addison-Wesley, 2005) More Effective C# (Addison-Wesley, 2009). , C#, Visual Studio Magazine
C# (C# Developer Center) Microsoft Developer Network
(MSDN). C#
http://srtsolutions.com/blogs/billwagner.


, ,
comp@piter.com ( , ).
!
- http://www.piter.com .

C# ( ) , , -,
. C#
C C, C++
Java.
(European Computer Manufacturers Association,
ECMA) ECMA-334,
(International Standards Organization, ISO)
(International Electrotechnical Commission, IEC)
ISO/IEC 23270. Microsoft C# .NET Framework .
- , C#
- . , . ,
, ,
, , , . C#
, ,
.
C#, :
, ;
; ,
,
,
.
C# . , C#,
, int double, object.
, ; , . ,
C# -,
.
, C# ,

29

C#.
. , ,
,
. C#, ,
virtual override, .
C#.
,
, .
, .

, C# , - , . , C#
2.0, LINQ 3.0 C# 4.0, ,
,
.


C# . , , -
, -
, .


C# - : , ,
. , C# 3.0
,
, , C# 1.0.


C# ,
C# 3.0 , ,
(mutability). ,
(immutability) ,
, (-) .

30

1.


C#. C# .
,
. C# ,
.

1.1. ,
,
. C# :
using System;
class Hello
{
static void Main() {
Console.WriteLine(", !");
}
}

C# .cs. , hello.cs,
Microsoft C# :
csc hello.cs

hello.exe.
, !

, using, System.
C#.
, System
, Console, , , IO Collections. using, , ,
, .
using Console.
WriteLine System.Console.WriteLine.
Hello, , , Main. Main
static. ,
this, . Main
.
WriteLine
Console System.
.NET Framework,

1.2.

31

Microsoft C#. , C# ,
.NET Framework , C# .

, Console.WriteLine() Console.
Out.WriteLine. Console.Out ,
System.IO.TextWriter,
.
:
using System;
class Hello
{
static void Main() {
Console.Out.WriteLine(Hello, World);
}
}

,
C# .
Console, , . ,
. Console.Out.WriteLine().

1.2.
C# , ,
, . C#
. ,
. . , , .
C# .
.exe .dll, ,
.
:
using System;
namespace Acme.Collections
{
public class Stack
{
Entry top;
public void Push(object data) {
top = new Entry(top, data);
}
public object Pop() {

32

1.
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- ( Just-In-Time1) (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);
1

, ,
. . .

1.3.

33

Console.WriteLine(s.Pop());
Console.WriteLine(s.Pop());
Console.WriteLine(s.Pop());
}
}

test.cs, acme.
dll /r:
csc /r:acme.dll test.cs

test.exe,
100
10
1

C#
. C#, ,
,
. C# ,
, , . C# ,

, .

Java. , C# , C# C++.


, using Acme.Collections,
#include C, .
, Stack, , .
, :
Acme.Collections.Stack s = new Acme.Collections.Stack();

1.3.
C# : -
. - ,
() .
, , , ,
,
. -
, ( ref out).

34

1.


, (reference) . (
)
.
- , ,
C#: ,
.


, -
, , . -,
, . -, . -, : , ,
, ,
-, , , -
. -, -
, , CLR,
.
, - ,
;
,
.


C#,
. ,
, ( C++).

.


C# ,
. ,
,
.

- C# , ,
. , , . C# .

1.3.

35

: sbyte, short, int, long


: byte, ushort, uint, ulong
Unicode: char
IEEE: oat, double
: decimal
: bool


enum E {...}


struct S {...}

-
null

object
Unicode: string


class C {...}
interface I {...}

;
int[] int[,]

, delegate
int D(...)

8-, 16-, 32-


64- .1

Byte , ! , Java byte ( ), ,
.
, uint, : , int,
. , , :
String.Length?


, 1 ,
CLS. CLS , CLS- :
CLS .

.NET Framework. . .

36

1.

, oat double, 32- 64-


IEEE 754.
decimal 128- .

, decimal
. ,
oat double .

bool C#
true false.
Unicode. char
UTF-16, string UTF-16.
C#.

8
16
32
64

sbyte
short
int
long

8
16
32
64
32
64

byte
ushort
uint
ulong
oat
double

128

decimal

/
128127
32 72832 767
2 147 483 6482 147 483 647
9 223 372 036 854 775 808
9 223 372 036854 775 807
0255
065 535
04 294 967 295
018 446 744 073 709 551 615
1.51045 3.41038, 7
5.010324 7.910308, 15
1.01028 7.91028, 28


C++ ,
, short, int long. short <= int <= long, 16-,
32- 64- . C# , , .

C# .
. C#
: , , ,
.

1.3.

37

, () (, .). , ,
, .


, , .

,
- -. , ,
- . , , object.

object . System.
ValueType, , , object.


(public) . , , , .
,
.
.
,
. ,
, , ,
- .
: , , .
, . ,
.
.

-.
,
. , C#
.

38

1.

C# .
, , .
. , int[] , int, int[,]
, int, int[][] ,
int.
. T
T?, null. , int?
, 32-
null.

T? Nullable<T>.


C# 1.0 -. C# 2.0
-.
. , , , . ,
,
. ; .
(code contracts) ,
;
, . ,
Spec#, Microsoft C#,
.

C# , . C#
object, object .

object. -
.
int object int.
using System;
class Test
{
static void Main() {
int i = 123;
object o = i;
int j = (int)o;
}
}

//
//

1.4.

39

- object, , (box), . , object -,


, , , .
C# , - . ,
object, ,
-.
C# : , ,
. ,
, ,
, .

-
-
object




,


,
,
, ,
, -,

1.4.
. , .
: +, -, *, / new. : , , .
, , , . ,
x + y * z x + (y * z), *
+.
.
,
.

40

1.


, , , . , . x, y, z, , . x ,
y, x y; , , .

C#. .
.

x.m
x()
x []
x++
x-new T(...)
new T(...){...}
new {...}
new T[...]
typeof(T)
checked(x)










System.Type T

T
( )

unchecked(x)
default(T)
delegate {...}

+x
-x
!x
~x
++x
--x
(T)x
x * y
x / y
x % y
x + y
x y
x << y
x >> y


()


x T

, ,

1.5.

41

x
x
x
x
x



true, x T,
false
T
null, x T


AND,
AND
XOR,
XOR
OR,
OR
y x true
y x false
y, x null, x
y x true, z x
false

;
: *= /= %= += -= <<= >>= &=
^= |=
(-)

< y
> y
<= y
>= y
is T

x as T

AND

x == y
x != y
x & y

XOR

x ^ y

OR

x | y

AND
OR

x && y
x || y
x ?? y

x ? y : z

x = y
x op= y

(T x) => y


, -
. .
, . , ,
, .

1.5.
. C# ,
.
, . , { }.

42

1.


.
- . ,
: ,
new, ()
, =, , ++ --.

. if switch.
. while, do, for
foreach.
.
break, continue, goto, throw, return yield.
try...catch , , try...nally
, ,
, .

; , nally . try
, fail fast (
), nally .

checked unchecked
.
lock ,
, , .
using , ,
.
C# .

static void Main() {


int a;
int b = 2, c = 3;
a = 1;
Console.WriteLine(a + b + c);
}

1.5.

if

switch

while

do

for

43

static void Main() {


const oat pi = 3.1415927f;
const int r = 25;
Console.WriteLine(pi * r * r);
}
static void Main() {
int i;
i = 123;
// -
Console.WriteLine(i); // -
i++;
// -
Console.WriteLine(i); // -
}
static void Main(string[] args) {
if (args.Length == 0) {
Console.WriteLine(" ");
}
else {
Console.WriteLine(" ");
}
}
static void Main(string[] args) {
int n = args.Length;
switch (n) {
case 0:
Console.WriteLine(" ");
break;
case 1:
Console.WriteLine(" ");
break;
default:
Console.WriteLine("{0} ", n);
break;
}
}
}
static void Main(string[] args) {
int i = 0;
while (i < args.Length) {
Console.WriteLine(args[i]);
i++;
}
}
static void Main() {
string s;
do {
s = Console.ReadLine();
if (s != null) Console.WriteLine(s);
} while (s != null);
}
static void Main(string[] args) {
for (int i = 0; i < args.Length; i++) {
Console.WriteLine(args[i]);
}
}

44

1.

foreach

break

continue

return

yield

throw
try

static void Main(string[] args) {


foreach (string s in args) {
Console.WriteLine(s);
}
}
static void Main() {
while (true) {
string s = Console.ReadLine();
if (s == null) break;
Console.WriteLine(s);
}
}
static void Main(string[] args) {
for (int i = 0; i < args.Length; i++) {
if (args[i].StartsWith("/")) continue;
Console.WriteLine(args[i]);
}
}
static int Add(int a, int b) {
return a + b;
static void Main() {
Console.WriteLine(Add(1, 2));
return;
}
static IEnumerable<int> Range(int from, int to) {
for (int i = from; i < to; i++) {
yield return i;
}
yield break;
}
static void Main() {
foreach (int x in Range(-10,10)) {
Console.WriteLine(x);
}
}
static double Divide(double x, double y) {
if (y == 0) throw new DivideByZeroException();
return x / y;
}
static void Main(string[] args) {
try {
if (args.Length != 2) {
throw new Exception(" 2 ");
}
double x = double.Parse(args[0]);
double y = double.Parse(args[1]);
Console.WriteLine(Divide(x, y));
}
catch (Exception e) {
Console.WriteLine(e.Message);
}

1.6.

checked
unchecked

lock

using

45

nally {
Console.WriteLine(" !");
}
}
static void Main() {
int i = int.MaxValue;
checked {
Console.WriteLine(i + 1); //
}
unchecked {
Console.WriteLine(i + 1); //
}
}
class Account
{
decimal balance;
public void Withdraw(decimal amount) {
lock (this) {
if (amount > balance) {
throw new Exception(" ");
}
balance -= amount;
}
}
}
static void Main() {
using (TextWriter w = File.CreateText("test.txt")) {
w.WriteLine(" ");
w.WriteLine(" ");
w.WriteLine(" ");
}
}

1.6.
C#. , () (
).
, . , ,
.
.
, ,
, ( ) , .
, , { }.

46

1.

Point:
public class Point
{
public int x, y;
public Point(int x, int y)
{
this.x = x;
this.y = y;
}
}

new, ,
.
Point :
Point p1 = new Point(0, 0);
Point p2 = new Point(10, 20);

, , , . -
C# , .

1.6.1.
, .
,
( ).

- , ,
,
.


, ( , Visual Basic)
. , , . ,
, -------- ( ).

,

,

1.6.

47

,
,
,
,
,

, ,

,

1.6.2.
, ,
. .

public
protected
internal
protected internal
private


public. C# ,
public C++! C++ . C# , extern C++ (
). !


internal ,
.
DLL, EXE-, DLL, .


protected internal .
, , protected
internal , .
, ,

48

1.

. ,
, private
.

C#
protected internal, , . proternal
intected, , .

C# protected internal
, . CLR
, . C++/CLI CLR public private ( private
public ).
.

1.6.3. -
-. ,
-. -
. -
Pair TFirst TSecond.
public class Pair<TFirst, TSecond>
{
public TFirst First;
public TSecond Second;
}

, -,
(, ) . ,
.

, -, , CLR.

,
-:
Pair<int,string> pair = new Pair<int,string> { First = 1, Second = "two" };
int i = pair.First;
// TFirst -
string s = pair.Second; // TSecond string

1.6.

49

,
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;
public Point3D(int x, int y, int z): base(x, y)
{
this.z = z;
}
}

. , , , .
, ,
. Point3D
Point x y, Point3D x, y z.

C#, . - . , ,
, C#.


. ,
. ,
, , Point
Point Point3D:
Point a = new Point(10, 20);
Point b = new Point3D(10, 20, 30);

50

1.

1.6.5.
, .
, static,
. . , , .

.

class Stack<T> {
public readonly static Stack<T> empty = whatever; ...
}

Stack<int>.empty , Stack<string>.empty.

, 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

Black = new Color(0, 0, 0);


White = new Color(255, 255, 255);
Red = new Color(255, 0, 0);
Green = new Color(0, 255, 0);
Blue = new Color(0, 0, 255);

private byte r, g, b;
public Color(byte r, byte g, byte b)
{
this.r = r;
this.g = g;
this.b = b;
}

, readonly. readonly .

readonly , , . ,
:
public class Names
{
public static readonly StringBuilder FirstBorn = new StringBuilder("Joe");

1.6.

51

public static readonly StringBuilder SecondBorn = new StringBuilder("Sue");


}

FirstBorn ( ) :
Names.FirstBorn = new StringBuilder("Biff");

//

, StringBuilder:
Names.FirstBorn.Remove(0,6).Append("Biff");
Console.WriteLine(Names.FirstBorn);

// "Biff"

, .
, int, double String.



. Adapter, Decorator,
Fasade Proxy. , ,
,
. , .

1.6.6.
, ,
.
. .
( ),
, , , . ,
void.
, -,
.
.
, . , -,
, .
.

, .


52

1.

, class C<T> { void M(T t){} void M(int t){} ...}


, C<int> M .
, . :
, ;
.

1.6.6.1.
,
. ,
. : -,
-, -.
- . ,
, . -
, .

, - ,
,
. - ,
, .

- ,
.
- ,
. , -, , -
, . ref. ref.
using System;
class Test
{
static void Swap(ref int x, ref int y) {
int temp = x;
x = y;
y = temp;
}
static void Main() {
int i = 1, j = 2;
Swap(ref i, ref j);
Console.WriteLine("{0} {1}", i, j); // "2 1"
}
}

1.6.

53


, , . C#,
; ,
.
.

. , .
- ( ), . , , .
,
,
. x i .


. -, ,
, , .
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"
}
}


CLR ref. out ref, ,
C#, ref
out. , ,
out ref:
.

- .
params.
-, .
- Write WriteLine
System.Console. :

54

1.

public class Console


{
public static void Write(string fmt, params object[] args) {...}
public static void WriteLine(string fmt, params object[] args) {...}
...
}

, -,
.
-
-,
-. . :
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);


params varargs C-.
C# ,
params
. , varargs.
, , C# -. ,
, , , params
.
StringBuilder.AppendFormat():
public
public
public
object
public

StringBuilder
StringBuilder
StringBuilder
arg2);
StringBuilder

AppendFormat(string format, object arg0);


AppendFormat(string format, object arg0, object arg1);
AppendFormat(string format, object arg0, object arg1,
AppendFormat(string format, params object[] args);


, params , ,
:
static object[] GetArgs() { ... }
static void Main() {
object[] args = GetArgs();
object x = args[0];
object y = args[1];

1.6.

55

object z = args[2];
Console.WriteLine("x={0} y={1} z={2}", x, y, z);
}

, ,
. , :
static object[] GetArgs() { ... }
static void Main() {
Console.WriteLine("x={0} y={1} z={2}", GetArgs());
}

.NET ,
,
IEnumerable<T> - . ,
:
static IEnumerable<object> GetArgs() { ... }
static void Main() {
Console.WriteLine("x={0} y={1} z={2}", GetArgs().ToArray());
}

, params IEnumerable .
, .

1.6.6.2.
, .
,
. . , , ,
. 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,
i .

56

1.

return, . , void, return . , - ,


return , .

1.6.6.3.
, static, .
.

, .

, static, .

, . ,
, this.
this .
Entity, ,
.
class Entity
{
static int nextSerialNo;
int serialNo;
public Entity()
{
serialNo = nextSerialNo++;
}
public int GetSerialNo()
{
return serialNo;
}
public static int GetNextSerialNo()
{
return nextSerialNo;
}
public static void SetNextSerialNo(int value)
{
nextSerialNo = value;
}
}

Entity (, ,
, ). Entity ( )
. , serialNo nextSerialNo.

1.6.

57

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());
// "1000"
Console.WriteLine(e2.GetSerialNo());
// "1001"
Console.WriteLine(Entity.GetNextSerialNo()); // "1002"
}
}

, SetNextSerialNo GetNextSerialNo , GetSerialNo


.

1.6.6.4. ,

, virtual, . virtual ,
.
, ,
,
. .
.
override, .
,
,
.

,
, , ,
.
;
, .
, C#
. ,
, - .

58

1.

. 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);
}
}
public class Operation: Expression
{
Expression left;
char op;
Expression right;
public Operation(Expression left, char op, Expression right) {
this.left = left;
this.op = op;
this.right = right;
}

1.6.

59

public override double Evaluate(Hashtable vars) {


double x = left.Evaluate(vars);
double y = right.Evaluate(vars);
switch (op) {
case +: return x + y;
case -: return x - y;
case *: return x * y;
case /: return x / y;
}
throw new Exception(" ");
}
}

. , ,
x + 3 :
Expression e = new Operation(
new VariableReference("x"),
+,
new Constant(3));

Expression ,
double.
Hashtable, ( ) ( ). Evaluate ; ,
,
.
Constant .
VariableReference -
. Operation ,
.
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)
)
);
Hashtable vars = new Hashtable();
vars["x"] = 3;
vars["y"] = 5;
Console.WriteLine(e.Evaluate(vars)); // "21"

60

1.
vars["x"] = 1.5;
vars["y"] = 9;
Console.WriteLine(e.Evaluate(vars)); // "16.5"
}


- , .
, , :
double GetHourlyRate(Person p) {
if( p is Student ) { return 1.0; }
else if( p is Employee ) { return 10.0; }
return 0.0;
}

:
class Person {
public virtual double GetHourlyRate() {
return 0.0;
}
}
class Student {
public override double GetHourlyRate() {
return 1.0;
}
}
class Employee {
public override double GetHourlyRate() {
return 10.0;
}
}

1.6.6.5.

, .
,
, . , , , .
.
Main , .
class Test
{
static void F() {
Console.WriteLine("F()");
}
static void F(object x) {
Console.WriteLine("F(object)");

1.6.

61

}
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)");
}
static void Main() {
F();
// F()
F(1);
// F(int)
F(1.0);
// F(double)
F("abc");
// F(object)
F((double)1); // F(double)
F((object)1); // F(object)
F<int>(1);
// F<T>(T)
F(1, 1);
// F(double, double)
}
}

, ()
.

. ,
.
, . , .
. , , ,
. , .NET Framework ( ) string:
public class String {
public int IndexOf (string value);
// value
public int IndexOf (char value);
// value
public int IndexOf (char [] value);
// value
//
}

, .


62

1.

,
"Joshua, Hannah, Joseph".IndexOf("Hannah");// 7,

"Joshua, Hannah, Joseph".IndexOf(new char [] {H,a,n,n,a,h;");


// 3

, - , :
public class String {
public int IndexOf (string value);
// value
public int IndexOf (char value);
// value
public int IndexOfAny(char [] value);
// value
//
}


. , , ,
, , ,
. ,
.

1.6.7.
, , . ,
. C#: ,
, , , .
List<T>,
.
.
public class List<T>
{
const int defaultCapacity = 4;
T[] items;
int count;
public List(int capacity = defaultCapacity) {
items = new T[capacity];
}
public int Count {
get { return count; }
}

1.6.

public int Capacity {


get {
return items.Length;
}
set {
if (value < count) value = count;
if (value != items.Length) {
T[] newItems = new T[value];
Array.Copy(items, 0, newItems, 0, count);
items = newItems;
}
}
}

public T this[int index] {


get {
return items[index];
}
set {
items[index] = value;
OnChanged();
}
}

public void Add(T item) {


if (count == Capacity) Capacity = count * 2;
items[count] = item;
count++;
OnChanged();
}
protected virtual void OnChanged() {
if (Changed != null) Changed(this, EventArgs.Empty);
}
public override bool Equals(object other) {
return Equals(this, other as List<T>);
}
static bool Equals(List<T> a, List<T> b) {
if (a == null) return b == null;
if (b == null || a.count != b.count) return false;
for (int i = 0; i < a.count; i++) {
if (!object.Equals(a.items[i], b.items[i])) {
return false;
}
}
return true;
}

public event EventHandler Changed;

public static bool operator ==(List<T> a, List<T> b) {


return Equals(a, b);
}
public static bool operator !=(List<T> a, List<T> b) {
return !Equals(a, b);
}

63

64

1.

1.6.7.1.
C# , .
, ,
.
, ,
.
,
.
static, . .
. ,
List<T> ,
int.
new. List<string>;
List.
List<string> list1 = new List<string>();
List<string> list2 = new List<string>(10);

, ,
, , .
,
.

!
. , ,
. ,

.
DataContext Linq to Entities.
, ,
. , , ,
.
, .

1.6.7.2.
.
, . , , ,
,
.

1.6.

65


,
, .
, , , .


, ,
, . ,
(
, , ), ,
, ,
.

( ),
, .


, . public:
class Cow
{
public int Milk; // !
}

, , :
class Cow
{
public int Milk { get; set; } // !
}

, , , - :
class Cow {
bool gotMilk = false;
int milk;
public int Milk {
get {
if( !gotMilk ) {
milk = ApplyMilkingMachine();
gotMilk = true; }
return milk;
}

66

1.

set {
ApplyReverseMilkingMachine(value); //
milk = value;
}
}
...
}

, ,
:
class Cow {
public Cow() {
Milk = ApplyMilkingMachine();
}
public int Milk { get; private set; }
...
}

,
, , . , : ,
.


, , , , , ,
.
(, ),
, . ,
.
( ) . DateTime.Now
, .

, , (get) (set), { } . , ,
, , , ,
, ,
, ,
.
(get) ,
.
, ,
get.

1.6.

67

(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, .

, private, CLR
.

1.6.7.3.
, . ,
, this, [ ].
. ,
, ,
.
List , , int.
List int. :
List<string> names = new List<string>();
names.Add("Liz");
names.Add("Martha");
names.Add("Beth");
for (int i = 0; i < names.Count; i++) {
string s = names[i];
names[i] = s.ToUpper();
}

. ,

.

68

1.

1.6.7.4.
,
. , , event
.

event , C#,
,

, . , event
, .


event :
delegate void WorkCompleted();
class Worker {
public WorkCompleted Completed; // - ( )
...
}
class Boss {
public void WorkCompleted() { ... }
}
class Program {
static void Main() {
Worker peter = new Worker();
Boss boss = new Boss();
peter.Completed += boss.WorkCompleted;
// ,
peter.Completed = boss.WorkCompleted; // ,
...
}
}

, event Completed , , . event += -=


:
class Worker {
public event WorkCompleted Completed;
...
}
...
peter.Completed += boss.WorkCompleted; //
peter.Completed = boss.WorkCompleted; //

event
public , . , , += -+
.

1.6.

69

, ,
(
). ,
, . ,
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("Liz");
names.Add("Martha");
names.Add("Beth");
Console.WriteLine(changeCount); // "3"
}
}

, , ,
add remove, - set .

C# 2.0
. ,
names.Changed += new EventHandler(ListChanged);

:
names.Changed += ListChanged;

, .

1.6.7.5.
, . -

70

1.

: , .
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>.

1.6.7.6.
, ,
. ,
, . .

, .
, .
,
.

(finalizers). API , GC.WaitForPendingFinalizers.

using .

1.7.

71

1.7.
, , - ,
- . struct ,
. ,
object.

, , ,
. . 1.3.

,
. ,
.
struct
. ,
100 . Point , 101
100 .
class Point
{
public int x, y;
public Point(int x, int y)
{
this.x = x;
this.y = y;
}
}
class Test
{
static void Main()
{
Point[] points = new Point[100];
for (int i = 0; i < 100; i++) points[i] = new Point(i, i);
}
}

Point :
struct Point
{
public int x, y;
public Point(int x, int y)
{
this.x = x;
this.y = y;
}
}

72

1.

, 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, ,
.

1.8.

73


.
. , .
( string),
. , .

1.8.
, , . ,
, , ,
.
, .
new.
new , .
Length 1. new
, , , null .

, , ,
1, 0, . , ,
C.
C, myArray[x], , x . , myArray[1]
, , ,
.
, . , ,
, .
int,
.
using System;
class Test
{
static void Main() {
int[] a = new int[10];
for (int i = 0; i < a.Length; i++) {

74

1.

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];

a1 10 , a2 50 (10 5) ,
a3 100 (10 5 2) .1

FxCop1 ;
. ,
, .

, .
,
.
, int:
int[][] a = new int[3][];
a[0] = new int[10];
a[1] = new int[5];
a[2] = new int[20];

,
int[] null.
.
new
, , { }.
int[] .
int[] a = new int[] {1, 2, 3};
1

FxCop Microsoft, .NET .NET


Framework. . .

1.9.

75

,
. ,
:
int[] a = {1, 2, 3};

:
int[] t = new int[3];
t[0] = 1;
t[1] = 2;
t[2] = 3;
int[] a = t;


, , - , -
, . ,
. ,
:
int[] a = new int[3];
a[0] = 1; a[1] = 2; a[2] =3;

. , ,
. , , object[] arr = {arr};, , arr , .
object[] arr = new object[1];
arr[0] = arr;

. ,
object[] temp = new object[1];
temp[0] = arr;
object[] arr = temp;

, arr .

1.9.
,
. , , . ,
,
, .
.
IComboBox ITextBox, IListBox.

76

1.

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) {...}
}


, , ,
.NET Framework. ,
, .
, .
,
, , ,
.

,
.
:
EditBox editBox = new EditBox();
IControl control = editBox;
IDataBound dataBound = editBox;


, . ,

IControl IDataBound. EditBox, .
object obj = new EditBox();
IControl control = (IControl)obj;
IDataBound dataBound = (IDataBou)obj;

1.10.

77

EditBox Paint IControl


Bind IDataBound
(public) . C# ,
public.
. , EditBox IControl.Paint IDataBound.Bind,
:
public class EditBox : IControl, IDataBound
{
void IControl.Paint() {...}
void IDataBound.Bind(Binder b) {...}
}


. , 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
}
class Test
{
static void PrintColor(Color color) {
switch (color) {
case Color.Red:
Console.WriteLine("Red");
break;
case Color.Green:

78

1.
Console.WriteLine("Green");
break;
case Color.Blue:
Console.WriteLine("Blue");
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
}


, . Alignment sbyte, System.Enum,
Alignment sbyte.

,
, .
.
, (
) ,
, .

, . :
int i = (int)Color.Blue;
Color c = (Color)2;

// int i = 2;
// Color c = Color.Blue;

1.11.

79


,
, , ,
.

, . , ,
.
, 0
. , :
Color c = 0;


Turbo Pascal.
(, !) , , 65
: !!.
RetirementAge
. .
, C#
.
API,
!

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) {

80

1.
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);
Multiplier m = new Multiplier(2.0);
double[] doubles = Apply(a, m.Multiply);
}
}

Function , double double. Apply


Function double[], double[] .
Main Apply
double[].
( Square
Math.Sin ), ( Multiply
). , , , ,
this .
,
, .
.
,
Multiplier:
double[] doubles = Apply(a, (double x) => x * 2.0);



, .

1.12.
, C# , . , public,

1.12.

81

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) { }
}

HelpAttribute Widget,
HelpAttribute Display .
- , ,
.
- ( Topic
).
,
.
using System;
using System.Reection;
class Test

82

1.

{
static void ShowHelp(MemberInfo member) {
HelpAttribute a = Attribute.GetCustomAttribute(member,
typeof(HelpAttribute)) as HelpAttribute;
if (a == null) {
Console.WriteLine("No help for {0}", member);
}
else {
Console.WriteLine("Help for {0}:", member);
Console.WriteLine(" Url={0}, Topic={1}",
a.Url, a.Topic);
}
}
static void Main() {
ShowHelp(typeof(Widget));
ShowHelp(typeof(Widget).GetMethod("Display"));
}
}

,
,
, .
,
.

,
C#
, IL.
C#.


2.1.
, C#,
, ( 9.1).
Unicode.
, .

UTF-8.
:
1. Unicode.
2. ,
.
3. ,
.

2.2.
C#, .
( 2.2.2) ,
Unicode , ,
, , . ( 2.2.3) ,
, ,
C#.

2.2.1.
.

84

2.

. , .
, .
, .
,
-while:
while ( -

-while, while,
(, , ), -.
, . ,
-:

, - , , . , , .
opt .
:
{ -opt }


:
{
}
{ - }

, -, { }.
,
, , , ,
.
.
,
--:
F f D d M m


--:
F
f
D
d
M
m

2.3.

85

2.2.2.
C# 2.3 2.5.
Unicode, , ( 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# .
-:
-- opt
--:
---
--
---
---:
-opt
-
-
-:
-
-

-:
-


C#: (. 2.3.1), ( 2.3.2),

86

2.

( 2.4) ( 2.5).

( 2.2.3).
C#
,
. ,
. ,

C#.

, . , //
, ,
/.

2.3.1.
C# .
-:
(Carriage return character) (U+000D)
(Line feed character) (U+000A)
(Carriage return character) (U+000D),
(line feed character) (U+000A)
(Next line character) (U+0085)
(Line separator character) (U+2028)
(Paragraph separator character) (U+2029)

, ,

C# :
Control-Z (U+001A),
.
(U+000D) ,

(U+000D), (U+000A),
(U+2028) (U+2029).

2.3.2.
: . //
. /*
*/. .

2.3.

87

:
-
-
-:
//
-opt
-:
-
-

-:
Unicode, --
--:
(U+000D)
(U+000A)
(U+0085)
(U+2028)
(U+2029)
-:
/*
--opt
--:
--
--

--

--:
/
---
opt
:
*
*
---:
Unicode, / *

. /*
*/ , //,
// /*
.
.

/* ,
", "
*/
class Hello
{
static void Main() {
System.Console.WriteLine(", ");
}
}

88

2.

// ,
// ", "
//
class Hello
//
{
static void Main() {
// "Main"
System.Console.WriteLine(", ");
}
}

2.3.3.
Unicode Zs (
), ,
.
-:
Unicode Zs
(U+0009)
(U+000B)
(U+000C)

2.4.
: , , ,
.
, .
:

-
-
-
-
-
---

2.4.1. Unicode
Unicode Unicode.
( 2.4.2),
( 2.4.4.4) ( 2.4.4.5).
Unicode
(, ,
).

2.4.

89

--unicode:
\u - - -
-
\U - - - -
-
- -


C# Java,
Unicode .

Unicode Unicode, ,
\u \U. C# 16-
Unicode, Unicode U+10000
U+10FFFF ,
Unicode--. Unicode
0x10FFFF .
. , \u005Cu005C \u005C, \. \ Unicode
\u005C.

class Class1
{
static void Test(bool \u0066) {
char c = \u0066;
if (\u0066)
System.Console.WriteLine(c.ToString());
}
}

\u0066, f.
class Class1
{
static void Test(bool f) {
char c = f;
if (f)
System.Console.WriteLine(c.ToString());
}
}

2.4.2.
, , Unicode Standard Annex 31,
, : (

90

2.

C), Unicode , @
,
.
:
-
@ ---
-:
---, -
--:
--
-opt
--:
-
_ ( U+005F)
-:
-
-

-:
-
-
-
-
--
-:
Unicode Lu, Ll, Lt, Lm, Lo Nl
--unicode,
Lu, Ll, Lt, Lm, Lo Nl
-:
Unicode Mn Mc
--unicode,
Mn Mc
-:
Unicode Nd
--unicode, Nd
-:
Unicode Pc
--unicode, Pc
--:
Unicode Cf
--unicode, Cf

Unicode, , . Unicode 3.0 (The Unicode Standard, Version 3.0), 4.5.

2.4.

91

: identier1, _identier2, @if.


, , , (Unicode Normalization
Form C), Unicode Standard Annex 15.
, Normalization Form 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. , ,
Unicode , cl\u0061ss ,
, @class.
, :
@, , .
--Unicode Unicode.
.
,
(U+005F), . , ,
.

92

2.

2.4.3.
,
, , @.
-:
abstract
as
byte
case
class
const
delegate
do
event
explicit
xed
oat
if
implicit
internal
is
new
null
override
params
readonly
ref
short
sizeof
struct
switch
try
typeof
unsafe
ushort
volatile
while

base
catch
continue
double
extern
for
in
lock
object
private
return
stackalloc
this
uint
using

bool
char
decimal
else
false
foreach
int
long
operator
protected
sbyte
static
throw
ulong
virtual

break
checked
default
enum
nally
goto
interface
namespace
out
public
sealed
string
true
unchecked
void

, .
. ,
get set ( 10.7.2).
, get set, ,
.
,
var ( 8.5.1), . , .

C# . , (yield,
select . .), ,
.
.


( ), :
add alias ascending assembly by descending dynamic equals eld from get
global group into join let method module on orderby param partial property
remove select set type typevar value var where yield

2.4.

93

2.4.4. ()
.
:
-
-
-
-
-
-

2.4.4.1.
: true false.
-:
true
false

bool.

2.4.4.2.
int, uint,
long ulong. : .
-:
--
--
--:
-
--opt
-:
-
-

-:
0
1
2
3
4
5

--:
U
u
L
l
UL
Ul
uL

ul

LU

Lu

lU

lu

--:
0x
-
--opt
0X
-
--opt
-:
-
-

-:
0 1 2 3 4 5 6 7 8 9 A

94

2.


C# . -,
- . -,
,
. :
FlightNumber = 0541;

, , .

:
, int, uint, long,
ulong, .
U u, uint, ulong,
.
L l, long, ulong,
.
UL, Ul, uL, ul, LU, Lu, lU, lu, ulong.

ulong, .
, long
L l, l 1.
int
long , :
-- 2147483648 (231)
-- ( 7.7.2), int 2147483648 (231). - uint.
-- 9223372036854775808
(263) -- -- L l ( 7.7.2), long 9223372036854775808
(263). --
ulong.

( 6.1.8)
uint, long ulong
( short, ushort, byte sbyte):
uint x = 3; long y = 3; ulong z = 3;

U L . ,
, 64- ,
32- :

2.4.

95

long error = 1000000 * 1000000 ;

// (32-
// )
long trillion = 1000000L * 1000000L; // ( )

2.4.4.3.
oat, double
decimal.
-:
- . -
-opt
--opt
.
opt
--opt
-
-
--opt
-
--
-:
e opt -
E opt -
:
+

--:
F
f
D
d
M
m

-- , double.
:
F f oat. ,
1f, 1.5f, 1e10f 123.456F oat.
D d double. ,
1d, 1.5d, 1e10d 123.456D double.
M m decimal. ,
1m, 1.5m, 1e10m 123.456M decimal.
decimal,
( 4.1.7). ,
(
0). , , 2.900m
0, 2900 3.
, .
oat double IEEE.
,
. , 1.3F , 1.F
.

96

2.


m f. (oat) (decimal)
. , ,
1.5 double:
oat x = 1.5;

// : no implicit conversion from double to oat


// (
// double oat
decimal y = 1.5; // : no implicit conversion from double to decimal
// ( double decimal

, , C# int decimal:
decimal z = 123; // : int,
// decimal

d ,
:
Console.WriteLine ((123.0).GetType() == typeof (double));

// True

2.4.4.4.
,
, a.
-:

:
-
--
--
--unicode
-:
, (U+0027), \ (U+005C) --
--:
\
\"
\\
\0
\a
\b
\f

\n

\r

\t

\v

--:
\x
-
-opt
-opt
-opt

, (\), : , , \, 0, a, b, f, n, r, t, u, U, x, v.
.

Unicode ,
, \x.
, , U+FFFF,
.

2.4.

97

Unicode ( 2.4.1) U+0000 U+FFFF.



Unicode, , .

\
\
\\
\0
\a
\b
\f
\n
\r
\t
\v

Unicode
0x0027
0x0022
0x005C
0x0000
0x0007
0x0008
0x000C
0x000A
0x000D
0x0009
0x000B

char.

2.4.4.5.
C# :
() .
,
, hello,
(, \t ),
Unicode.
@,
, . , -. , , Unicode
. .

, ,
, , . , , , , \r\n \n
: .
-:
--
--

98

2.

--:
"
---opt
---:
---
---

"

---

---:
----
--
--
--unicode
----:
, " (U+0022), \ (U+005C)
--:
@"
---opt

"

---:
---
--- ---
---:
----
--
----:
"
--:
""

(\)
: , , \, 0, a, b, f, n, r, t, u, U, x, v.
.

string a = "hello, world";


string b = @"hello, world";

// hello, world
// hello, world

string c = "hello \t world";


string d = @"hello \t world";

// hello
world
// hello \t world

string
string
string
string

//
//
//
//

e
f
g
h

=
=
=
=

"Joe said \"Hello\" to me";


@"Joe said ""Hello"" to me";
"\\\\server\\share\\le.txt";
@"\\server\share\le.txt";

Joe said "Hello" to me


Joe said "Hello" to me
\\server\share\le.txt
\\server\share\le.txt

string i = "one\r\ntwo\r\nthree";
string j = @"one
two
three";

. j , .

2.4.

99

, , ,
, , .

C#
(`). :
. ,
. , C# ,
.

, \x123
123. ,
12, 3,
\x00123 \x12 + 3.

, .
, \x9Tabbed, , , ,
\x9Badly tabbed Unicode U+9BAD.

string.
.
, ( 7.10.7), . , ,
:
class Test
{
static void Main() {
object a = "hello";
object b = "hello";
System.Console.WriteLine(a == b);
}
}

True,
.

(interning). ,
.


100

2.

a b object, .
a b string,
string ==, true,
,
.

2.4.4.6. null
-:
null

null (-) .

null .

2.4.5.
.
,
. , a + b
+ a b.
.
----:
{
}
[
]
(
)
.
,
+
*
/
%
&
|
^
=
<
>
?
??
::
++
-->
==
!=
<=
>=
+=
-=
*=
&=
|=
^=
<<
<<= =>

:
!
&&
/=

;
~
||
%=

-:
>|>
---:
>|>=

- -- , , , ( )
. ,
-- ( 10.1.3).

2.5.

101

2.5.

,
. , C#,
.


, .
C C++. C# :
.
-:
-
-
-line
-
-region
-pragma

:
#de ne #undef
( 2.5.3).
#if, #elif, #else #endif
( 2.5.4).
#line , ( 2.5.7).
#error #warning ( 2.5.5).
#region #endregion
( 2.5.6).
#pragma ( 2.5.8).

# . #
#
.
, #dene, #undef, #if, #elif,
#else, #endif #line, .
( /* */) ,
, .
C#. ,

102

2.

,
C#. ,
#dene A
#undef B
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

: .
,
( ).
#dene, , , .
, #undef
.
#dene #undef .
true,
false. ,
.
, , false.

103

2.5.


C#.
#dene #undef .

2.5.2.
#if #elif. !, ==, !=, &&, ||,
.
-:
-or
-opt

-opt

-or:
-and
-or

-opt

||

-opt

-and

-and:
-
-and -opt

&&

-opt

-:
--
-
-opt == -opt
--
-
-opt != -opt
--
--:
--
!
-opt
--
--:
true
false
--
(
-opt
-

-opt

true, false.

. ,
( 7.19), ,
, , .

2.5.3.

104

2.

-:
-opt
#
-opt
-
--
#
-opt
-opt
-
--
--:
-opt

dene

undef

-opt

#dene ,
, , . , #undef ,
,
, .
#dene #undef
( 2.4); .
, #dene #undef
.

#dene Enterprise
#if Professional || Enterprise
#dene Advanced
#endif
namespace Megacorp.Data
{
#if Advanced
class PivotTable {...}
#endif
}

, #dene
( namespace) .
, #dene
:
#dene A
namespace N
{
#dene B
#if B
class Class1 {}
#endif
}

#dene ,
#undef . ,
, A,
:
#dene A
#dene A

#undef ,
. , ,

2.5.

105

A, ; , .
#dene A
#undef A
#undef A

2.5.4.
, .
-:
-if-
-elif-opt
-endif-

-else-opt

-if-:
-opt
#
-opt
if
-
-
--
-opt
-elif-:
-elif-
-elif-

-elif-

-elif- :
-opt
#
-opt
elif
-
--
-opt

-else-:
-opt
#
-opt
else
--
-opt
-endif:
#
-opt
-opt
--

endif

-:
--
-
-:
--
-
--
--:
-opt
-
-
-:
-opt

-:
-, #

-opt

106

2.

, , , , #if,
#elif, #else #endif. .
. ,
, ,
.

#if #endif, #elif? ?
, , ?
#elseif ,

- - .
- #if #elif
, true.
true, -.
- false
#else, - #else.
-.
-, ,
--: , , ;
; ,
.
, , -:
, ; ;
, . -, -,
- (
#if...#endif #region...#endregion) .
, :
#dene Debug
//
#undef Trace
//
class PurchaseTransaction
{
void Commit() {
#if Debug
CheckConsistency();
#if Trace

2.5.

107

WriteToLog(this.ToString());
#endif
#endif
CommitHelper();
}
}

, .
, ,
#else.
#dene Debug
//
class PurchaseTransaction
{
void Commit() {
#if Debug
CheckConsistency();
#else
// -
#endif
}
}

, , .


, C#, Visual Studio,
, , . ,
#dene Debug
//
#undef Trace
//
class PurchaseTransaction {
void Commit() {
#if Debug
CheckConsistency();
#if Trace
WriteToLog(this.ToString());
#endif
#endif
CommitHelper();
}.

,
. ,
class Hello
{
static void Main() {
System.Console.WriteLine(@"hello,
#if Debug
World

108

2.

#else
Nebraska
#endif
");
}
}

:
hello,
#if Debug
world
#else
Nebraska
#endif


-.
#if X
/*
#else
/* */ class Q { }
#endif

, X,
(class Q { }). X , #if #endif. X ,
(#if, #else, #endif).

2.5.5.

, ,
.
-:
-opt
-opt

#
#

-opt
-opt

-:
-
- --opt

error
warning

-
-

#warning Code review needed before check-in


#if Debug && Retail
#error A build cant be both debug and retail
#endif
class Test {...}

("Code review needed before check-in") ("A build cant be both debug and retail"),
Debug Retail . ,
; , -

2.5.

109

,
cant.

2.5.6. region
#region .
-region:
-

-opt

-:
-opt

-opt

region

-:
-opt

-opt

endregion

; ,
. , #region
#endregion, :
. #region #endregion
-.
region
#region
...
#endregion


:
#if true
...
#endif

2.5.7. line
#line
, .
#line , C# .
-line:
-opt
#
-opt
-
-
-:
-
-
default
hidden

line

110

2.

-:
"
--
--:
--
--

"

--

--:
- "

#line , . ,
-, default, , ( , ) ,
.

, , ,
#line . : #line
,
. ,
:
#line 5
Console.WriteLine(@"First line
#line 6
Second line");

#line default #line.


,
#line .
#line hidden
, . , #line
hidden #line ( #line
hidden) .
.
, - ,
; \ -
.

2.5.8. pragma
#pragma
. , #pragma, .

2.5.
-pragma:
#
-opt
-pragma
-

-opt

pragma

111

-pragma:
-pragma-warning

C# #pragma
. , ,
#pragma.
C#, Microsoft C#
#pragma; , , .

2.5.8.1. pragma warning


#pragma warning

.
-pragma-warning:
warning -
warning -

-warning
-warning

-warning

-warning:
disable
restore
-warning:
-
-warning -opt

-opt

#pragma warning, ,
. #pragma warning,
, , .

, .
all
(
) . , ,
, - , ,
.

#pragma warning disable


.
#pragma warning restore
, .
, -

112

2.

, #pragma warning restore ( )


.
#pragma warning
,
,
Microsoft C#.
using System;
class Program
{
[Obsolete]
static void Foo() {}
static void Main() {
#pragma warning disable 612
Foo();
#pragma warning restore 612
}
}


, ,
. -
/ , . ,
,
.


3.1.
, , . () .
,
.
, .
, , . , ,
, ,
. ,
,
.
.
, ,
. Main
:
static
static
static
static

void Main() {...}


void Main(string[] args) {... }
int Main() {...}
int Main(string [] args) {...}

, ()
int.
( 3.2).
, , . , string[].
, string[], ,
. string[] null,
, .
C# , ,
.
, Main

114

3.

.
Main ,
, string[].
. , Main , .
(, ), Main
.

csc /main:.

C# .
( 3.5.1)
( 10.3.5), ,
, . , .
:

.
, , .
, .

3.2.
.
int,
.

.
void,
}, ,
return, ,
0.

C#
.

3.3.

115

, ,
(, 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).
(declaration space),
. ,
.
(,
).

(scope). , .
,
. , , ,
. (
, ,
, ,
-.)

.
--, --,
,
.
-- --, ,
.

116

3.

, . --,
--, --
-. ,
,
. , . ,
. , ,
, ( 3.6). ,
, . ,
, .
, .
. ( -) -.
. -.
, , ,
, . (- -)
-.
, ,
.
, . ,

,
, .

, .
switch-, for, foreach using,

.
-- --. ,
,
. , , , ,
.

3.3.

117

switch- .
-,
-.
. , ,
, .
, , ,
. ,
, , , , , , , , , . :
,
( ).
, ( 3.7).
( 14.3) ,
.
,
. :
namespace Megacorp.Data
{
class Customer
{
...
}
}
namespace Megacorp.Data
{
Class Order
{
...
}
}


, : Megacorp.Data.Customer Megacorp.Data.Order.
,
,
.


. ,
.

118

3.

,
. , F G , i, ,
. H I , i
.
class A
{
void F()
{
int i = 0;
if (true)
{
int i =
}
}
void G()
{
if (true)
{
int i =
}
int i = 1;
}
void H()
{
if (true)
{
int i =
}
if (true)
{
int i =
}
}
void I()
{
for (int i
H();
for (int i
H();
}
}

1;

0;

0;

1;

= 0; i < 10; i++)


= 0; i < 10; i++)

3.4.
. ,
, . .
, , ,
. , ,

3.4.

119

, ,
.
, , ,
. - ,
( 3.5.1),
( 3.7.1.2).

3.4.1.
, ,
. ,
.
, ,
. , .
. private, protected internal,
.

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.
oat System.Single.
double System.Double.
decimal System.Decimal.
bool System.Boolean.

120

3.

3.4.3.
, ,
, System.Enum System.ValueType
object.

3.4.4.
, , , ( object,
). ,
, , , , , ,
, ,
,
.
.
, , ,
, , , , , , .
object string ,
:
object System.Object.
string System.String.

3.4.5.
,
. object ,
, - ( 13.2).
object (member lookup)
( 7.4).

3.4.6.
, System.
Array.

3.4.7.
, System.
Delegate.

3.5.

121


Microsoft C#
Invoke, BeginInvoke EndInvoke , System.
MulticastDelegate.


, , Invoke, BeginInvoke EndInvoke,
Delegate MulticastDelegate,
.
, , C#.

3.5.
.
( 3.5.1)
,
, .
, , . , , ,
. , , , ( 3.5.2) .

3.5.1.
:
public
public. public .
protected protected. protected
,
.
internal internal. internal
.
protected internal ( protected internal) protected
internal. protected internal
,
.

122

3.

private private. private


, .

, .
.


C# :
, getter/setter,
, .
, -
. , - , ,
. , , , ,
.
, ,
. ,
.

public. .
, ,
public internal; internal.
;
private. (, ,
, , , , public
internal.)

protected
protected internal, .
protected protected internal,
(CS1057).

public, internal private;


private, .
, ( -

3.5.

123

), protected protected internal. (,


, , public,
internal private, , ,
public internal.)
public.
.
public.
.

, .
System.Enum, System.ValueType System.Object,
public.


, , , .
.


, ;
.


. , , public , internal,
.
, ,
public public ,
.


, public internal ( private)
.
internal ,
.

124

3.

3.5.2.
(accessibility domain) (, ), . :
, ,
, .
, ,
,
, -
(, , , ).
(, object, int
double) .
T ( 4.4.3),
P, :
T public, T P , P.
T internal, T
P.
,
, .
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
P , T;
M T D.
M internal, M
T P.
M private, M
T.
, , .

3.5.

125

, ,
, , .
, 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;
}
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;
}
}

126

3.

A A.X .
A.Y, B, B.X, B.Y, B.C, B.C.X B.C.Y .
A.Z A.
B.Z B.D B, B.C B.D.
B..Z B.C.
B.D.X B.D.Y B,
B.C B.D.
B.D.Z B.D.
,
, . ,
X public, , A.X, ,
.

public internal ,
, internal. , ,
, public
, .
, , ,
internal, public internal:
public, , public? , , public .


,
,
,
. , , public, private protected.

3.4, , , , .
private . private
, .

class A
{
int x;
static void F(B b)
{

3.5.
b.x = 1;

127

//

}
}
class B : A
{
static void F(B b)
{
b.x = 1;
}
}

// : x

B x private A.
private, - A. ,
b.x A.F, B.F.

, new B:F().

3.5.3. protected

protected
, , protected internal
, ,
, ,
. , ,
,
. ,
.

, , Animal Mammal Reptile Animal Feed(). Mammal
Feed() Mammal Mammal (, Tiger).
Mammal Feed() Reptile,
Mammal Reptile .
, Animal Reptile , Mammal Feed()
Animal.

B , protected M,
D B. - D M
:

128

3.

- - M.
- E.M, , E T ,
T, T D, ,
D.
- base.M.

protected ( 10.11.1).

, . ,
protected new:
class Base
{
protected Base() { }
}
class Derived : Base
{
static void Main()
{
new Base();

//
//
//
//

Error CS0122: Base.Base() is


inaccessible due to its protection level
( CS0122: Base.Base()
- )

}
}


public class A
{
protected int x;
static void F(A a, B b)
{
a.x = 1;
b.x = 1;
}
}
public class B : A
{
static void F(A a, B b)
{
a.x = 1;
b.x = 1;

//
//

// Error: must access through instance of B


// (: B)
//

}
}

A x A, B,
A ,

3.5.

129

A. B x
A, A B.

class C<T>
{
protected T x;
}
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 ,
, .

, private/internal ,
. .


,
. .

3.5.4.
C# ,
, . , T ,
M, T M. , T , M, T
, M.

. , protected public sealed , ,
(, ,
- ), .

130

3.

:
,
.
-
, .
-
, .
, .
, .
, .
, .
-
, .
-
, .
-
, .

class A {...}
public class B: A {...}

B , A ,
B.
,
class A {...}
public class B
{
A F() {...}
internal A G() {...}
public A H() {...}
}

H B , A
, .

3.6.
, ,
:
, -,
(-, - ) ,
. - ,
, ,
- . -

3.6.

131

, params,
, -.

-
(, , .NET System.Nullable System.Nullable<T>).
, , .
, , :
void Process<T>(T reference) where T : class { ... }
void Process<T>(T value) where T : struct { ... }

, .
, , (type inference).

(, - )
, . params,
.
, .
, params,
.
, .
.
, :
,

, .

.
,
, .

:
,
:


132

3.

class A : IIndexable
{
int IIndexable.this[int x]
{
get { /* ... */ }
}
public int this[int x] // Okay
{
get { /* ... */ }
}
}



.
out ref , , ,
ref out.
, , out ref, .
( ) ref out
.
( C#
(CLI),
, ref out.)
, object dynamic . , , , ,
object dynamic.

.
interface ITest
{
void F();
//
void F(int x);
//
void F(ref int x);
//
void F(out int x);
//
void F(int x, int y);
//
int F(string s);
//
int F(int x);
//
void F(string[] a);
//
void F(params string[] a);
}

F()
F(int)
F(ref int)
F(out int)

F(int, int)
F(string)
F(int)
F(string[])
// F(string[])

, ref out ( 10.6.1)


. , F(int) F(ref int) . F(ref int) F(out int)
,
ref out. , params
,

3.7.

133

params.
, F(int) F(params string[]),
, .

3.7.
(scope) , .
,
(, , , 3.3, ,
, ).
, ,
.
, -- ( 9.5),
--, .
, -- -- N, --
--, N,
N .
, --
(extern-alias-directive), -using, --- -
--, .
--
. , -- ,
, .
, -using
( 9.4), --
- --,
. -using
-
--,
. , -using ,
- --,
.
-, - - ( 10.1), -, - - -.

134

3.

-, --
- ( 11.1), -,
-- - .
-, - - ( 13.1), -,
-- - .
-, - - ( 15.1), -, - -- .
, --
( 10.1.6), -, .
, ,
( 3.5.2).
, - ( 11.2), -,
.
, - ( 14.3), -,
.
, - ( 10.6), - -.
, -
( 10.9), -- .
, - ( 10.10), -.
, - ( 10.11), -
.
, - ( 7.15), -- -.
, --
( 7.15), --.
, - ( 8.4),
, .
, - ( 8.5.1), ,
.

3.7.

135

, -switch switch ( 8.7.2), -switch.


, for for ( 8.8.3), -for, -for,
-for , for.
, - ( 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);
}

// :

//

136

3.

void H()
{
int a = 1, b = ++a;
}

//

F i , . ,
, . G j j
, --. H --
, --
--.
,
, , ,
.
,
,
, , , ,
.
,
.
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.
, . ,
,
, . . ,
, .

3.7.

137

,
- . .

, ,
.

3.7.1.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) { }
class Inner
{
void G()
{
F(1);
F("Hello");
}
static void F(long l) { }
}
}

// Outer.Inner.F
//

F(1) F, Inner, F
.
F("Hello") .

138

3.


,
,
: , (non-invocable) (. 7.3).
class A
{
static void Foo() { }
class B
{
const int Foo = 1;
void Bar()
{
Foo();
}
}
}

//

3.7.1.2.
,
, . :
, , , , ,
.
, , , , ( , ,
).
, , ( ).
, ( 10.10), ,
. ,
.
,

.
class Base
{
public void F() { }
}
class Derived : Base
{
public void F() { }
}

// :

3.7.

139

F Derived . , , , . ,
- ,
Base F, . ,

, .
, ,
new:
class Base
{
public void F() { }
}
class Derived : Base
{
new public void F() { }
}

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 private, MoreDerived. , F()
MoreDerived.G Base.F.

new, ,
, ,
,
. :
class Base { public void F() {} }
class Derived : Base { new public void F() {} }
Derived d = new Derived();
((Base)d).F();
// Base.F ,

, .

140

3.

3.8.
C# , --
- .
--:
----
-:
----
----:
--opt
----
. --opt
--

-- ----,
. , , --- --
, .
- ( 4.4.1) - (- ).
- ----, .
, , ---- -,
.
---- -, 9.7.
----
:
I
I<A1, ..., AK>
N.I
N.I<A1, ..., AK>
I , N ----,
<A1, ..., AK> --. - , , K .
---- :
---- I I<A1, ...,
AK>:
 K ---- ( 10.6)
- ( 10.1.3) I, --- -.
 , ---- , T ( 10.3.1), -

3.8.

141


( ):
 K T - I,
---- -.
 , ----
T I K -, --- , -. , ,
. , ,
(, , , , , , ,
) - -
----
.
 ,
N, , ---, (
) , , :
 K I N, :
 ----
N
-- using-, I ,
----
.
 ----
I N.
 , N I K -,
:
 K ---- N
-- using-, I
, ----
.
 ----
, -.
 , ---- N:
 K
-- using--, -

142

3.

I ,
----
.
 , , using-- , I K -, --- ,
-.
 , , using-- , I K -, --- .
 ----
.
, ---- N.I N.I<A1,
..., AK>. N ----.
N , .
N.I N.I<A1, ..., AK> :
K N N
I, ----
.
, N N I K -, --- , -.
, N (, ) N I K -, --- ,
-. , . , N.I
N, N object ( 10.1.4.1).
N.I ---- .

( 7.6.2), . ,
-
-:
class T
{
public const int X = 1;
}

3.8.

143

class C
{
void Foo<T>(int x = T.X /* global::T */,
T y = default(T) /* type parameter */) { }
}

----
( 10.11.3) :
---- T --- T.I,
---- T -typeof ( 7.5.11)
typeof(T).

3.8.1.
,
. N :
N ,
N.
S.N, S
, N.
, N , N, .
, , .
, ,
.
class A { }
namespace X
{
class B
{
class C { }
}
namespace Y
{
class D { }
}
}
namespace X.Y
{
class E { }
}

// A
// X
// X.B
// X.B.C
// X.Y
// X.Y.D

// X.Y
// X.Y.E

144

3.


( , ), .
, global:: ( 9.7). ,
, ; .

(IDE) global::
, .

3.9.
C# ,
.
.
:
1. , , ,
.
2. , ,
. C#
, , .
, , ,
,
, ( )
.

C# Finalize
. Finalize
. C++/CLI
, , IDisposable,
.


, , ,
. , ,
-

3.9.

145


.
, -
, .NET
SafeHandle.

3. ,
( 10.13) ( ).
,
.

,
IDisposable.
, .


, GC.ReRegisterForFinalize
.
, .

4. ,
,
,
.
5. , , , ,
.
:
,
.
,
, C# , . , C# ,
,
, - .
System.GC. ,
, ( )
. .

146

3.


.
, , , ,
.

.

,
, .
using System;
class A
{
~A()
{
Console.WriteLine(" A");
}
}
class B
{
object Ref;
public B(object o)
{
Ref = o;
}
~B()
{
Console.WriteLine(" B");
}
}
class Test
{
static void Main()
{
B b = new B(new A());
b = null;
GC.Collect();
GC.WaitForPendingFinalizers();
}
}

A B. , b null,

.
A
B

B
A

3.9.

147

,
.

. :
using System;
using System;
class A
{
~A() {
Console.WriteLine(" A");
}
public void F() {
Console.WriteLine("A.F");
Test.RefA = this;
}
}
class B
{
public A Ref;
~B() {
Console.WriteLine(" 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 null");
}


, :
A
B
A.F
RefA null

, A A ,
A ( F) . , . B
A, , , -

148

3.

Test.RefA. WaitForPendingFinalizers B , A - Test.RefA.


, , , ,
, .

: , , ,
. ,
, ,
.
Console.WriteLine , .
. , , , , , .

System.IDisposable. ,
,
using ( 8.13).

GC.Collect() .
, - .
, .
, ,
. ,
. GC.Collect()
. ,
, , .
? , , -, ?
?


,
, .
,
, .
, , .

3.10.

149


-,
.NET, . ,
,
(, XNA Xbox) Windows Phone 7,
.
,
.


:
C
, C++ C. ,
, C,
.


, , . , . ,
.
, ,
. ,
. , .

3.10.
C# ,
.
-
, , ,
. ,
, ( 10.5.3), lock ( 8.12),
.
C#, :
. ,
.
, , ( 10.5.4
10.5.5).

150

3.


(volatile reads) ( 10.5.3). ,
, ,
(
- ).
( , ), ,
.

C# : -
. ,
-. - -, .
:
-
-
-

. .
18.2.
- , ,
(). , , , , ,
, .
- ,
.
C# ,
. C# object,
.
. - (boxing) (unboxing) ( 4.3).

-,
. , , ,
object.

4.1. -
- . C#
, . .

152

4.

-:

:
-
-
-
-:
-
bool
-:
-
-
decimal
-:
sbyte
byte
short
ushort
int
uint
long
ulong
char
-:
oat
double
-:
--

--:

:
-

, -
null, (nullable).
-
-, null.
- . , , .

4.1.1. System.ValueType
- System.ValueType, ,
, object.

4.1. -

153

-, , , - ( 10.1.1.2).
, System.ValueType -. , , .

. :
, - ? ,
, . , - , ,
, .
, ,
-.

4.1.2.
-
, . ,
:
-
, :
 sbyte, byte, short, ushort, int, uint, long ulong
0.
 char \x0000.
 oat 0.0f.
 double 0.0d.
 decimal 0.0m.
 bool false.
 E 0,
E.
 - , -
, null.

, (
). ,
.

154

4.

 - ,
HasValue false Value .

.
, new.
,
. i j
.
class A
{
void F() {
int i = 0;
int j = new int();
}
}

- ,
,
( 11.3.8).

default(type).


, , C#
. .NET -, .
.NET -
. , ,
- .

4.1.3.
-, , , , , , , ,
.
11.1.

4.1. -

155

4.1.4.
C# , .
,
System, .

sbyte
byte
short
ushort
int
uint
long
ulong
char
oat
double
bool
decimal


System.SByte
System.Byte
System.Int16
System.UInt16
System.Int32
System.UInt32
System.Int64
System.UInt64
System.Char
System.Single
System.Double
System.Boolean
System.Decimal

, . , int ,
System.Int32, , System.Object,
:
int i = int.MaxValue;
string s = i.ToString();
System.Int32.ToString()
string t = 123.ToString();
System.Int32.ToString()

// System.Int32.MaxValue
//
//

, :
( 2.4.4). , 123 int, a
char. C#, ,
,

.

,
, short, .
, short,
short, ,
.


156

4.

,
. , NaN (Not-a-Number) .

, .
- ( 7.19). ,
, ,
.

: ,
- .

const ( 10.4). , static readonly.


, ,
, ,
, ,

( 6.4.3).

,
IL (, , ).
, ( oat, double ) , .

4.1.5.
C# : sbyte, byte, short, ushort, int,
uint, long, ulong char. :
sbyte 8-
128 127.
byte 8-
0 255.
short 16-
32 768 32 767.

4.1. -

157

ushort 16-
0 65 535.
int 32-
2 147 483 648 2 147 483 647.
uint 32-
0 4 294 967 295.
long 64-
9 223 372 036 854 775 808 9 223 372 036 854 775 807.
ulong 64-
0 18 446 744 073 709 551 615.
char 16-
0 65 535. char Unicode. char , ushort,
, , .

, , ,
, , ,
int ( )
double . .

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 ( bool ).
, long,
ulong.
<< >> T, T
int, uint, long ulong,

158

4.

. ,
, , T,
T.
char , :
char. ,
sbyte, byte ushort ,
char, sbyte, byte ushort char.
char -
- char. , (char)10
, \x000A.
checked unchecked

( 7.6.12). System.
OverowException. ,
, , .

4.1.6.
C# : oat double. oat
double 32- 64-
IEEE 754, :
. , ,
( 7.8.2).

, Equals -
.
- , .

Equals.
using System;
struct S
{
double X;
static void Main()

4.1. -

159

{
var a = new S {X = 0.0};
var b = new S {X = -0.0};
Console.WriteLine(a.X.Equals(b.X)); // True
Console.WriteLine(a.Equals(b));
// False
}
}


, ,
, C#
, 0.0, . , ,
, . ,
, , , 1/(-0.0) = -Innity < 0:
public static string DoubleToString(double d) {
if (d == 0.0 && 1/d < 0)
return "-0.0";
else
return d.ToString();
}

.
, .
, 1.0/0.0 , 1.0/0.0 .
, (Not-a-Number),
NaN.
, .

NaN, . . 7.8.1.

s m 2e,
s 1 1, m e :
oat 0 < m < 224 149 e 104; double 0 < m < 253 1075 e 970.
.
oat 1.5 1045 3.4 1038
7 .

160

4.

double 5.0 10324


1.7 10308 15 16 .

, ,
:
, .
, double,
double,
double, double ( bool
).
oat, oat ( bool
).
, ,
.
, NaN,
:
, .

, .
,
NaN.
NaN, NaN.
,
. , extended long double
, double,
, . .
, , C# . - ,
. x * y / z,
, double,
double,

.

4.1. -

161


NaN . Microsofts
Windows Presentation Foundation double.NaN , .
; ,
.

4.1.7.
(decimal) 128- ,
. 1.0 1028 7.9 1028 28 29 .
decimal
(1)s c 10-e, s 0 1, c 0 c < 296, e 0 e 28. ,
NaN. decimal 96- , . decimal ,
1.0m, 28- ,
. decimal , 1.0m,
28 29 . oat
double, , 0.1,
decimal. oat double
, .

IEEE 754-2008 ,
decimal128. decimal, ,
128 . 34 ,
106134 106144 NaN.
(Mike Cowlishaw) IBM. decimal, C#
IEEE decimal128.

decimal, decimal.
decimal.

decimal (oat, double). ,
. decimal
.

162

4.

decimal ( , ),
.
, ,
, ( ).
0.

, , ,
, .
, , , .

,
5 1029,
.
, ,
System.OverowException.
decimal , ,
. , decimal , decimal
.
decimal,
decimal.

C# Currency, Visual Basic 6
OLE Automation. decimal
, Currency, ,
Currency, decimal.

4.1.8.
bool .
bool true false.
. , ;
, .

4.1. -

163

C C++ ,
false, ,
true. C# null.

- bool
null. :
object obj = null;
if( obj ) { ... }
if( obj != null ) { ... }

// C/C++, C#
// C/C++/C#

4.1.9.
() ,
. ,
byte, sbyte, short, ushort, int, uint, long ulong. , .
. ( 14.1).

:
. ,
, , .


CLR char . , , C#
, ,
.

4.1.10.

. T?, T .
System.Nullable<T>,
.

164

4.

-, , -,
System.Nullable<T>, T? ( T),
-, - (
- struct). System.Nullable<T> T
- ( 10.1.5), ,
-.
.
, int?? string? .
T? :
HasValue bool.
Value T.
, HasValue true, . , Value
.
, HasValue false, . .
Value System.
InvalidOperationException. Value (unwrapping).
T?
T. x T

new T?(x)

T?, Value x.
(wrapping).
null T? ( 6.1.5)
T T? ( 6.1.4).

4.2.
, , .
-:

:
-
object
dynamic
string

4.2.

165

:
-
:
-

-:

-:
-
-

-:
[ -opt ]
-:
,
- ,
:
-

, . null
.

4.2.1.
, ( ), (, , , ,
, , )
. , ,

. --
( 7.6.10.1).
10.
C#,
.

System.Object
System.String
System.ValueType
System.Enum
System.Array
System.Delegate
System.Exception


( 4.2.2)
C# ( 4.2.3)
- ( 4.1.1)
( 14)
( 12)
( 15)
( 16)

166

4.

4.2.2. object
object .
C# object.
object
System.Object.

4.2.3. dynamic
dynamic, object, . , . ,
, ,
. , .
4.7, 7.2.2.

4.2.4. string
string ,
object. string
Unicode.
string ( 2.4.4.5).
string
System.String.

4.2.5.
. , , .
,
.
13.

4.2.6.
,
, . ,
, , ,
.
12.

4.3.

167

4.2.7.
, .

.
C C++ ,
, , .
, ,
.
15.

C++ - , ,
!

4.3.
C#.
- , object .
,
.

, C# 2.0, ,
,
, . C# 2.0
, . , ,
, ,
.


,
,
out ref.

168

4.


, ,
, , .
.

4.3.1.
- . :
- object.
- System.ValueType.
- ,
-.
, .

; .
, .

System.Enum.
- System.Enum.

, .
; . ,
.

, -
, ( 6.1.10).
-- --
.
- ,
null (HasValue false),
(unwrapping) .

4.3.

169

-- ,
,
:
sealed class Box<T>: System.ValueType
{
T value;
public Box(T t) {
value = t;
}
}

v T
new Box<T>(v) object .
,
int i = 123;
object box = i;


int i = 123;
object box = new Box<int>(i);

, Box<T>, ,
. T
T, is
T.
,
int i = 123;
object box = i;
if (box is int) {
Console.Write("Box int");
}

Box int.
, .
object,

object. ,
struct Point
{
public int x, y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
}


Point p = new Point(10, 10);
object box = p;
p.x = 20;
Console.Write(((Point)box).x);

170

4.

10, ,
p box, p. Point class,
20, p box .

, . , ,
, : .

4.3.2.
- -. :
object -.
System.ValueType -.
--,
.
-, .
System.Enum .
System.Enum -,
.

, .
, , . ,
.

, -
,
- ( 6.2.6).
-- ,
-, .

int double,
int double

4.3.

171

int. ,
,
.
, , Convert.

- -,
null, (wrapped) , .
, , 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.

C# , CLI. ,
:
object o = System.DayOfWeek.Sunday;
int i = (int) o;

.NET, ,
C#.


, null, --
-.
, System.InvalidCastException.

,
, (


172

4.

!). , , out ref


, .

4.4.
,

-. - (< >) . ,
-, . , .
-typeof ( 7.6.11).
( 7.6.2) ( 7.6.4).
----, . ,
,
. ,
:
namespace Widgets
{
class Queue {...}
class Queue<TElement> {...}
}
namespace MyApplication
{
using Widgets;
class X
{
Queue q1;
Queue<int> q2;
}

// Widgets.Queue
// Widgets.Queue

- , . ,
,
, ( 10.3.8.6):
class Outer<T>
{
public class Inner {...}
public Inner i;
}

// i - Outer<T>.Inner

4.4.

173


- ( 18.2).

4.4.1. -
.
--:
< -

>

-:
-
- , -
-:

( 18) - .
- - ( 10.1.5).

4.4.2.
(open) (closed) . , -. :
- .
,
.
,
- . ,
- ()
.
, .
,
, ,
-
. -
.
,
.

, .
, , .
,
- .

174

4.

4.4.3.
(unbound)
. (bound) .

, , .

,
. ,
,
. , , typeof ( 7.6.11).

4.4.4.
- , ( 10.1.5).
where A, -,
:
, , C -,
-.
, A C
:
 ( 6.1.1).
 ( 6.1.6).
 ( 6.1.7), , A
-.
 , - A C.
(class), A :
 A , , . System.ValueType,
System.Enum.
 A -, , ( 10.1.5).
- (struct), A :

4.5. -

175

 A , .
System.ValueType, System.Enum , .
 A -, - ( 10.1.5).
new(), A
.
, :
 A -, - ( 4.1.2).
 A -, ( 10.1.5).
 A -, - ( 10.1.5).
 A , .
 A abstract ( 10.11.4).
- , .
- , .
- T D , T ,
B<T>. E, , ,
List<T> IEnumerable T.
class B<T> where T: IEnumerable {...}
class D<T>: B<T> where T: IEnumerable {...}
class E<T>: B<List<T>> {...}

4.5. -
- , - , .
-:

- -, -
, :
- , ( 10.2.4) ( 13.1.3).
(member lookup) -
, .
. 6.1.10 6.2.6.

176

4.

null , -,
, , -
( 6.1.10).
default ( 7.6.13). , , -,
null == != ( 7.10.6),
- -.
new ( 7.6.10.6) - - -
- ( 10.1.5).
- .
- ( 7.6.4)
( 3.8)
.
- ( 18.2).
- .
-
, -
. , , -,
( 4.4.2).
, -,
, - .

4.6.
, .
System.Linq.Expressions.Expression<D>, D
.
Expression<D>.
D,
Expression<D>. ,
,
.


.
D, Expression<D> ,
D, .
, . -

4.7. dynamic

177

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>,

.
:
. , ,
, ,
. ,
.
Expression<D> Compile,
D:
Func<int,int> del2 = exp.Compile();

,
. , del del2 , :
int i1 = del(1);
int i2 = del2(1);

i1 i2 2.

4.7. dynamic
dynamic C#.
, 7.2.2.
dynamic object, :
dynamic
( 7.2.2).
( 7.5.2),
object, dynamic, dynamic.
:
object
dynamic, , , dynamic object.
object
dynamic.

178

4.

, dynamic object,
.
dynamic object.
dynamic .

dynamic , , . var
, : , -, -
. .


, var
, dynamic .
var : ,
. dynamic, , : ,
, ;
,
( , ). dynamic
( this) , , -,
, .


dynamic
object. : , object,
dynamic.


, - :
class Foo {
public static dynamic DoFoo() {...}
}

, , DoFoo (
) ,
static dynamic,
.

. ,
, . C# ,
, C# , , , .

++ --.
( 5.3),
.
,
.
.
.
,

.

5.1.
C# : , , , -, -,
.
.

class A
{
public static int x;
int y;
void F(int[] v, int a, ref int b, out int c)
{
int i = 1;
c = a + b++;
}
}

x , y , v[0] ,
a -, b -, c , i .

180

5.


, , x
. , ; , .
:
class ASimpleExampleClass
{
public static int staticMember;
int memberVariable;
void ExampleFunction(
int[] arrayOfIntsParam,
int simpleParam,
ref int refParam,
out int OutParam)
{
int tempVariable = 1;
outParam = simpleParam + refParam
}
}

, ,
.

5.1.1.
, static, .
( 10.12) , , .
( 5.2) .
,
.

5.1.2.
, static, .

5.1.2.1.

,
( ).

5.1.

181


( 5.2) .
,
.

5.1.2.2.
,
, . , ,
.
. ,
, ,
.

, .
, ,
. .

5.1.3.

, .

( 5.2) .
,
.

5.1.4. -
, ref out, .
-
(, , ) , ,
, . -
.
, - , ( 7.15),
,

182

5.

, ,
.
, .

5.1.5. -
, ref, .
- . ,
- , ,
. , - , .
- . ,
5.1.6.
( 5.3),
-
.
-
.

this -
( 7.6.7).

, - , - .
,
. ;

, .


.
, ,
. ,
, , ,
.
.

5.1.

183


: (input only).
, , C#, ,
.
; ,
, . C# .

5.1.6.
, out, .
. , ,
. ,
, .
. ,
5.1.5.
,

.

, , ,
.
.
( 5.3) .
this
( 7.6.7).


(tuple type).
.NET 4.

5.1.7.
--, , -for, -switch using, -foreach, catch- -try.

184

5.

, .
, -for, -switch,
-using, -foreach catch- -try, , . (
, ,
.)
( 7.15.5.1),
, , ,
, ,
.

, , .
, , ,
, .

, -for, -switch, -using,


-foreach catch- -try ,
-, , .
, --,
.
, ,
--, . -- -;
( 5.3.3.4).

,
: ,
.
. CLR , .

. C# ,
, ,
; , , .

5.2.

185

,
--, , --, .
( 8.5.1),
-- .
, -foreach catch-, .
.
, , ,
, .
, , , ,
.
, ,

( 3.9).

C , :
void F() {
int x = ...;
...
Foo(x);
// x?
...
}

C#, ,
, . :
void F() {
...
int x = ...;
Foo(x); // -, x...
...
}

, (locality of
reference), .

5.2.
:
.
.
.

186

5.


:
- , - ( 4.1.2).
null.

, .
.

5.3.

, , ( 5.3.3), , .
, :
( 5.3.1) .
( 5.3.2)
, ,
, :
 ( 7.17.1), .
 ( 7.6.5) ( 7.6.10.1), .
 ( 8.5.1),
.
, , 5.3.15.3.3.
, .
, :
,
.
,
.
:
,
. ,

5.3.

187

. , :
 .
 .

.
,
-. ,
.
, ( return ).
,
,
,
, .
this
, .

5.3.1.
:
.
.
.
.
-.
-.
, catch foreach.

5.3.2.
:
.
, this
.
, catch foreach.

188

5.

5.3.3.

, ,
, .
, . v (definite assignment
state, DAS ) :
.
( 8.1) .

.
.
.
DAS v :
. ,
v .
.
bool , ,
( ) :
 true. , v , true,
,
false.
 false. , v , false,
,
true.
, v
.

5.3.3.1.
v .
v .

.
?
.

5.3.

189

, . ,
. ,
; , .
, .
, ,
. ,
.

DAS v
v , .
, v ,
v .
, ( 8.1).
DAS v checked, unchecked, if, while, do,
for, foreach, lock, using switch
v , .
, v ,
v .
v . ,
( 8.1).

5.3.3.2. , checked unchecked


DAS v
( , ) , DAS
v checked unchecked.

5.3.3.3. -
- stmt, expr :
v DAS expr, stmt.
v expr,
stmt.

5.3.3.4.
, v DAS
stmt, stmt.
stmt , DAS v , stmt (
).

190

5.

5.3.3.5.
if stmt :
if ( expr ) then-stmt else else-stmt

v DAS expr, stmt.


v expr,
then-stmt else-stmt, , else, stmt.
v true expr,
then-stmt
else-stmt, , else, stmt.
v false
expr, else-stmt
then-stmt. stmt
, then-stmt.
v then-stmt else-stmt ,
else, stmt.

5.3.3.6.
switch stmt expr:
DAS v expr , stmt.
DAS v switch ,
DAS v expr.

, , switch ( bool, byte
), ,
switch. :
int x;
bool b = B();
switch(b) {
case true : x = 1; break;
case false: x = 2; break;
}
Console.WriteLine(x);

// : x


default, .

5.3.3.7. while
while stmt :
while ( expr ) while-body

5.3.

191

v DAS expr, stmt.


v expr,
-while stmt.
v true
expr, while, stmt.
v false
expr, stmt,
-while.

5.3.3.8. do
do stmt :
do do-body while ( expr ) ;

v DAS stmt -do, stmt.


v DAS expr, -do.
v expr,
stmt.
v false
expr, stmt.

5.3.3.9. for
DAS for :
for ( -for ; -for ; -for ) -

, :
{
-for ;
while (-for ) {
- ;
-for ;
}
}

-for for , DAS ,


-for true.

5.3.3.10. break, continue goto


DAS v , break, continue,
goto , DAS v .

5.3.3.11. throw
stmt :
throw expr ;

DAS v expr , DAS v stmt.

192

5.

5.3.3.12. return
stmt :
return expr ;

DAS v expr , DAS v stmt.


v ,
:
 expr.
 nally try-nally try-catchnally, return.
stmt :
return ;

v ,
:
 stmt.
 nally try-nally try-catchnally, return.

5.3.3.13. try-catch

,
(throw, catch, goto, nally) , ,
. . try/nally
, , , .

stmt :
try -try
catch(...) -catch-1
...
catch(...) -catch-n

DAS v -try , DAS v stmt.


DAS v -catch-i ( i) , DAS v stmt.
stmt v
, v -try catch-i ( i 1 n).

5.3.3.14. try-finally
try stmt :
try -try nally -nally

5.3.

193

DAS v -try , DAS v stmt.


DAS v -finally , DAS v stmt.
v stmt
, :
 v -try.
 v -finally.
(, goto)
-try -try, v
, v -finally. (
: v
, .)

5.3.3.15. try-catch-finally
DAS try-catch-nally :
try -try
catch(...) -catch-1
...
catch(...) -catch-n
nally -nally

, try-nally,
try-catch:
try {
try -try
catch(...) -catch-1
...
catch(...) -catch-n
}
nally -nally

, try ( 8.10) DAS.


class A
{
static void F()
{
int i, j;
try
{
goto LABEL;
// i, j
i = 1;
// i
}
catch
{
// i, j
i = 3;
// i
}

194

5.

nally
{
// i, j
j = 5;
// j
}
// i j
LABEL: ;
// j
}
}

5.3.3.16. foreach
foreach stmt :
foreach ( in expr ) -

DAS v expr , DAS v stmt.


DAS v - stmt
, DAS v expr.

5.3.3.17. using
using stmt :
using ( - ) -

DAS v - , v
stmt.
DAS v - , DAS v
-.

5.3.3.18. lock
lock stmt :
lock ( expr ) -

DAS v expr , DAS v stmt.


DAS v - , DAS v
expr.

5.3.3.19. yield
yield return stmt :
yield return expr ;

DAS v expr , DAS v stmt.


DAS v stmt , DAS v expr.
yield break DAS .

5.3.

195

5.3.3.20.
: ( 7.6.1.), ( 7.6.2),
( 7.6.4), ( 7.6.8), typeof ( 7.6.11)
( 7.6.13).
DAS v , DAS v .

5.3.3.21.

: , ( 7.6.3),
( 7.6.6), ( 7.6),
( 7.6.9, 7.7.5), ( 7.7.6), +, -, ~, *, +, -, *,
/, %, <<, >>, <, <=, >, >=, ==, !=, is, as, &, |, ^ ( 7.87.11),
( 7.17.2); checked unchecked ( 7.6.12);
( 7.6.10).
, ,
, . , % , . ,
. expr
expr1, expr2, ..., exprn, :
DAS v expr1 , DAS expr.
DAS v expri , DAS expri-1.
DAS v expr , DAS exprn.

5.3.3.22.

expr
- ( arg1 , arg2 , ... , argn )


new ( arg1 , arg2 , ... , argn )

DAS v - ,
v expr.
DAS v arg1 , v expr.
argi DAS v arg1 , ref out.

196

5.

argi, i 1, DAS v arg1


, v argi 1.
v out ( out v) , v expr .
DAS v expr , DAS v argn.
( 7.6.10.4),
( 7.6.10.2), ( 7.6.10.3) ( 7.6.10.6) DAS ,
.

5.3.3.23.
expr w = expr-rhs:
DAS v expr-rhs , DAS v expr.
w , v, v expr
. DAS v expr , DAS v
expr-rhs.

5.3.3.24. &&-
expr expr-1 && expr-2:
DAS v expr-1 , DAS v expr.
v expr-2 , v expr-1
,
true. .
DAS v expr :
 v expr-1 , v expr
.
 , v expr-2 v expr-1 false, v expr
.
 , v expr-2 true, v expr
true.
 , v expr-1 false
v expr-2
false, v expr false.
 v expr .

class A
{
static void F(int x, int y)

5.3.

197

{
int i;
if (x >= 0 && (i = y) >= 0)
{
// i
}
else
{
// i
}
// i
}
}

i if, . if F i
, (i = y)
. , i
, x >= 0
false, i .

5.3.3.25. ||
expr expr-1 || expr-2:
DAS v expr-1 , DAS v expr.
v expr-2 , v expr-1
,
false. .
DAS v expr :
 v expr-1 , v expr
.
 , v expr-2 v expr-1 false, v expr
.
 , DAS v expr-1 false v expr-2 false, DAS v expr
false.
 v expr .

class A
{
static void F(int x, int y)
{
int i;
if (x >= 0 && (i = y) >= 0)

198

5.

{
// i
}
else
{
// i
}
// i
}
}

i if, . if G i
, (i = y)
. , i
, x >= 0
true, i .

5.3.3.26. !
expr ! expr-operand:
DAS v expr :
 v expr-operand , v expr .
 v expr-operand , v expr
.
 v expr-operand
false, v expr true.
 v expr-operand
true, v expr false.

5.3.3.27. ??
expr expr-1 ?? expr-2:
DAS v expr-1 , DAS v expr.
DAS v expr-2 , DAS v expr-1.
DAS v expr :
 expr-1 ( 7.19)
null, v expr , v expr-2.
DAS v expr , DAS v expr-1.

5.3.3.28. ?:
expr expr-cond ? expr-true : expr-false:
DAS v expr-cond , v expr.

5.3.

199

v expr-true
, v expr-cond true.
v expr-false
, v expr-cond false.
DAS v expr :
 expr-cond ( 7.19) true, v expr , v
expr-true.
 , expr-cond ( 7.19)
false, v expr , v expr-false.
 , v expr-true
expr-false v , v expr
.
 v expr .

5.3.3.29.
- -- expr body (
):
DAS v body , v expr.
DAS
.
DAS v expr , v 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);
}

200

5.

, n n
.

5.4.
-- ,
. -- ,
, , ,
.
--:

C C++ -- lvalue.

5.5.
(): bool, char, byte,
sbyte, short, ushort, uint, int, oat . ,
. , long, ulong, double decimal,
,
. ,
, , -- ,
.

, , ,
, . , :
long 0x0123456776543210 ,
, , 0x0123456700000000. ,
: 32 , 32 (
). C# , 32- ,
.


. , , ,
( ) , , ,
.

.
, ,
. ,
, . , int
long , ,
.
, long int, , .
int a = 123;
long b = a;
int c = (int)b;

// int long
// long int

, (.
6.4).

,
,
. ,
.
,
, . , -
,
.
, ,
, . int double
, . double, ,
.
,
.

202

6.

6.1.
:
.
.
.
.
null.
.
.
.
.
.
.
.
,
( 7.5.4), ( 7.7.6), ( 7.17).

. .
object dynamic . dynamic ( 6.1.8 6.2.6) , dynamic ( 4.7).

6.1.1.
.
, , , ,
.
object dynamic ,
object dynamic
. , , dynamic object.

. T
S, S T.
,

6.1.

203

. , List<object[]> List<dynamic[]>
List<object[]>.


dynamic. , ,
List<object> List<dynamic>, .
object dynamic.
dynamic object
. ,
List<string> List<dynamic> .

6.1.2.


:
sbyte short, int, long, oat, double decimal.
byte short, ushort, int, uint, long, ulong, oat, double decimal.
short int, long, oat, double decimal.
ushort int, uint, long, ulong, oat, double decimal.
int long, oat, double decimal.
uint long, ulong, oat, double decimal.
long oat, double decimal.
ulong oat, double decimal.
char ushort, int, uint, long, ulong, oat, double decimal.
oat double.
int, uint, long ulong oat long ulong
double ,
.
- .
char,
char.

6.1.3.
- 0 , -,
. ( 4.1.10).

204

6.


, .
CLR , , [Flags] None .
, ,
.
4.8 : ,
.NET (Framework Design
Guidelines).
# Microsoft , .
.Net .


, enum 0 . , , 0
.

6.1.4.


, .
S T
:
S? T?.
S T?.
,
S T, :
S? T?:
 null ( HasValue false), null T?.
 S? S,
S T (wrapping, 4.1.10)
T T?.
S T?, S T T T?.

6.1.

205

6.1.5. null
null .
null ( 4.1.10) .

6.1.6.
:
- object dynamic.
S T, S T.
S T, S T.
S T, S
T.
S, SE, T, TE, :
 S T . , S T
;
 SE TE ;
 SE TE.

, SE , , .


C# Microsoft , SE TE
, , SE TE . ,
-.
, - T
( 10.1.5), :
T[] Foo<T,S>(S[] x) where S: class, T
{
return x;
}


CLI #.
. ,


206

6.

Turtle Animals,
Giraffes , Animals.
Turtle , Turtle Giraffes. ,
Turtle Animals,
. .

. CLI
, . ,

, ,
. , Giraffes
Animals
, , .
,
#
. CLR , int[]
uint[].

System.Array , .
S[] System.Collections.
Generic.IList<T> ,
S T.
System.Delegate ,
.
null .
- - T,

0, 0 T.

, L i s t < o b j e c t >
ICollection<dynamic>.

- T,
0,
0 , ( 13.1.3.2) .

, L i s t < s t r i n g >
IEnumerable<object>.

6.1.

207

, -,
. 6.1.10.
, , .
. ,
,
, .

6.1.7.
-
. -- object
dynamic, System.ValueType , -. System.
Enum.
,
-
.
- I,
I0 I0 I.
- I,
I0 I0
( 13.1.3.2) I.
- object -
.
System.ValueType,
( 11.3.2).
:
null ( HasValue false), null .
,
.
6.3.1.

6.1.8. dynamic
, dynamic,
. ( 7.2.2).
,
;
, .

208

6.


, dynamic
. :
class A
{
static void Foo(string x) { }
static void Foo(dynamic x) { }
static void Main()
{
Foo(null);
}
}

Foo(string x) , Foo(dynamic x),


string dynamic, dynamic string.

,
, 6.1,
.
, .
. ,
object, .
dynamic:
object o = "object"
dynamic d = "dynamic";
string s1 = o;
// :
//
string s2 = d;
//
int i = d;
// ,
// :

s2 i
dynamic,
.
d (string)
. string , int .

6.1.9.

:
- int ( 7.13) sbyte, byte, short, ushort, uint ulong, .

6.1.

209


C# , Java. Java . , C#,
Java- , 10
byte:
void MethodTakingByte(byte b) { ... }
...
//
MethodTakingByte(10);

- long ulong,
- .

6.1.10. -
- T:
, ,
, . ,
-, ,
.
I , I . ,
-, ,
.
- U, U ( 10.1.5). , U -, U ,
. , -,
.
.
null , .
S,
So So S. ,
So.
I, Io Io
I ( 13.1.3.2). , -, .
.
( 10.1.5), ,
, ( 6.1.6).
, ( 6.1.7).

210

6.

6.1.11.

,
(). 6.4.4.

6.1.12.

,
.
6.5, 6.6.

6.2.
:
.
.
.
.
.
.
.
dynamic.
.
( 7.7.6).
. ,
.
,
. .

6.2.1.
-
- ,
( 6.1.2):

6.2.

211

sbyte byte, ushort, uint, ulong char.


byte sbyte char.
short sbyte, byte, ushort, uint, ulong char.
ushort sbyte, byte, short char.
int sbyte, byte, short, ushort, uint, ulong char.
uint sbyte, byte, short, ushort, int char.
long sbyte, byte, short, ushort, int, uint, ulong char.
ulong sbyte, byte, short, ushort, int, uint, long char.
char sbyte, byte short.
oat sbyte, byte, short, ushort, int, uint, long, ulong, char decimal.
double sbyte, byte, short, ushort, int, uint, long, ulong, char, oat
decimal.
decimal sbyte, byte, short, ushort, int, uint, long, ulong, char, oat
double.
, ,
- - ( 7.7.6).
. :

( 7.6.12),
:
 ,
,
System.OverowException,
.

:
 , , .

.
 , ,
, ,
.
.
 ,
, .
decimal
( ),

212

6.

.
, System.OverowException.
oat double
( 7.6.12) :
 :
 NaN , System.OverowException.
 ( ). ,
.
 System.OverowException.

:
 NaN ,
.
 (
).
,
.
 .
double oat double
oat. double , oat, +0,
0. double , oat, +, . double
NaN, NaN.
oat double decimal
decimal ( 4.1.7).
, decimal,
. NaN,
, decimal,
System.OverowException.
decimal oat double
oat double. , .


, . ,

6.2.

213

, (int) 3,9 3, 4. #
,
. Microsoft Framework.NET Convert,
, ToInt32.

6.2.2.
:
sbyte, byte, short, ushort, int, uint, long, ulong, char, oat, double
decimal .
sbyte, byte, short, ushort, int, uint, long, ulong,
char, oat, double decimal.
.

,
. , int byte ( 6.2.1)
int byte, byte
( 6.1.2) byte int.

6.2.3.
.
-
S - ( 6.1.16.1.3, 6.2.2)
:
S? ?.
S ?.
S? .
,
S , :
S? ?:
 null ( HasValue false), null ?.
 ,
S? S, S ,
?.
S ? S ,
?.

214

6.

S? S? S,
S .
null, , .

6.2.4.
:
object dynamic .
S , S T.
S , S .
S ,
S.
S , S .
S SE, E,
:
 S ,
S .
 SE, E .
 SE E.

, C# Microsoft , SE TE
, , S T
. 6.1.6.

System.Array , , .
S[] System.Collection.Generic.IList<T>
,
S .
System.Collection.Generic.IList<T>
S[],
S T.
System.Delegate .
, 0, 0 .

6.2.

215

,
0, 0
, ( 13.1.3.2).
D<S1.Sn> D<T1.Tn>, D<X1.Xn> ,
D<S1.Sn> D<T1.Tn> ,
- Xi D :
 Xi , Si Ti.
 Xi , Si Ti.
 Xi , Si Ti ,
.

, Action<S>, S
Action<object>. Action<object>
Action<T>, ,
Action<S> Action<T> S .

, - . 6.2.6.

.
, null,
, ,

( 6.1.6), ( 6.1.7).
, System.
InvalidCastException.
, , ,
. ,
, ,
.

6.2.5.
. object, dynamic, System.ValueType
--,
--,
. ,
System.Enum .

216

6.

-,
--,
.
I - S ,
I0, I0 I.
I - S, I0, , I0,
I, ( 13.1.3.2).
, -.
. null -
null . System.ValueType , ( 11.3.2).
4.3.2.

6.2.6. dynamic
dynamic
.
dynamic , ,
; ,
.
, object, .
, :
class C
{
int i;
public C(int i) { this.i = i; }
public static explicit operator C(string s)
{
return new C(int.Parse(s));
}
}

dynamic:
object o = "1";
dynamic d = "2";
var c1 = (C)o;
var c2 = (C)d;

//
//
//
//

,

,


. ,
1 o . d

6.2.

217

dynamic,
,
string .

6.2.7. -
- :

. , -,
. .
. ,
-, .
.
I,
I. , -,
, .
.
- U , U ( 10.1.5). , U -, U ,
. , -,
.
.
,
( 6.2.4).
,
( 6.2.5).

- :
static T Cast<T>(object value) { return (T)value; }

,
.
string s = Cast<string>("s"); // ,
int i = Cast<int>(3); // ,
long l = Cast<long>(3); // InvalidCastException:
//

System.Linq.Enumerable.Cast,
LINQ Microsoft.

218

6.

, , - , .
,
. , ,
:
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; // , T long
}
}

, X<int>.F(7)
,
int long.

6.2.8.

,
. 6.4.5.

, ,
, , .
,
.
. , X Y
Y Z, X
Z .

6.3.

219

. , Foo Foo? decimal. Foo int?


Foo Foo?, Foo? decimal, decimal int,
, , int int?.

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.10).
, .

6.3.2.

,
. ,
, ,
.

, double decimal
decimal double .

220

6.

6.4. ,

C# ,
.
( 10.10.3) .

,
. , , ,
.
, , .
,
.

6.4.1. ,

# . ,
.
S , S , S0 0 , S0 0 S
.
S , :
S0 0 .
S0 0 ,
.
S0, 0 .
S T T S, , .
10.10.3.

6.4.2. (lifted)
- S
- , (lifted)
S? ?. S? S,

6.4. ,

221

S , ?,
, null S? null ? .

, .

, .

6.4.3.


.
.
:
,
. :
( , , , ,
). ,
-,
.
,
.
( 6.3)
,
.
. ,
, .

.
.
,
:
,
.

222

6.

.
, , ,
, .
. , S
S
.
. :
( 6.3.1)
, , , ,
, .
, . ,
, , .
,
.
,
. , ,
.
, .

,
.
.

6.4.4.

S
:
S0 0. S , S0 0 , S0 0 S .
D, . S0 ( S0

6.4. ,

223

), S0 ( S0 ) 0 ( 0
).
U.
, D, , S,
, . U , ,
.
Sx U:
 - U S,
Sx S.
 Sx
U. , , .
x U:
 - U ,
.
 , U.
, ,
.
:
 U
Sx , .
 , U S , .
 , .
, :
 S Sx, S
Sx.
 Sx .
 ,
.

6.4.5.

S
:

224

6.

S0 0. S , S0 0 , S0 0 S .
D,
. S0 ( S0 ),
S0 ( S0 ), 0 ( 0 )
0 ( 0 ).
U.
,
D, ,
S, , . U
, , .
Sx U:
 - U S,
Sx S.
 , - U
, S, Sx
U.
, ,
.
 Sx
U. , , .
U:
 - U ,
.
 , - U
, ,
U.
, , .
 U. , ,
.
:
 U
Sx , .
 , U S , .
 , .

6.5.

225

, :
 S S x, S
Sx.

Sx .
 ,
.

6.5.
-- -
( 7.15). ,
.
D F :
F --, D F
.
F --, D ,
out.
F ,
D , F.

: D params, F
.

F , D
ref out.
D void, F ,
F , D,
F ( 7), - ( 8.6).
D void, F , F ,
D, F ( 8.2), return .
D void, F ,
F , D,

226

6.

F ( 7),
D.
D void, F ,
F , D, F ( 8.2) , return
, D.
Expression<D> F,
D F.

-
(--
). -
.

, , . ,
:
.
.

, , --, - , ref out, ---,
, , ,
.
dynamic , as,
object.

F<A, R>, ,
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; //

, .

6.5.

227


Func<int, double>, int, +1 , int.
Func<int, double>, +1 int
double.
, double, +1 double,
int.
. 7.5.

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;
}
static void F(double[] a, double[] b)
{
a = Apply(a, (double x) => Math.Sin(x));
b = Apply(b, (double y) => Math.Sin(y));
...
}
}

228

6.

()
, . ,

.

,
, .
:
button.Click += (sender, args) => Console.WriteLine("Clicked");
button.Click -= (sender, args) => Console.WriteLine("Clicked");

. ? Click , ,
. ,
.


C# .

6.5.2.


( 4.6). ,
,
. ,
, .

,
.

6.5.3.

C#.
, C# Microsoft. ,
.
,
. -

6.5.

229

, 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);
}
static void __Method1()
{
Console.WriteLine("test");
}
}


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);
}
}

230

6.

:
class Test
{
void F()
{
int y = 123;
D d = () => { Console.WriteLine(y); };
}
}

,
, . .
( 7.15.5.2) ,

. , :
class Test
{
void F()
{
__Locals1 __locals1 = new __Locals1();
__locals1.y = 123;
D d = new D(__locals1.__Method1);
}
class __Locals1
{
public int y;
public void __Method1()
{
Console.WriteLine(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); };
}
}
}

, , , .

6.5.

231

__Locals2, ,
z ,
__Locals1. __Locals1, , y ,
this .

__Locals2,
.

F() D, ,
, :
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);
}
}
}

, , .
,
. ,

.

232

6.


, , , :
,
,
.
, expensive cheap ,
:
D longlived = ()=>cheap;
D shortlived = ()=>expensive;

expensive , , longlived, , expensive longlived .


, .

6.6.
( 6.1) ( 7.1)
. D , . D ,
, , ,
( 7.5.3.1) ,
- D, .
D , . , D
, .

, ,
, ,
,
, . , ,
- ,
. ,
, ,
, .

(
7.6.5.1) () :
 , ,
(ref out), - D.

6.6.

233

 , ( 7.5.3.1), ,
.
7.6.5.1 ,
. M, , D, .
M ( 15.2) D,
.
M , ,
E, .
M ,
,
.
D,
, .
,
, 7.6.5.1 ,
E(A) ( 7.6.5.2).

.

, ,
-, - ( 10.1.5), .
, System.Nullable<T>, ,
.

.
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; // : ,
}
}

234

6.

d1 F
D1.
d2 , , ()
() .
d3 ,
, .
d4 ,
.
d5 ,
.
, , .

object obj = new EventHandler(myDialog.OkClick);

-.
object obj = (EventHandler)myDialog.OkClick;

. 7.5.

:
, ,
,
, , E:
 , ,
.
 -, ,
, .
null, System.NullReferenceException
.
 -, ( 4.3.1) object,
.
,
null.
D . , System.
OutOfMemoryException .

6.6.

235


, ,
. C# Microsoft , ,
, this.
, , , .

,
, ,
.

.
,
.

7.1.
:
. .
. , .

-
.

.
-- ( 7.6.4). ,
, .
. - ( 7.6.4) as ( 7.10.11), is ( 7.10.10) typeof ( 7.6.11).
, , .

-, .
, .

, ,
( 7.4). -

7.1.

237


. ,
,
this ( 7.6.7). -
(invocation-expression) ( 7.6.5), - (
7.6.10.5) is ( 6.6). ,
, .

, is,
. is false, .


is .

null.
.
. .

, null ,
. ,
.

.
, . ,
. ( get set)
, , this ( 7.6.7).
. ,
. ,
.
+= = ( 7.167.3). ,
,
.
.
, . ,

238

7.

. ( get set),
, this ( 7.6.7),
.
. ,
, void. ,
, ( 8.6).
,
, . , ,
.

get (get-accessor) set (set-accessor).
: ,
set-accessor ( 7.17.1).
get-accessor ( 7.1.1).

7.1.1.
, ,
. , , , , .
, ,
, :
,
, . ( 5.3) , ;
.

get. , .
( 7.5.4),
.
get . ,
. ( 7.5.4) ,
,
.

7.2.

239


get, ,
.

7.2.

, (, , ),
. ,
.
.
C# , . , .
.
( dynamic),
,
, .
, . .
,
. ,
,
.
C# :
: e.M.
: e.M(e1,...,en).

: e(e1,...,en).
: e[e1,...,en].
: new C(e1,...,en).
: +, -, !, ~, ++, --, true, false.
: +, -, *, /, %, &, &&, |, ||, ??, ^, <<, >>, ==,!=,
>, <, >=, <=.
: =, +=, =, *=, /=, %=, &=, |=, ^=, <<=, >>=.
.
, C#
, , -

240

7.

, . , , ,
.

7.2.1.
, .
, , , .

:
object o = 5;
dynamic d = 5;
Console.WriteLine(5); // Console.WriteLine(int)
Console.WriteLine(o); // Console.WriteLine(object)
Console.WriteLine(d); // Console.WriteLine(int)

: Console.WriteLine .
, .
: Console.WriteLine .
,
dynamic. ,
.

.
. :
var i = 5;
Console.WriteLine(i);

// i int ( )
// Console.WriteLine(int)

, i . i .

7.2.2.
C# , ,
C#. ,
, ,
.

7.2.

241


,
, : (1) ,
IronPython, IronRuby, JScript . .; (2) expando,

, Internet Explorer (Internet
Explorer Document Object Model) ,
(markup-based object models), (3) COM,
Microsoft Office. C# ;
C# , JScript.

, , . ,
, ,
C#, .
, , ,
C#, .

Microsoft ,
IronPython DLR ,
.
DLR-,
C# ,
. , , .


, C#
, , .
, ,
, . ,
, , .

C# :
,
, ,
. C# (,


242

7.

), ,
:
. , ,
, ,
. ( ,
-) .

,
, ( ) ,
.

7.2.3.
, (
, , )
.
, :
, dynamic, ,
.

, dynamic
, , :
public class Base
{
public Base(dynamic parameter)
{
}
}
public class Derived : Base
{
public Derived(dynamic parameter) : base(parameter)
{
}
}

,
.

7.3.

243

, -,
, - .
.

7.3.
. , .
+, -, *,/ new. , , .
:
. (, x), (, x++).
. (, x + y).
. ?:. (c? x: y).

( 7.3.1).
. ,
F(i) + G(i++) * H(i) F i, ,
G i, , , H
i. .
.
,
( 7.3.2).

7.3.1.
,
, . ,
x + y * z x + (y * z), * , +.
. , -, + -; , +
, *, / %.

.

244

7.

7.6

7.7
7.8
7.8
7.9
7.10
7.10
7.11
7.11
7.11
7.11
7.12
7.12
7.14
7.17, 7.15

AND
XOR
OR
AND
OR

x.y
f(x) a[x] x++
x-- new
typeof default checked unchecked
+

!
~
++x
--x
(T)x
*
/
%
+ <<
>>
<
>
<=
>=
is
as
==
!=
&
^
|
&&
||
??
?:
= *= /= %= += -= <<= >>= &=
=>

delegate

^=

|=

, ,
, :
,
, , .
, x + y + z (x + y) + z.
, ,
. , x = y = z
x = (y = z).
. , x + y * z y z,
x, (x + y) * z x y,
z.

, . ,
. (
, ),
, .


, . , ,
, , ,

7.3.

245

, .
, , .


, ,
. .
, , ,
. , ,
y z , x. , ,
x .

7.3.2.
,
.
, ,
( 10.10). ,
, : ,
,
, 7.3.3 7.3.4.
:
+

++

--

true

false

true false (, ,
7.3.1), , :
( 7.20), ,
( 7.14), ( 7.12).
:
+

&

<<

>>

==

!=

>

<

>

<

. ,
, =, &&, ||,
??, ?:, =>, checked, unchecked, new, typeof, default, as is.
, . , *
, *= . 7.17.2.
, (=) .
.
(T)x ,
( 6.4).

246

7.

a[x]
. , , ( 10.9).
( ), .

. op . op
++ --. op
.

op x
x op
x op y


operator op(x)
operator op(x)
operator op(x, y)

, , ,
, () . ,
, .
, , , . , /
, , 7.3.1,
.
, , , , , ,
, . ,
operator == , bool.

, , ,
, .



. , ,
. ., , , , . ,
, -.

7.67.12 , -

7.3.

247

. :
,
, .

7.3.3.
op x x op, op ,
x X, :
, X
operator op(x), 7.3.5.
,
.
operator op, .
( 7.6 7.7).
7.5.3

list (x), .
, .

7.3.4.

x op y, op , x
X y Y,
:
, X Y
operator op(x, y).
X Y,
7.3.5. X Y
,
.
,
.
operator op, .
( 7.87.12).
7.5.3

248

7.

list (x, y), . , .

7.3.5.

T operator op(A), op
A ,
T operator op(A), :
T0. T , T0 ;
T T0.
operator op T0
, ( 7.5.3.1) A, T0.
, T0 object,
.
, T0
T0 T0, T0 -.

7.3.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);
oat operator *(oat x, oat y);
double operator *(double x, double y);
decimal operator *(decimal x, decimal y);

,
,
. , b * s, b byte, s
short,

7.3.

249

operator *(int, int). , b s int


int. i * d, i int d
double,
operator *(double, double).

7.3.6.1.

+, ~. sbyte, byte, short, ushort char int. ,

uint long.

7.3.6.2.

+, , *, /, %, &, |, ^, ==, !=, >, <, >= <=.
, .
, , :
decimal,
decimal, , oat double, .
, double,
double.
, oat,
oat.
, ulong,
ulong, , sbyte, short, int long,
.
, long,
long.
, uint
sbyte, short, int, long.
, uint,
uint.
int.
, ,
decimal double oat. , decimal double oat.
, ulong,
. ,
, ulong, .

250

7.


,
.
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);
}


, , 8-
16- , short, ushort, sbyte byte.
8-
16- :
byte a = 1, b = 2;
byte c = a + b;

//

a b int,
byte.

7.3.7.
(lifted)
, , .
,
, :

+

++

--

, .
? .
, . ,
.

+

&

<<

>>

7.4.

251

, .
? . ,
( & |,
bool?, 7.11.3).
,
.

==

!=

,
bool.
?
. , .
,
, bool.

<

>

<=

>=

,
bool.
? .
false, . , bool.

,
.

7.4.
(member lookup) ,
.
- ( 7.6.2) -- ( 7.6.4) .

- - ( 7.6.5.1),
(invoked).
,
( 15) dynamic ( 4.7),
(invocable).

252

7.

,
- .
, ,
-.

, , , , , () , . -
,
, , .
, ,
.

N K - T
:
-, N:
 T -, N , T ( 10.1.5),
N object.
 ( 3.5)
N T N object.
T , -, 10.3.2. , override, .
, K , ,
-, . K , - . , K
, , -, ,
( 7.5.2) -.
, , - .

, T
- . ,
Count() ,
Count int.

, . S.M , S , M,
:

7.4.

253

 M , , ,
, ,
S.
 M , S, ,
-, M.
 M , , , S.

, -
. , ,
, .

, .
, T -
, object, ( 10.1.5).
S.M , S , M,
, S , object:
 M , , , , , , , .
 M , , , , ,
, M, ,
.
,
:
 , ,
.
 , ,
.
 .

Microsoft C# .
, , , CS0467, , , ,
.
, COM.

254

7.

, - , , (
),
,
.
.
, 13.2.5.

7.4.1.
T :
T object, T .
T , T System.
Enum, System.ValueType object.
T , T System.
ValueType object.
T , T T,
object.
T , T T
object.

, ToString()
System.Object .
, System.
Object, .

T -, T System.
Array object.
T -, T System.
Delegate object.

7.5.
,
. . C#
:
.
.

7.5.

255

.
.
, .
.
.
.
(
), , ,
. .
( 7.5.1) .
, .
7.5.2.
, ,
, . 7.5.3.
, ,
,
7.5.4.
,
, ,
. e, x, y value
, , T , , F , P
.

F(x, y)

T.F(x, y)

e.F(x, y)


F . (x, y).
(static), this

T. , . (x, y)

, ,
e. (static), .
e (x, y)

256

7.

P = value

T.P
T.P = value

e.P

e.P = value

E += value

E -= value

T.E += value

T.E -= value

e.E += value

e.E -= value

e[x, y]

get
. P
, . P
(static), this
set P
(value). P
,
. P (static), this
get P T
set P T (value). P
(static) P
,
get P ,
, e, e. P (static)
P ,

set P ,
, e, e (value), P (static) P
,
add E
. E , this.
remove E
. E , this
add E T. E ,

remove E
T. E ,
add E ,
, e, e. E ,

remove E ,
, e,
e. E ,


, , e.
get e
(x, y).
,

7.5.

e[x, y] = value

-x

x+y

new T(x, y)

257


, , e.
set e
(x, y, value).
,

,
(-) x. (x)

,
x y.
(x, y)

T.
(x, y)

7.5.1.
,
.
:
, ,
-, . ,
set,
, .

.

, get,
,
set.
,
+= -=.
, ,

.

258

7.

( 10.7), ( 10.8) , ( 10.10), -


( 10.6.1.1). ( 10.9)
- ( 10.6.1.1) - ( 10.6.1.4).
-
.
, ,
-:
-:

-
:
-opt
-:

-:

ref
--
out
--

- ,
. -,
-.
, .

, .
- :
, , - ( 10.6.1.1).
ref, -- ( 5.4);
, - ( 10.6.1.2).
( 5.3),
-.
out, -- ( 5.4);
, ( 10.6.1.3).
, .

7.5.1.1.
.
, ,
:

7.5.

259

, ,

;
.

(, , ) , , , void M(int x, int y), , , void M(int y, int x). M(y:1, x:2)
M(1, 2) M(2, 1)?
, , .


;
. ,
, .
.
, .
, .

:
- , , :
 ,
, .
 -,
, -, .
 -,
,
,
-.

.
 , set, , ,
value set.

260

7.

, get, . set, ,
, value set.
( )

.
,
.

7.5.1.2.

( 7.5.4)
:
-
( 6.1) . -
.
- ,
. , ,
-, ,
, .
, System.
ArrayTypeMismatchException.
,
- ( 10.6.1.4).
, , ( 7.5.3.1):
- , , -,
, ( 6.1) . - .
- , -, ,
( 6.1) -.
- ,

7.5.

261

,
, .
,
. , :
class Test
{
static void F(int x, int y = -1, int z = -2)
{
System.Console.WriteLine("x = {0}, y = {1}, z = {2}", x, y, z);
}
static void Main()
{
int i = 0;
F(i++, i++, i++);
F(z: i++, x: i++);
}
}

:
x = 0, y = 1, z = 2
x = 4, y = -1, z = 3

( 12.5)
A[] B[], , B A. , - -
, , ,
.
class Test
{
static void F(ref object x) {...}
static void Main()
{
object[] a = new object[10];
object[] b = new string[10];
F(ref a[0]);
//
F(ref b[1]);
// ArrayTypeMismatchException
}
}

F System.ArrayTypeMismatchException,
b string, object.
- , ,
( 7.6.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);

262

7.


F(10, 20, new object[] { });
F(10, 20, new object[] { 30, 40 });
F(10, 20, new object[] { 1, "hello", 3.0 });

, , - ,
.
, ,
. , .

7.5.2.
-, ,
. . , :
class Chooser
{
static Random rand = new Random();
public static T Choose<T>(T rst, T second)
{
return (rand.Next(2) == 0) ? rst : second;
}
}

Choose -:
int i = Chooser.Choose(5, 213);
string s = Chooser.Choose("foo", "bar");

// Choose<int>
// Choose<string>

- int string
.

, , . ,
, .
, ( -),
. ,
, .


, , , - .

7.5.

263

( 7.6.5.1) .

,
. ,
-
. , ,
-
. , .

. ,
- .

. (, .) :
- -,
?
,
, ,
.


, ,
, .
-,
.

- , . ,
:
Tr M<X1...Xn>(T1 x1 ... Tm xm)

M(E1 ...Em) - S1...Sn -


X1...Xn, , M<S1...Sn>(E1...Em).

, ref/
out.

264

7.

Xi Si,
.
. - , ,
.
,
.
.
. . 7.5.2.13,
7.5.2.14.

: ,
(HindleyMilner-style algorithm)?. :
(1) , , .
(2) .
,
.
- , , , .


, , , , (
) . ,
(DamasMilner algorithm) (1978) .
, , C#, C#
. ,
-, . , .

7.5.2.1.
Ei:
Ei , ( 7.5.2.7) Ei Ti.
, Ei U xi -,
(lower-bound inference) U Ti.

7.5.

265

Ei U xi ref out, U to Ti.


.

7.5.2.2.
:
Xi, Xj,
( 7.5.2.10).
,
Xi, :
 Xj, Xi.
 Xi .
, .
,
, .
E i T i,
X j ,
, ( 7.5.2.6)
Ei Ti. .

7.5.2.3.
E T
, T
E T.

7.5.2.4.
E T
, , T,
E T.

7.5.2.5.
Xi Xj, Tk Xj
Ek Tk Xi Ek Tk.
Xj Xi, Xj Xi, Xi Xk Xk
Xj. , ,
.

266

7.


- .

7.5.2.6.
E T :
E U ( 7.5.2.12) T
Tb, U Tb.
, E T
T 1...T k
Tb T1...Tk U,
U Tb.

, ,
, .
, .

, E U, U T.
.

7.5.2.7.
E T :
E U1...Uk T
V1...Vk,
Ui ( 7.5.2.8) Ui Vi.

7.5.2.8.
U V :
V Xi, U Xi.
V1...Vk U1...Uk
:

7.5.

267

 V V1[...] U U1[...],
V U .
 V V1? U U1?.
 V C<V1...Vk> U C<U1...Uk>.
 ,
Ui Vi.
.

7.5.2.9.
U V :
V Xi, U Xi.
V1...Vk U1...Uk
:
 V V1[...] U U1[...] (
, U1[...]),
V U ( ).
 V IEnumerable<V 1 >, ICollection<V 1 >
IList<V1>, U U1[] (
, U1[]).

, ( ) System.Collections.Generic.

 V V1? U U1?.
 V , ,
C<V1...Vk>, C<U1...Uk>,
U (, U -,
) C<U1...Uk>, ( )
( ).
(, , , interface
C<T>{} class U: C<X>, C<Y>{} U C<T> , U1 X Y.)
, Ui Vi :
, Ui ,
.

268

7.

, V , .
, U C<U1...Uk>, i- C:
 , .
 ,
.
 , .
.

7.5.2.10.
U V :
V Xi, U Xi.
V1...Vk U1...Uk
:
 U U1[...] V V1[...],
V U .
 U IEnumerable<U e >, ICollection<U e >
IList<Ue>, V Ve[].
 U U1? V V1?.
 U , , C<U1...Uk>, V , ,
, C<U1...Uk>, ( ) ( ).
(, , ,
interface C<T>{} class V<Z>: <X<Z>>, C<Y<Z>>{}, <U1> V<Q>
. U1 X<Q> Y<Q>.)
, Ui Vi :
, Ui ,
.
, V , .
, U C<U1...Uk>, i- C:
 , .
 ,
.
 , .
.

7.5.

269


, -
, . ,
, :
using System.Collections.Generic;
class C
{
static void Foo<T,U>(T x) where T : IEnumerable<U> { }
static void Main()
{
Foo(new List<int>());
// CS0411: The type arguments for method C.Foo<T,U>(T)
// cannot be inferred from the usage
// ( - )
}
}

7.5.2.11.
Xi
:
U j
, Xi.

C#. ,
. {, ,
}, ; :
, .

Xi: U
Xi Uj, U, . U Xi Uj,
U, . U Xi Uj, U,
.
Uj V,
, Xi
V.
.

270

7.


C# 2.0 ,
- . C# 3.0
, Join, .
, , int,
int?, . int
int?, ,
. C# 4.0 ,
; ,
-.

7.5.2.12.
F
.
, -:
,
.
:
F , F .
F return
T ( 7.5.2.14),
F T.

return
. ,
.

E .

Select, System.Linq.Enumerable:
namespace System.Linq
{
public static class Enumerable
{
public static IEnumerable<TResult> Select<TSource, TResult>(
this IEnumerable<TSource> source,
Func<TSource, TResult> selector)

7.5.

271

{
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.6.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.

7.5.2.13.


, M, ,
D ( 6.6).
Tr M<X1...Xn>(T1 x1 ... Tm xm)

272

7.

M, D;
S1...Sn, ,
M<S1...Sn>

D ( 15.1).
,
.
, , ,
.
, Xi ,
Uj D
Tj M. - Xi ,
. Xi
Si, .

7.5.2.14.

.
, .
, E1...Em
Tr M<X>(X x1 ... X xm)

Ei.
, X,
, Ei X. X
, , S . S ,
.

7.5.3.

. , ,
C#:
, - ( 7.6.5.1).
, --
( 7.6.10.1).
-- ( 7.6.6).

( 7.3.3 7.3.4)

7.5.

273


,
(base-access).


,
. ,
, override ( 7.4),
, -
( 7.6.5.1).

,
:

.
, .
, ,

7.5.3.2.
, .
(applicable function member) (better function member).

. , , . ,
.
, .

7.5.3.1.

( 7.6.5.3).

A, :

274

7.

A
, 7.5.11, , , .
A (
value, ref out)
, :
 - - ( 6.1) ,
 ref out . ref out .
, -,
, ,
. , -, ,
:
-
- , A . A ,
,
, , .
, A
, :
 - -, , ( 6.1)
,
 ref out .

7.5.3.2.

A, ,
.
:
,
.

.

7.5.

275

,
,
.
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
.
{P1, P2, , PN} {Q1, Q2, , QN}
( Pi
Qi),
:
MP , MQ ,
MP , MQ.
, M P , M Q
params , MP
, MQ.
, M P , M Q, M P ,
MQ. , params .
, MP ,
MQ , MP , MQ.
, MP , MQ, MP ,
MQ. {R1, R2, , RN} {S1, S2, , SN}
MP MQ. MP , MQ, RX
, SX, RX
, SX:
 - , , .
 , , ( ),

, .

276

7.

 , ( ), ,
.
, , , .
.

7.5.3.3.
C1, E
T1, C2, E T2, C1
, C2,
:
S S T1,
S T2.
E
, T2 ( 7.5.3.5).
E , T 1 D 1,
Expression<D1>, T2 D2,
Expression<D2>, :
 D1 , D2.
 D1 D2 :
 D1 Y1, D2 Y2, X
( 7.5.2.12),
X Y1 , X Y2.
 D1 Y, D2
.

7.5.3.4.
C1, S T1, C2,
S T2, C1 , C2,
:
S T1, S T2.
T1 , T2 ( 7.5.3.5).

7.5.3.5.
T1 T2, T1 , T2, :

7.5.

277

T1 T2, T2 T1.
T1 , T2 .
:
 T1 sbyte, T2 byte, ushort, uint ulong.
 T1 short, T2 ushort, uint ulong.
 T1 int, T2 ulong.

7.5.3.6.
, , .

, .
:
interface I1<T> {...}
interface I2<T> {...}
class G1<U>
{
int F1(U u);
int F1(int i);
void F2(I1<U> a);
void F2(I2<U> a);
}
class G2<U,V>
{
void F3(U u, V v);
void F3(V v, U u);

// G<int>.F1
//
//

// ,
// G2<int,int>.F3

void F4(U u, I1<V> v);


void F4(I1<V> v, U u);

// ,
// G2<I1<int>,int>.F4

void F5(U u1, I1<V> v2);


void F5(V v1, U u2);

//

void F6(ref U u);


void F6(out V v);

//

7.5.4.


. , ,
. :

278

7.

.
, .
.

, ,
. :
: , dynamic, 7.5.2.
.
:
7.5.3.1, , ,
.
, .

7.5.5.
, . ,
, , , , .
:
. , , ,
. .
. , .
, ,
.
, this ( 7.6.7).
, . M
, M , E :
M :
 , 7.5.1.
 M.
M , :
 E. ,
.

7.5.

279

 E , E, E .
E
. this M, . , E
, ,
M this.

,
(mutability) . , readonly
. , ,
readonly value, , ! ,
value.

 , 7.5.1.
 M. , E, ,
this.
M , :
 E. ,
.
 , 7.5.1.
 E -, ( 4.3.1), E object, , E
object .
System.Object.
 E . E null,
NullReferenceException
.
 , , :
 E ,
, , M, , E,
.
 , M ,
, , M, , E.

280

7.

( 10.6.3), M,
, E.
 M ,
, .
, . , E, ,
this.

7.5.5.1.
, -,
:
(override) , object,
object.
-.
.

-, this . ,
, ,
.

7.6.
.
-:
---
--
----:

-
--
--
-
---
this-
base-
--
--
--
--

7.6.

281

---
-typeof
checked-
unchecked-
---
--

-- ----.


,
object o = new int[3][1];


object o = (new int[3])[1];

7.6.1.
-, ( 2.4.4),
.

7.6.2.
- ,
-:
-:

--opt

- I, I<A1,...,AK>, I
, <A1,...,AK> . - , K .
:
K - ( ) ( 3.3)
, I,
- ,
.

,
.

K - - I, -
-.

282

7.

, T ( 10.3.1), , ,

( ),
:
 K T - I,
- -.

.

 , ( 7.4) I T K :
 T , () ,
, this.
, ( 7.6.5.1).
 , T ,
() ,

,
, ,
( 7.6.4) this.I. , K
.
 ,
( 7.6.4) T.I T.I<A1, ..., AK>. , , .
N, , -,
( ) , ,
:
 K I N, :
 -
N - using--, I

7.6.

283

, -
.
 , I,
N.
 , N , I K
, :
 K - N - using--,
I ,
.
 ---- , -.
 , N:
 K
-- -using--,
I
,
.
 , , using---,
, I K -,
- ,
-.
 , , using---
, I K -,
- .
,
---- ( 3.8).
- .

7.6.2.1.
-
( 3.3), ,
- .
,
, switch, for, foreach, using
.

284

7.


,
, . ,
, .

:
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.6.4). :
struct Point
{
int x, y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
}

7.6.

285


. x y
,
this.x this.y .

7.6.3.
-- , :
--:
( )

-- , .
, .
--
.

7.6.4.
-- (member-access) -,
- --, . , --.
--:
-
.

--opt
-
.

--opt
--
.

--opt
-:
bool
byte
char
decimal double
object
sbyte
short
string
uint

oat
ulong

int
ushort

long

-- 9.7.
-- E.I, E.I<A1,...,AK>,
E -, I ,
<A1,...,AK> -.
-- - dynamic ( 7.2.2).
dynamic. , --,
, -
. , -
-.
-- :
K , E E
I, .

286

7.

, E E ,
I K -, , -.
E - -, , E -,
( 7.4) I E K - ,
E.I :
 I , ,
-.
 I , .
-,
( 7.6.5.1).
 I , .
 I :
 readonly , , , I E.
 , I
E.
 I :
 ,
( 10.8), E.I , I
.

.
 I , , .
 I , ,
.
 E.I .
E , ,
T ( 7.4) I T K - , E.I :
 -, E , ( 7.1.1), E
.
 I , E. -

7.6.

287

,
( 7.6.5.1).
 I , E.
 T I :
 E null, System.
NullReferenceException.
 , readonly , , , I ,
E.
 , I ,
E.
 T I :
 E readonly
,
, ,
, E.
 , I
, E.
 I :
 ,
( ) , ( 10.8),
+= =, E.I , I
.
 E.
E.I
( 7.6.5.2). , E.I
.

,
readonly ,
, ( . ).
:
struct S {
public int x;
public void SetX() { x = 2; }
}

288

7.

class C {
static S s;
public static void M() { s.SetX(); }
}

, s.SetX() s.x 2. readonly s,


s.SetX() ! s , , , ; , SetX() s, s.
, s ,
using ( 8.13), s ;
s.SetX() s.x, .

7.6.4.1.
E.I, E
E - ( 7.6.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
}
static void G() {
Color c = Color.White;
// Color.White
}
}

, Color A,
, , Color, .

7.6.4.2.
- ( 7.6.2) --
( 7.6.4) .

7.6.

289

,
F(G<A,B>(7));

F , G < A and B > (7).


F ,
G -
.
( )
- ( 7.6.2), -- ( 7.6.4) --- ( 18.5.2), --
( 4.4.1), ,
>.
(

==

!=

- -, - ----,
. - -, - ----,
. ,
-- -- ( 3.8).
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.6.5.
- .
-:
-

-opt

- ( 7.2.2), :
- dynamic.
-
dynamic, - .

290

7.


: ref/out dynamic
.
- -
, base-,
(CS1971).

-
dynamic. ,
- , dynamic. -
dynamic, , 7.5.4.
- -
-. - ,
- ( 7.6.5.1). -, ( 7.6.5.3). -
, -, .
- ( 7.5.1)
.
- :
- , void,
. ,
,
( 8.6) - ( 7.15).
.
, .

7.6.5.1.
- -
. ,
, ,
, .
, , , -.
M(A) , M
(, -), A , :
. F, M:

7.6.

291

 F , F , :
 M -,
 F A ( 7.5.3.1).
 F , M , F ,
:
 ( 7.5.2) , - ,
 - - , F
( 4.4.4), F
A ( 7.5.3.1).

, ,
. , , , .

 F , M , F ,
:
 F -, -,
 - - , F
( 4.4.4), F
A ( 7.5.3.1).

: C.F , C , F, , , .
, C , object, ,
, . ( ,
, , object,
.)

,
,

. ,
, ( 7.4).
class Base
{
public virtual void Foo(int x) { }
}

292

7.

class Derived : Base


{
public override void Foo(int x) { }
static void Foo(object x) { }
static void Main()
{
var d = new Derived();
d.Foo(1);
//
//
//
//
//
//

Error CS0176: Member Derived.Foo(object)


cannot be accessed with an instance reference;
qualify it with a type name instead
CS0176: Derived.Foo(object)
;

}
}

,
,
( 7.6.5.2). ,
.
7.5.3.
, ,
. ,
- ( )
.
:
 :
, - -- .
,
- -- ,
base-. ,
.


-, - (
, , ,

7.6.

293

-),
.

 , -
( ) ,
. -
() , .

, ,

.


, , :
, , , : ,
? , ,
, ? , : C# ,
. ,
, ,
, , ,
- . ,
, , .



,
7.5.4.

: , .

, : , ,
, ,
, , .
, , -

294

7.

, ,
. ,
.

, , .
.
-, ,
; , .
-,
. , , . ,
,
.
, :
, , ,
, .
;
, .


,
.
, ,

, ,
. ,
, :
public void Foo(object x) { ... }
public override void Foo(int y) { ... }

Foo(10)
,
Microsoft, - .

7.6.5.2.
( 7.5.5.1)
. ( )
. ( )

7.6.

295

. < - > ( )
. < - > ( )

, .
dynamic, .

ref/out dynamic.

- C, ,
:
C
C
C
C

.
.
.
.

(
(
<
<

)
, )
- > ( )
- > ( , )

Ci.Mj , :
Ci .
Mj .
Mj ,
, .
Mj.

, , double,
int. ,
.

C :
, ,
:

Ci Mj,
, .
 ,
using- ,
Ci

296

7.

Mj, , .

, .
,
7.5.3.
, .
C , .
C ,
( 7.5.4).

, .



, , . , SetX(this S s) S , S, s.SetX()
SetX(s), s ,
, . ,
s
.



, .
, , using:
using static System.Linq.Enumerable;


IntelliSense,
, . ,
, , .

, , , ,
, ,
, ,

7.6.

297

, using- . :
public static class E
{
public static void F(this object obj, int i) { }
public static void F(this object obj, string s) { }
}
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 .
public static class C
{
public static void F(this int i) {
Console.WriteLine("C.F({0})", i);
}
public static void G(this int i) {
Console.WriteLine("C.G({0})", i);
}
public static void H(this int i) {
Console.WriteLine("C.H({0})", i);
}
}
namespace N1
{
public static class D
{
public static void F(this int i) {
Console.WriteLine("D.F({0})", i);
}

298

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

, , , , .


, C# , , .
, ,
, .

, .
, ,
.

7.6.

299

Microsoft C# , , .
, ,
#pragma, .


,
,
, ( ). ,
, , , .
,
.
.

7.6.5.3.
- -
-. , -
, -,
- ( 7.5.3.1) -
-.
D(A) , D
A
, :
D. ,
.
D . D null, System.NullReferenceException .
D . ( 7.5.4)
. ,
, , .

7.6.6.
(element-access) ---, "[", -
, , "]". - , .

300

7.

---:
---- [ - ]

- ref out.
--- ( 7.2.2),
:
----
dynamic.
- dynamic ----
.
---
dynamic. ,
---, ,
---- , dynamic.
----
dynamic, --- , 7.5.4.
---- -- -, ---
( 7.6.6.1). --- , , ,
--- ( 7.6.6.2).

7.6.6.1.
----
--- . ,
- . - ,
, int, uint, long
ulong .
, , (
) .

, , : ,
ref out. ,
.

7.6.

301

P[A],
P ---- - A
-, :
P. , .
-
.
: int, uint, long, ulong.
, ( 6.1). , short, int, short
int short long.
,
.
P . P null, System.NullReferenceException
.
- ,
P. , System.IndexOutOfRangeException
.
, , .

7.6.6.2.
----
--- ,
,
, - --.
P[A], P
---- ,
T A -, :
T. , T , override
( 3.5).
, .
S.I , S , I:
 I A ( 7.5.3.1), I
.

302

7.

 I A ( 7.5.3.1), , S, .
 I A ( 7.5.3.1) S , object, , ,
.
, .
7.5.3.
,
.
- , . ,
.
, ,
P A.
, ,
get-accessor, set-accessor.
, set-accessor,
( 7.17.1). get-accessor
( 7.1.1).

,
.

7.6.7. this-
this- this.
this-:
this

this- ,
. :
this - , .
, ,
.
this -
, .
( 10.3.1),
, ,
.

7.6.

303

this - , .
, , . this
, out
, ,
.
this -
, . ,
.
 ( 10.14), this ,
, , ref
.
 , this , ,
, -
.


this = new CustomStruct(...);

- . ,
, , , ,

C#.

this ,
, . ,
this , .

7.6.8. Base-
Base- base,
. , - :
base-:
base .
base [ - ]

Base- , . Base- -

304

7.

,
. 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- (
dynamic), (CS1972).

base- (,
), ,
( 7.5.4).
( 10.6.3) B (
this, - ).
,
base-
. ,
base-, , .

Microsoft C# 2.0 ,
, , .
, ,
,
, ,
. , base- .

7.6.9.

--:
- ++
--:
- --

7.6.

305


, , . ,
.
- dynamic, ( 7.2.2), --
-- dynamic,
,
-.
,
get set. ,
.
( 7.3.3). ++
-- : sbyte, byte, short, ushort, int, uint,
long, ulong, char, oat, double, decimal . ++ , 1
, -- , 1 . ,

, System.
OverowException.

x++ x-- :
x :
 x, .
 x .
 x.
 , , , x.
 x .
x :
 ( x static)
( x ),
x, get
set.
 get x .
 x
.
 set x ,
value.

306

7.


--get, --set ,
.

 x .
++ -- ( 7.7.5).
x++ x-- x , ++x --x x .
x .
++ --
, . .

7.6.10. new
new .
new:

-.

.

.
new , . ,
- , ,
: new
-, .

7.6.10.1.
-- -
-.
--:
---opt
new

( -opt )
new

---
---:
-
-

-- -, -
-. abstract.
- ( 7.5.1) ,
.

7.6.

307

, , ,
. .
, , :
,
,
( 7.6.10.2) ( 7.6.10.3).
-
dynamic, -- ( 7.2.2),
, , dynamic.
,
7.5.4.
-- new
T(A), T - - A -, :
T - A :
 -- . -- T
, T, ,
4.1.1.
, T A :
 -
( 10.1.5) T, .
 --
, -, .
-.
, T :
 T abstract, .
 , ,
7.5.3. ,
T, A ( 7.5.3.1).
,
.
 -- T, , ,
.

308

7.

-- .
-- ,
T.
--
new T(A), T A , :
T :
 T.
, System.
OutOfMemoryException .
 ( 5.2).

( 7.5.4). ,
this.
T :
 T .

,
.

( 7.5.4). ,
this.

7.6.10.2.

.
-:
{ --opt
{ -- ,
--:
-
--

}
}

-:
= -
-:

---

7.6.

309


, .

, , .
,
.
.
, ,
, ( 7.17.1).
, ,
, . ,
.
- -, .
,
, .
, , , , .
, 7.6.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.X
__a.Y
Point

__a = new Point();


= 0;
= 1;
a = __a;

__a . ,
:
public class Rectangle
{
Point p1, p2;
public Point P1 { get { return p1; } set { p1 = value; } }
public Point P2 { get { return p2; } set { p2 = value; } }
}

310

7.

Rectangle :
Rectangle r = new Rectangle {
P1 = new Point { X = 0, Y = 1 },
P2 = new Point { X = 2, Y = 3 }
};


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;

__r, __p1 __p2 , .



,
. :
Point p = null;
int zero = 0;
try { p = new Point { X = 3, Y = 4 / zero }; }
// DivideByZeroException
catch { Console.WriteLine (p == null); }
//


. :
Point p1 = new Point(); p1.Y = p1.X; //
Point p2 = new Point() { Y = p2.X }; // ; p2

Rectangle Point:
public class Rectangle
{
Point p1 = new Point();
Point p2 = new Point();
public Point P1 { get { return p1; } }
public Point P2 { get { return p2; } }
}

Point
:

7.6.

311

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;

7.6.10.3.
.
-:
{ --
{ --

}
,

--:
-
--

-:
--
{ - }
-:


, .
,
(), , , .
,
. --
7.18.
:
List<int> digits = new List<int> { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

, , , System.Collections.IEnumerable,
.
Add , . , ()
Add .

312

7.


:
IEnumerable Add. ,
IEnumerable ! ?
C#
. -, , Add, , .
, . -, , ,
Add, . -,
IEnumerable, .
IEnumerable
Add .

:
public class Contact
{
string name;
List<string> phoneNumbers = new List<string>();
public string Name { get { return name; } set { name = value; } }
public List<string> PhoneNumbers { get { return phoneNumbers; } }
}

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 __clist = new List<Contact>();
Contact __c1 = new Contact();
__c1.Name = "Chris Smith";
__c1.PhoneNumbers.Add("206-555-0101");
__c1.PhoneNumbers.Add("425-882-8080");
__clist.Add(__c1);
Contact __c2 = new Contact();
__c2.Name = "Bob Harris";
__c2.PhoneNumbers.Add("650-555-0199");
__clist.Add(__c2);
var contacts = __clist;

__clist, __c1 __c2 ,


.

7.6.

313

7.6.10.4.

.
--:
new -- [ - ]
new -
-
new
-

opt -opt


,
. , new int[10, 20]
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.5.2.14)
. , , , ,
-.
12.6.

, .
:
, ,
.

314

7.

( 6.1) : int, uint, long,


ulong. ,
.
,
.
. , System.OverowException .
.
,
System.OutOfMemoryException .

( 5.2).
, .
,
, , .

,
( , ,
).

, .
,
int[][] a = new int[100][];

100 int[].
null. ,
,
int[][] a = new int[100][5]; //

.
, ,
:
int[][] a = new int[100][];
for (int i = 0; i < 100; i++) a[i] = new int[5];


, , FxCop, ,
.

, , ,

7.6.

315

. , ,
101 100 . ,
int[,] = new int[100, 5];

,
.

, , , . ,
500 , 100 5
( ), ,
, ,
,
, JIT- CPU.


:
var
var
var
var

a
b
c
d

=
=
=
=

new[] { 1,
new[] { 1,
new[,] { {
new[] { 1,

10, 100, 1000 };


//
1.5, 2, 2.5 };
//
"hello", null }, { "world", "!" } }; //
"one", 2, "two" };
//

int[]
double[]
string[,]
Error

, int,
string ,
.
, object[].
, .

( 7.6.10.6)
. :
var contacts = new[] {
new {
Name = "Chris Smith",
PhoneNumbers = new[] { "206-555-0101", "425-882-8080" }
},
new {
Name = "Bob Harris",
PhoneNumbers = new[] { "650-555-0199" }
}
};

7.6.10.5.

-.
--:
new
- (

316

7.

, dynamic . , ,
, .
,
. ,
, .
dynamic, - ( 7.2.2) ,
,
. .
-- new
D(E), D - E , :
E , , ( 6.6)
E D.
E , , E D
( 6.5).
E , E D ( 15.1) D,
, E. E D, .
--
new D(E), D - E , :
E ,
( 6.6) E D.
E , E D ( 6.5).
E -:
 E. ,
.
 E n u l l , S y s t e m .
NullReferenceException .
 D.
, System.OutOfMemoryException
.
 ,
, E.

7.6.

317


. , .
( 15.1),
;
.
, , ,
, , ,
.
, ,
,
.
delegate double DoubleFunc(double x);
class A
{
DoubleFunc f = new DoubleFunc(Square);
static oat Square(oat x) {
return x * x;
}
static double Square(double x) {
return x * x;
}
}

A.f , Square,

DoubleFunc. ,
.

7.6.10.6.

.
---:
new
--
--:
{ - opt }
{ -- , }
--:
-
--
-:
-
--
base-
=

318

7.


Base- ,
base., .

. ,
object.
,
, . ,

new { p1 = e1 , p2 = e2 , ... pn = en }


class __Anonymous1
{
private readonly T1 f1 ;
private readonly T2 f2 ;
...
private readonly Tn fn ;
public __Anonymous1(T1 a1,
f1 = a1 ;
f2 = a2 ;
...
fn = an ;
}
public T1 p1 { get { return
public T2 p2 { get { return
...
public Tn pn { get { return

T2 a2,..., Tn an) {

f1 ; } }
f2 ; } }
fn ; } }

public override bool Equals(object o) { ... }


public override int GetHashCode() { ... }
}

Tx ex . ,
-, . ,
- null
, .
.

, Microsoft , , ,
( ),
.
(protected) , ,
,
. Microsoft
.

7.6.

319


.

,
: ,
,
. , .

,

,
.

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) ( 7.6.8). ( projection initializer)
.
,
.

, :
= = .

, , , .
, ,
.

7.6.11. typeof
typeof System.Type .
-typeof:
typeof
(

)
typeof
(
--
typeof
(
void
)

320

7.

--:

--opt

::

--opt
-- . --opt
--:
>
< opt
:
,
,

-typeof typeof, .
System.Type .
System.Type . , T typeof(T)
== typeof(T) . dynamic.
-typeof typeof,
--. --
- ( 3.8) , -- --, -
--. typeof ,
--, -
, --,
- - ,
-.
:
-,
-- --,
object .
, -.
--
, ( 4.4.3).
-typeof System.Type .
-typeof typeof, void.
System.Type, .
, typeof(void), .
,
,
, void,
System.Type.

7.6.

321

typeof -.
System.Type , . typeof
( 4.4.3). System.Type
, System.Type
.

, 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>) .

322

7.

7.6.12. checked unchecked


checked unchecked .
-checked:
checked
(
-unchecked:
unchecked
(

checked
, unchecked . -checked
-unchecked -- ( 7.6.3), , .

, ,
, ( )
. , ,
, . ,
/checked+ /checked-
,
.
Windows Forms.
, , GetHashCode; ;
, .


checked unchecked ( 8.11).
, checked unchecked, :
++ -- ( 7.6.9 7.7.5),
.
+, -, * / ( 7.8), .
( 6.2.1)
oat double .
,
, , , , :
,
( 7.19), . , -

7.6.

323

,
OverowException.
, .
(,
), checked unchecked,
unchecked, (
) checked.
(,
) checked.
unchecked, ,
,
.

.
:
class Test
{
static readonly int x = 1000000;
static readonly int y = 1000000;
static int F() {
return checked(x * y);
}

// OverowException

static int G() {


return unchecked(x * y);
}

// -727379968

static int H() {


return x * y;
}

//

, . F
System.OverowException, G 727379968
( 32 , ). H
: , F, , G.

class Test
{
const int x = 1000000;
const int y = 1000000;
static int F() {
return checked(x * y);
}

// ,

324

7.

static int G() {


return unchecked(x * y);
}

// -727379968

static int H() {


return x * y;
}

// ,

, F
H, , .
G,
, .
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);
public const int HighBit = unchecked((int)0x80000000);
}

uint.
int, unchecked
int .
checked unchecked .
. , decimal
unchecked.

checked. ,
, ,
.

7.7.

325

7.6.13.

( 5.2) .
-, ,
- -. (
null -, , .)
---:
default ( )

---
,
null, . --
-, ( 4.1.2).
--- ( 7.19), -
( 10.1.5). , --- , -:
sbyte, byte, short, ushort, int, uint, long, ulong, char, oat, double, decimal,
bool .

. Microsoft LINQ , , FirstOrDefault, SingleOrDefault DefaultIfEmpty.

7.6.14.
--
. 7.15.

7.7.
+, -, !, ~, ++, -- .
-:
-
+ -
- -
! -
~ -
--
--
-

326

7.

dynamic,
( 7.2.2).
- dynamic,
,
.

7.7.1.
+x ( 7.3.3).
, ,
. :
int operator +(int x);
uint operator +(uint x);
long operator +(long x);
ulong operator +(ulong x);
oat operator +(oat x);
double operator +(double x);
decimal operator +(decimal x);

.

.
, int x = -30; int y = +40;,
, .

7.7.2.
x ( 7.3.3).
, ,
. :

int operator (int x);
long operator (long x);

x . x
,
(231 int 263 long), x .
, System.OverowException; , .
uint, long,
long. ,

7.7.

327

int 2147483648 (231)


( 2.4.42).
ulong, . ,
long 9223372036854775808 (263) ( 2.4.4.2).
:
oat operator (oat x);
double operator (double x);

x . x NaN,
NaN.
:
decimal operator (decimal x);

x .
System.
Decimal.

7.7.3.
!x ( 7.3.3).
, ,
.
:
bool operator !(bool x);

: true,
false. false, true.

7.7.4.
~x ( 7.3.3).
, ,
.
:
int operator ~(int x);
uint operator ~(uint x);
long operator ~(long x);
ulong operator ~(ulong x);

x.
E :
E operator ~(E x);

328

7.

~x, x E,
U, , (E)(~(U)x).

7.7.5.

--:
++
-
--:

-- -

, ,
. , .

,
(get set). , .

( 7.3.3). ++
-- : sbyte, byte, short, ushort, int, uint,
long, ulong, char, oat, double, decimal .
++ , 1
, -- ,
1 . , , System.
OverowException.

++x --x :
x :
 x.
 x .
 , , , x.
 , , .
x :
 ( x static)
( x ), x, get set.

7.7.

329

 get x.
 ,
get.
 set value, .

- get set ,
.

 , , .
++ -- ( 7.6.9).
x++ x-- x ,
++x --x .
x .

, . , , ,
. x -
; - x , x
, ,
get . .
:
index = 0; value = this.arr[index++];

, : 0 ;
, ++ index.
, , . : (1) ;
(2) ; (3) . , .

operator++ operator-- , .
.

7.7.6.
- .
-:
( )
-

330

7.

- (T)E, T , E , ( 6.2) E T.
E T,
. ,
. ,
E .

- ,
- . ,
(1) , , - ,
; (2) ,
, , . , .
, - , , ,
.

- . , (x)y
- ( y x), -
-- ( x y).
, : ( 2.3.3), ,
- , :
,
, .
,
, , , ~, !, (, ( 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),
(
).

7.8.

331

7.8.
*, /, %, +, .
-:
-
-
-
-

*
/
%

-
-
-

-:
-
-
-

-
-


dynamic, ( 7.2.2).
dynamic, ,
dynamic.

, double, dynamic,
( ). , dynamic,
, , . , (
Microsoft .NET 4.0), sum double:
dynamic sum = 0;
for (int i=0; i<count; i++)
sum += (i + 1.0) * i;

7.8.1.
x * y
(7.3.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);

332

7.

,
, OverowException.
, ,
, .
:
oat operator *(oat x, oat y);
double operator *(double x, double y);


IEEE 754. , ,
NaN. x y ;
z x y. ,
, z .
, z .

+x
x
+0
0
+

NaN

+y

+0

NaN

+z
z
+0
0
+

NaN

z
+z
0
+0

+
NaN

+0
0
+0
0
NaN
NaN
NaN

0
+0
0
+0
NaN
NaN
NaN

NaN
NaN
+

NaN

+
NaN
NaN

+
NaN

NaN
NaN
NaN
NaN
NaN
NaN
NaN

:
decimal operator *(decimal x, decimal y);

, , ,
decimal, System.
Over owException. ,
. .

System.Decimal.

IEEE 754 ,
IEEE 754 , 2008
IEEE , , !
IEEE 754
, Microsoft
Mono C#: NaN,
NaN, , ,

7.8.

333

NaN. NaN 64- double 51 ( double 251 , 2*101


NaN). NaN 32- oat 22 ( float 222 , 4*106 NaN).
NaN .
,
IEEE . , ,
.
.NET System.Double.IsNan(d),
, NaN double.
d o u b l e D o u b l e T o I n t 6 4 B i t s
Int64BitsToDouble,
NaN:
public static long GetNanPayload(double d) {
return System.BitConverter.DoubleToInt64Bits(d) & 0x0007FFFFFFFFFFFF;
}
public static double MakeNanPayload(long nanbits) {
nanbits &= 0x0007FFFFFFFFFFFF;
nanbits |= System.BitConverter.DoubleToInt64Bits(Double.NaN);
return System.BitConverter.Int64BitsToDouble(nanbits);
}

, .NET oat,
,
. . 18.4.

7.8.2.
x / y
. , , .
.
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.
. ,
,
. , ,
, .

334

7.

,
int long, 1,
.
System.ArithmeticException ( ).
System.
ArithmeticException, ,
.
:
oat operator /(oat x, oat y);
double operator /(double x, double y);

IEEE
754.
, , NaN. x
y ; z x y. ,
, z . ,
, z .

+x
x
+0
0
+

NaN

+y

+0

NaN

+z
z
+0
0
+

NaN

z
+z
0
+0

+
NaN

NaN
NaN
+

NaN

+
NaN
NaN

+
NaN

+0
0
+0
0
NaN
NaN
NaN

0
+0
0
+0
NaN
NaN
NaN

NaN
NaN
NaN
NaN
NaN
NaN
NaN

:
decimal operator /(decimal x, decimal y);

,
System.DivideByZeroException. , decimal,
System.OverowException. , decimal, .
, , ,
.

System.Decimal.

7.8.3.
x % y ( 7.3.4) .

7.8.

335

,
, .
.
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 , DivideByZeroException.

:
static bool IsOdd(int x) { return x%2 == 1; }

? ! , ,
static bool IsOdd(int x) { return x-(x/2)*2 == 1; },

x, -1. :
static bool IsOdd(int x) { return x%2 != 0; }

int long
1, System.OverowException.
x % y , x / y.
:
oat operator %(oat x, oat y);
double operator %(double x, double y);

, , NaN. x
y ; z x % y x n * y, n
, x / y.
, ,
IEEE 754 ( n , x / y).

+x
x
+0
0
+

NaN

+y

+0

NaN

+z
z
+0
0
NaN
NaN
NaN

+z
z
+0
0
NaN
NaN
NaN

NaN
NaN
NaN
NaN
NaN
NaN
NaN

NaN
NaN
NaN
NaN
NaN
NaN
NaN

x
x
+0
0
NaN
NaN
NaN

x
x
+0
0
NaN
NaN
NaN

NaN
NaN
NaN
NaN
NaN
NaN
NaN

336

7.

:
decimal operator %(decimal x, decimal y);

, System.
DivideByZeroException.
, , ,
, x.

System.Decimal1.1

, (modulo), mod. ,
, modulo
, Modulo testing, debugging,
and documentation, were ready to ship!1.

7.8.4.
x + y ( 7.3.4) .
- ,
, .
. .
string,
.
:
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.OverowException.

, , .
:
oat operator +(oat x, oat y);
double operator +(double x, double y);

IEEE
754.
1

, , . .
.

7.8.

337

, , NaN.
x y ; z
x + y. x y , , z
. x + y ,
, z ,
x + y.

x
+0
0
+

NaN

+0

NaN

z
y
y
+

NaN

x
+0
+0
+

NaN

x
+0
0
+

NaN

+
+
+
+
NaN
NaN

NaN

NaN

NaN
NaN
NaN
NaN
NaN
NaN

:
decimal operator +(decimal x, decimal y);

, decimal, System.OverowException.

.
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);

+ .
null, .

338

7.

, , ToString, object.
ToString null, .
using System;
class Test
{
static void Main() {
string s = null;
Console.WriteLine("s =
int i = 1;
Console.WriteLine("i =
oat f = 1.2300E+15F;
Console.WriteLine("f =
decimal d = 2.900m;
Console.WriteLine("d =
}
}

>" + s + "<");

// s = ><

" + i);

// i = 1

" + f);

// f = 1.23E+15

" + d);

// d = 2.900

, , .
null.
, System.
OutOfMemoryException.

, .NET System.String +, , ,
. , a+b+c+d : String.Concat(a, b, c, d)
.

.
, D :
D operator +(D x, D y);

+ ,
D ( , ).
null, (
null). null,
.
, , ,
.
7.8.5 15.4. System.Delegate , + .

7.8.

339

7.8.5.
x y ( 7.3.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);

,
, System.OverowException.
,
, , .
:
oat operator (oat x, oat y);
double operator (double x, double y);

IEEE.
, , NaN.
x y ; z x y.
x y , z . x y ,
, z
, x y.

x
+0
0
+

NaN

+0

NaN

z
y
y
+

NaN

x
+0
0
+

NaN

x
+0
+0
+

NaN

NaN

NaN

+
+
+
+
NaN
NaN

NaN
NaN
NaN
NaN
NaN
NaN


decimal operator (decimal x, decimal y);

,
decimal, System.OverowException.
.

System.Decimal.

340

7.

. , 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.10.8).)
.
.
,
, .
, null.
:
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 + cd2 + cd1;
// M1 + M2 + M2 + M1
cd3 -= cd1;
// => M1 + M2 + M2
cd3 = cd1 + cd2 + cd2 + cd1;
cd3 -= cd1 + cd2;

// M1 + M2 + M2 + M1
// => M2 + M1

7.9.
cd3 = cd1 + cd2 + cd2 + cd1;
cd3 -= cd2 + cd2;

// M1 + M2 + M2 + M1
// => M1 + M1

cd3 = cd1 + cd2 + cd2 + cd1;


cd3 -= cd2 + cd1;

// M1 + M2 + M2 + M1
// => M1 + M2

cd3 = cd1 + cd2 + cd2 + cd1;


cd3 -= cd1 + cd1;

// M1 + M2 + M2 + M1
// => M1 + M2 + M2 + M1

341

}
}

7.9.
<< >> .
-:
-
-
<<
-
-
- -

dynamic,
( 7.2.2). , , dynamic, , ,
, dynamic.
x << count x >> count ( 7.3.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 , ,
.
, ,
, ,
.
:
int operator >>(int x, int count);
uint operator >>(uint x, int count);
long operator >>(long x, int count);
ulong operator >>(ulong x, int count);

342

7.

>> x , ,
.
x uint ulong, x , ,
.
,
, :
x int uint,
count. , count & 0x1F.
x long ulong,
count. , count & 0x3F.
, ,
x.

,
:
for (int i = 0; i < 40; i++)
{
Console.WriteLine(int.MaxValue >> i);
}

,
int.MaxValue
.


.
>> , , ( )
. >>
, ,
.
, , .
, x int, unchecked((int)
((uint)x >> y)) x .

7.10.

==, !=, <, >, <=, >=, is as .

7.10.
-:
-
-
-
-
-
-
-

<
>
<=
>=
is
as

-
-
-
-

-:
-
-
-

==
!=

-
-

343

i s 7.10.10, a s 7.10.11.
==, !=, <, >, <= >= -.
dynamic,
( 7.2.2).
dynamic, , ,
, dynamic.
x op y, op ,
( 7.3.4) . - ,
, .
.
bool, .

x == y
x != y
x<y
x>y
x <= y
x >= y

true, x y, false
true, x y, false
true, x y, false
true, x y, false
true, x y, false
true, x y, false

7.10.1.


:
bool
bool
bool
bool

operator
operator
operator
operator

==(int x, int y);


==(uint x, uint y);
==(long x, long y);
==(ulong x, ulong y);

344

7.

bool
bool
bool
bool

operator
operator
operator
operator

!=(int x, int y);


!=(uint x, uint y);
!=(long x, long y);
!=(ulong x, ulong y);

bool
bool
bool
bool

operator
operator
operator
operator

<(int x, int y);


<(uint x, uint y);
<(long x, long y);
<(ulong x, ulong y);

bool
bool
bool
bool

operator
operator
operator
operator

>(int x, int y);


>(uint x, uint y);
>(long x, long y);
>(ulong x, ulong y);

bool
bool
bool
bool

operator
operator
operator
operator

<=(int x, int y);


<=(uint x, uint y);
<=(long x, long y);
<=(ulong x, ulong y);

bool
bool
bool
bool

operator
operator
operator
operator

>=(int x, int y);


>=(uint x, uint y);
>=(long x, long y);
>=(ulong x, ulong y);

bool, ,
.

7.10.2.

:
bool operator ==(oat x, oat y);
bool operator ==(double x, double y);
bool operator !=(oat x, oat y);
bool operator !=(double x, double y);
bool operator <(oat x, oat y);
bool operator <(double x, double y);
bool operator >(oat x, oat y);
bool operator >(double x, double y);
bool operator <=(oat x, oat y);
bool operator <=(double x, double y);
bool operator >=(oat x, oat y);
bool operator >=(double x, double y);


IEEE 754:

7.10.

345

NaN, false !=, true.


x != y , !(x ==y). ,
NaN, <, >, <= >=
, . ,
x, y NaN, x < y false, !(x >= y)
true.
NaN,
:
< max < ... < min < 0.0 == +0.0 < +min < ... < +max < +,

min max
,
. :
 .
 ,
, .
 ,
, .

, Object.
Equals, -, Object.GetHashCode,
. ,
.


NaN, ==, , ,
Equals:
double x = double.NaN;
Console.WriteLine(x == x);
Console.WriteLine(x != x);
Console.WriteLine(x.Equals(x));

// False
// True
// True

, Equals ,
. ,
. == !=,
, .


NaNs, (. 7.8.1), Object.Equals.

346

7.

7.10.3.

:
bool
bool
bool
bool
bool
bool

operator
operator
operator
operator
operator
operator

==(decimal x, decimal y);


!=(decimal x, decimal y);
<(decimal x, decimal y);
>(decimal x, decimal y);
<=(decimal x, decimal y);
>=(decimal x, decimal y);

bool, ,
.

System.Decimal.

7.10.4.

:
bool operator ==(bool x, bool y);
bool operator !=(bool x, bool y);

== true, x y
true false. false.
!= false, x y
true false. true. bool, !=
, ^.

7.10.5.

:
bool
bool
bool
bool
bool
bool

operator
operator
operator
operator
operator
operator

==(E x, E y);
!=(E x, E y);
<(E x, E y);
>(E x, E y);
<=(E x, E y);
>=(E x, E y);

x op y, x y
E, U, op ,
, ((U)x) op ((U)y). ,

.

7.10.

347

7.10.6.
:
bool operator ==(object x, object y);
bool operator !=(object x, object y);

.

object, ,
operator == operator !=. ,
.
:
-,
null. , ( 6.2.4)
.
T, T -,
null. , T
-.
, . :
, ,
. , ,
, A B
A, B , . ,
.
-.
,
.
T, -, null
-,
false.
, null.
class C<T>
{
void F(T x) {
if (x == null) throw new ArgumentNullException();
...
}
}

348

7.

x == null , T
-. false.

C.F() default(int?), . null, HasValue false.

x == y x != y, -
operator == operator !=,
( 7.3.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,
string ( 7.10.7). False, ,
object,
.
, .

class Test
{
static void Main()
{
int i = 123;
int j = 123;
System.Console.WriteLine((object)i == (object)j);
}
}

7.10.

349

False, int.

7.10.7.
:
bool operator ==(string x, string y);
bool operator !=(string x, string y);

string ,
:
null.
,
.
, . , , .
7.10.6,
.

7.10.8.
:
bool operator ==(System.Delegate x, System.Delegate y);
bool operator !=(System.Delegate x, System.Delegate y);

:
- null, ,
null.
,
.
( 15.1), ,
( , ) .
:

(
), .
, -- (, )
, ( )
.

350

7.

7.10.9. null
== != , ,
null,
(
).
:
x == null

null == x

x != null

null != x,

x ,
( 7.2.4) , x,
HasValue. , !x.HasValue, x.HasValue.

7.10.10. is
is
. E is T, E
T , , , E
T ,
. - -, :
E , .
E null, E E null,
false.
, D E :
 E , D E .
 E , D .
 E -, D
E.
D T :
 T , true, D
T , D D T, D
- D T.
 T , true, D
T .
 false.
, is
.

7.10.

351

7.10.11. as
as
. ( 7.7.6),
as . ,
, null.
E as T, E T
, - .
, ,
, , :
E T ( 6.1.1), ( 6.2.3), ( 6.1.6), ( 6.1.7),
( 6.2.3), ( 6.2.4)
( 6.2.5).
E T .
E null.
E dynamic, E as T
,
E is T ? (T)(E) : (T)null,

, E . ,
E as T ,
, , .
E dynamic, , as ( 7.2.2).
:
E is T ? (T)(object)(E) : (T)null

, , , , as,
.

class X
{
public string F(object o) {
return o as string;
//:
}
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
}
}

352

7.

, - T G ,
. - U H, , ; , as H .

FxCop ( !), :
if( x is Foo ) { ((Foo)x).DoFoo(); }

:
Foo foo = x as Foo;
if( foo != null ) { foo.DoFoo(); }

? is, as , ,
.NET, , ,
null, -
, .


, . .


,
FxCop, .


as .
as , ,
. ,
as, , , , null. , ,
s, :
int length1 = ((string) s).Length;
int length2 = (s as string).Length;

// InvalidCastException
// NullReferenceException

InvalidCastException,
() NullReferenceException.
( s null ?)


as null ,
:

7.11.

353

//
as if (Foo foo = x)
{
foo.DoFoo();
}

, , , .

7.11.
&, ^ | .
-:
-
-
&
-
--:
-
--
--:
--
--

--

dynamic,
( 7.2.2).
dynamic, ,
, , dynamic.
x op y, op ,

( 7.3.4). - ,
, .
.

7.11.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);

354

7.

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.11.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 U op , ,
(E)((U)x op (U)y). ,

.

7.11.3.
:
bool operator &(bool x, bool y);
bool operator |(bool x, bool y);
bool operator ^(bool x, bool y);

x & y true, x y
true. false.
x | y true, x, y
true. false.
x ^ y true, x true, y
false, x false, y true.
bool, ^ , !=.

7.11.4.

bool? : true,
false null. ,

7.12.

355

SQL. & |
bool? SQL,
:
bool? operator &(bool? x, bool? y);
bool? operator |(bool? x, bool? y);

true, false null.


x
true
true
true
false
false
false
null
null
null

y
true
false
null
true
false
null
true
false
null

x&y
true
false
null
false
false
false
null
false
null

x|y
true
true
true
true
false
null
true
null
null

7.12.
&& || .
.
--
--
--
&&
--:
--
--

||

--

--

&& || & |:
x && y x & y, , y , x false.
x || y x | y, , y , x true.

dynamic, ( 7.2.2).
, , dynamic,
, , ,
dynamic.
x && y x || y ( 7.3.4) , , x & y
x | y. :

356

7.

,
.
,
( 7.11.3) ( 7.11.4), , 7.12.1.
, ,
, 7.12.2.
.
, ,
,
.
7.12.2.

7.12.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.12.2.

&& || , operator& operator |,
. T ,
:
T. , AND
OR T T.
T operator true operator false.

7.13.

357

,
. && ||
operator true operator
false :
x && y T.false(x) ? x : T.&(x, y), T.false(x)
operator false, T, T.&(x, y)
operator &. ,
x operator false , ,
x false. , x false,
, x.
y operator & ,
x, , y,
.
x || y T.true(x) ? x : T.|(x, y), T.true(x)
operator true, T, T.|(x, y)
operator |. ,
x operator true , ,
x true. , x true,
, x. y operator | , x, , y,
.
x , y .
, operator true operator false,
11.4.2.

7.13.
?? (null coalescing operator).
--:
--
--
??

--

a ?? b , a
. a , a ?? b ; b. b ,
a null.
, ,
. , a ?? b ?? c
a ?? (b ?? c). E1 ?? E2 ?? ...

358

7.

?? EN , , null,
null.
a ?? b , . , a ?? b A0, A B, A
a ( , a ), B b
( , b ), A0 A, A
, A . ,
a ?? b :
A ,
.
b , dynamic.
a. a
, a dynamic, .
b, .
, A
b A0, A0. a. a , a
A0, . b A0, .
, A b A,
A. a.
a , a . b A, .
, b B a B,
B. a.
a , a A0 ( A ) B,
. b,
.
a b , .

.
,
.


?? -.

7.14.

359

:
Foo f1 = ...;
Foo f2 = f1 ?? new Foo(...);
int? i1 = ...;
int i2 = i1 ?? 452;

7.14.
?: , .
-:
---:
--

b ? x : y b. ,
b true, x, . y, .
x y.
, , . , a ? b :
c ? d : e ? b :(c ? d : e).

.
, , . C#
, F#.
return rstCondition ? rstValue :
secondCondition ? secondValue :
thirdCondition ? thirdValue :
fallbackValue;

?: ,
bool, ,
operator true. , .

,
bool? bool, bool?.

?:, x y, .

360

7.

x X y Y, :
 ( 6.1) X Y, Y X,
Y.
 ( 6.1) Y X, X Y,
X.
 .
x y ,
, .
, .

Microsoft C# : , .
, .


, Microsofts C# 4.0 :
, ,
<null> <null>:
int? x = args.Length>0 ? null : null;

C# 2.0 . C# 4.0
, . ,
, - .


,
C#, C# 3.0, . null , .

b ? x : y :
-, b b:
 b bool, , bool.
 operator true,
b, .

7.15.

361

bool, , true, x ,
.
y
, .

: . ,
,
if-then-else. , ,
.

7.15.
- , .
, .
, ,
: ,
, .
, , .
, - --.
- ,
--, .
-:
--

=>

--

--:
delegate
---opt

--:
---
---
---
( ----opt
----:
---
----

---

362

7.

---:
---opt

---:
ref
out
---:
( ----opt
---
----:
---
----

---

---:

--:

=> , (=), .
-
. , ,
. , ,
, ,
, ( 6.5).

, ,
.


. ,

(

param

=>

expr


param

=>

expr

--
. , . , ,
out.
:
x => x + 1
x => { return x + 1; }

// ,
// ,

7.15.
(int x) => x + 1
(int x) => { return x + 1; }
(x, y) => x * y
() => Console.WriteLine()
delegate (int x) { return x + 1; }
delegate { return 1 + 1; }

//
//
//
//
//
//

363

,
,



- --
:
-- ,
-.
- , -- ,
.
- ,
-- .
- ,
--
( 4.6).

, ,
, Linq2SQL Entity Framework.

7.15.1.
-- () .
-- ( 3.7).
-- ( ) ( 3.3).
,
, --
-, .
---,
,
, . ( 6.6), .
--,
,
out.

364

7.

, -- -. --
, .
, , , ( 4.6).

. , v o i d
M ( E x p r e s s i o n < F u n c < G i r a f f e > > f ) v o i d M ( F u n c < A n i m a l > f ) ,
M(()=>myGiraffes[++i]),
. ,
.

7.15.2.
( ) :
, , , . ,
( 6.5), .
, ref out
( ) ,
ref out
.
this , this . , (, , this.x) (, , x,
x ). , .
( 7.15.5) .
, -
-- ( 7.15.6).
goto, break continue,

, .
return , . , return,
-

7.15.

365

, -- ( 6.5).
, - - -. ,
, .
.

7.15.3.

. 7.4.2.3.
.
class ItemList<T>: List<T>
{
public int Sum(Func<T,int> selector) {
int sum = 0;
foreach (T item in this) sum += selector(item);
return sum;
}
public double Sum(Func<T,double> selector) {
double sum = 0;
foreach (T item in this) sum += selector(item);
return sum;
}
}

ItemList<T> Sum.
selector, .
int double,
int double.
Sum , , .
class Detail
{
public int UnitCount;
public double UnitPrice;
...
}
void ComputeSums() {
ItemList<Detail> orderDetails = GetOrderDetails(...);
int totalUnits = orderDetails.Sum(d => d.UnitCount);
double orderTotal = orderDetails.Sum(d => d.UnitPrice * d.UnitCount);
...
}

orderDetails.Sum Sum .
d => d.UnitCount Func<Detail,int>,

366

7.

Func<Detail,double>.
Sum, Func<Detail,int> ,
Func<Detail,double>.
orderDetails.Sum Sum,
d => d.UnitPrice * d.UnitCount
double. ,
Sum.

7.15.4.

, .

7.15.5.
, - -, - -, .
this , .

C#. .

7.15.5.1.
, ,
.
, ( 5.1.7).
,
, ,
.

, x ,
, . x
, ,
.

7.15.

367


using System;
delegate int D();
class Test
{
static D F() {
int x = 0;
D result = () => ++x;
return result;
}
static void Main() {
D d = F();
Console.WriteLine(d());
Console.WriteLine(d());
Console.WriteLine(d());
}
}

x , x , , F,
(
).
x,
1
2
3

-
, -
( 18.3), ,
(moveable) . ,
, , , xed,
.

7.15.5.2.
, . ,
, x
:
static void F() {
for (int i = 0; i < 3; i++) {
int x = i * 2 + 1;
...
}
}

x , :

368

7.

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

7.15.

369

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


,

. , , .

foreach , .
, , .
, ,
.

. , 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, :
1 1
2 1
3 1

370

7.

using System;
delegate void Setter(int value);
delegate int Getter();
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.15.6.

F D
E , new D(F).
, 6.5.

7.16.

, ,
, SQL XQuery.
-:
-from
-from
from
opt

-:
--opt
-opt
--:
--
--

in

-select--group

--

7.16.

371

--:
-from
-let
-where
-join
-join-into
-orderby
-let
let

-where:
where
-
-join:
join
opt

-join-into

join opt
into

in

in

on

on

equals

equals

-orderby:
orderby
:

:
-opt
-:
ascending
descending
-select--group:
-select
-group
-select:
select

-group:
group

by

-:
into

from select, group. from


from, let, where, join orderby.
from (range variable),
.
let , ,
. where , .

372

7.

join , .
orderby
. select group
. , into
, .

from...whereselect, , SQL, selectfromwhere? , SQL,
, SQL
,
(Integrated Development Environment, IDE),
C# .
-, SQL IntelliSense -
. , IDE select. select: ?
, ,
.
where. C# from select;
, where select, IDE
.
-, C# , : ,
,
. , .


, select-from SQL from-select LINQ,
LINQ , ,
SQL from-select.

7.16.1.
, ,
, . from, where, join, on, equals, into,
let, orderby, ascending, descending, select, group by.
, - , ,
.

7.16.

373

,
from , , ;,
= ,.

@. ( 2.4.2).

7.16.2.
C# . ,
( 7.16.3).
, Where, Select, SelectMany, Join, GroupJoin, OrderBy,
OrderByDescending, ThenBy, ThenByDescending, GroupBy Cast.
, 7.16.3.

, , .
,
.
, ,
C#. , ,
, , ,

.

,
.


, C# , . , ,
, ,
, .

, .
: , ;

374

7.

,
.
. C# ,
.

, *.
7.16.2.7.

C# 3.0,
. , . ,
, , , ,
.
, , , :
var duluthians = from c in Customers
where c.City == "Duluth" select c;

:
var duluthians = Customers.Where(c => c.City == "Duluth");

, .
:
var tenDuluthites = Customers.Where(c => c.City == "Duluth").Take(10);

var tenDuluthites = (from c in Customers where c.City ==


"Duluth" select c).Take(10);

, , , ,
, .


, IEnumerable<T> ,
: , .
(, Reactive Extensions Parallel Extensions),
.

7.16.2.1. select GroupBy



from ... into x ...

7.16.

375


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() })


select group
( ) .
,
join let, . , let select:
.
, , , .

7.16.2.2.

from,
from T x in e


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 Customer c in customers


where c.City == "London"
select c

376

7.


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.

7.16.2.3.

from x in e select x


( e ) . Select ( x => x )

from c in customers
select c


customers.Select(c => c)

.
,
, . , , , ,
. , , , Select .
, Select
, .

7.16.2.4. from, let, where, join orderby



,
:
.
C# 3.0;
- .

, let, from join.

7.16.

377

, from select
from x1 in e1
from x2 in e2
select v


( 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 )
...

join into,
select
from x1 in e1
join x2 in e2 on k1 equals k2
select v


( e1 ) . Join( e2 , x1 => k1 , x2 => k2 , ( x1 , x2 ) => v )

join into,
- , select
from x1 in e1
join x2 in e2 on k1 equals k2
...


from * in ( e1 ) . Join( e2 , x1 => k1 , x2 => k2 , ( x1 , x2 ) => new { x1 , x2 })
...

, join
into, select

378

7.

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 )
...

,
OrderByDescending ThenByDescending.
, let, where, join orderby
from.

from c in customers
from o in c.Orders
select new { c.Name, o.OrderID, o.Total }


customers.
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 }


from * in customers.
SelectMany(c => c.Orders, (c,o) => new { c, o })
orderby o.Total descending
select new { c.Name, o.OrderID, o.Total }

7.16.

379

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 }


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,
Select(x => new {
Where(y => y.n >=
Select(y => new {

c => c.CustomerID, o => o.CustomerID, (c, co) => new { c, co }).


x, n = x.co.Count() }).
10).
y.x.c.Name, OrderCount = y.n)

380

7.

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)

7.16.2.5. 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")

7.16.2.6. GroupBy

from x in e group v by k


( e ) . GroupBy ( x => k , x => v )

, v x.
( e ) . GroupBy ( x => k )

from c in customers
group c.Name by c.Country


customers.
GroupBy(c => c.Country, c => c.Name)

7.16.2.7.
(transparent identifiers), *. -

7.16.

381

;

.
,

.
:
,
.
,
.
, .
, , . 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 })

x , ,
.

382

7.

from c
join o
join d
join p
select

in customers
in orders on c.CustomerID equals o.CustomerID
in details on o.OrderID equals d.OrderID
in products on d.ProductID equals p.ProductID
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 {
Join(details, x => x.o.OrderID, d => d.OrderID, (x, d) => new { x,
Join(products, y => y.d.ProductID, p => p.ProductID, (y, p) => new
Select(z => new { z.y.x.c.Name, z.y.x.o.OrderDate, z.p.ProductName

c, o }).
d }).
{ y, p }).
})

x, y, z ,
.

7.16.3.
(query expression pattern) , .
, ,
. ,
,
,
, .
C<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);

7.16.

383

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);
public O<T> ThenByDescending<K>(Func<T,K> keySelector);
}
class G<K,T> : C<T>
{
public K Key { get; }
}

Func<T1,
R> Func<T1, T2, R>,
.
, C<T> O<T>, ThenBy ThenByDescending
OrderBy OrderByDescending. ,
GroupBy ,
Key.

ThenBy , OrderBy, ,
.

System.Linq , System.Collections.
Generic.IEnumerable<T>.

, IQueryable<T>.

384

7.


Join , . , , int, int?,
,
- , . int int?,
int? K.

7.17.
, , .
:
-

-:
=
+=
-=
*=
/=
%=
&=
|=
^=
<<=
---

, , ,
.
= .
, , . 7.17.1.
, =, .
, ,
, .
7.17.2.
+= -=
. .
7.17.3.

7.17.

385

, ,
. , a = b = c
a = (b = c).

7.17.1.
= .
E.P
E[Ei], E dynamic,
, , , E.
,
.
, ,
.
, . , ,
.
, set. ,
.

:
x :
 , .
 y x ( 6.1).
 , x, ,
, , , y, ,
x. , y
null ( 6.1.6)
, y,
, x. ArrayTypeMismatchException.
 , y,
, x.
x :
 x ( x
static) ( x ),
set.
 y x ( 6.1).

386

7.

 set x value, , y.
( 12.5) ,
A[] B[] ,
B A.

, , , ,
. :
string[] sa = new string[10];
object[] oa = sa;
oa[0] = null;
//
oa[1] = "Hello";
//
oa[2] = new ArrayList();
// ArrayTypeMismatchException

System.ArrayTypeMismatchException, ArrayList string[].



, ,
.

, , ,
, .
, . 7.6.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; }
}
public int Y
{
get { return y; }
set { y = value; }
}
}

7.17.

387

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;

p.X, p.Y, r.A r.B , p r .


Rectangle r = new Rectangle();
r.A.X = 10;
r.A.Y = 10;
r.B.X = 100;
r.B.Y = 100;

, r.A r.B .

C# 1.0 ,
r.A.X = 10, , r.A
Point ( ), .
, .


, .

388

7.

7.17.2.
E.P E[Ei],
E dynamic,
( 7.2.2).
dynamic, ,
, E.
x op= y ( 7.3.4), , x op y.
:

x, x = x op y, x .
, x ,
x = (T)(x op y), T x, x .
.

, y x,
, C# , C,
C++ Java. x+=0.9 , x . x ,
; x , x 1. C#
, .


, x <<= null ,
x int. , .

,
x op y x - , x.
, A()[B()] += C(), A ,
int[], B C , int,
: A, B, C.

,
get set. , .

7.17.

389

x op= y x = (T)(x op y). ,


,
sbyte, byte, short, ushort char.
,
int, 7.3.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;

//
//
//
//

: b = 1000
: b = i

ch += 1;
// : ch = 1
ch += (char)1; //

, .
, .
int? i = 0;
i += 1;

//

+(int?,int?).

7.17.3.
, :
, .
+= -= ( 6.1).
,
, , , .
+=, add; -=,
remove.
. ,
- ( 8.6).

390

7.

7.18.
--, .

--

--:
-
-
-

7.19.
- , .
-:

null
: sbyte, byte, short, ushort, int, uint, long, ulong, char, oat,
double, decimal, bool, string .
:
( null).
const .
.
const .
, .
, ,
, .
checked unchecked.
.
+, , ! ~.
+, , *, /, %, <<, >>, &, |, ^, &&, ||, ==, !=,
<, >, <= >=, , , .
?:.
:
.
.

7.19.

391

.
.
,
, null.
, ,
, . :
class C {
const object i = 5;
const object str = "hello";
}

// :
// :

i ,
. str , .
, ,
, . ,
,
.

,
. , ,
.
,
,
, ( 7.19).
, .
,
.
( 10.4).
( 14.3).
case switch ( 8.7.2).
goto case ( 8.9.3).
(7.6.10.4), .
( 17).
( 6.1.8) int sbyte, byte, short,
ushort, uint ulong ,
.

392

7.

7.20.
- ,
bool , operator
true , .
-:


-if ( 8.7.1), -while ( 8.8.1), -do ( 8.8.2) -for ( 8.8.3).
?: ( 7.14) ,
,
--.
bool
, operator true.
, .
bool,
operator true, operator true ,
bool.
, operator true operator false, DBBool, 11.4.2.

C# .
, ++.
:
-
-:
-
-
-
-checked
-lock
-yield

,
. -,

. :
void F(bool b)
{
if (b)
int i = 44;
}

, if , . , i
, . i
, .

8.1.
. ,
, . , ,
,
. , .

, , .
, .

394

8.


void F()
{
Console.WriteLine("reachable");
goto Label;
Console.WriteLine("unreachable");
Label:
Console.WriteLine("reachable");
}

Console.WriteLine ,
.
. .
, ,
, .
( 7.19),
,
. ,

.

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 ,
.

, , false,
false . ,
if (. ) (i*0 == 0),
Console.WriteLine , ,
.

8.1.

395

.
.
:
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.
, , :
switch ,
switch , switch
. ,
break.
, , , .
, return.


, . :
public class Program
{
public static int counter = 5;
static void Main(string[] args)
{
if (counter == 6)
Console.WriteLine("weird");
else
Console.WriteLine("normal");
}
}

396

8.

, weird , .

8.2.
,
.
:
{ opt

- ( 8.2.1), . ,
.
- ( 8.5).
, , .
, ,
.
:
, .
, .
, .
, .
, .
, yield ( 8.14), . ( 10.14).
:
return
( yield return ).
( 18.1). ,
.

8.2.1.
, . ( 8.2) switch-
( 8.7.2).

8.3.
-:

397


. , .
,
.
,
:
.
.
,
goto.

, goto try
catch try, nally,
try, nally . :
class C
{
static void Main()
{
int x;
try
{
goto A; //
}
nally
{
throw new System.Exception();
}
A: x.ToString(); //
}
}

,
.

8.3.
.
-:
;

, ,
.

398

8.


, ,
, . , , , , :
while (text.IndexOf("xx") != -1);
{
text = text.Replace("xx", "x");
}

while .
- void;, .


, , ,
. ,
. ,
, , :
while (text.IndexOf("xx") != -1)
{
;
}
{
text = text.Replace("xx", "x");
}

, while .
,
.


. , ,
.
while, :
bool ProcessMessage() {...}
void ProcessMessages()
{
while (ProcessMessage())
;
}

,
:
void F()
{
...
if (done) goto exit;
...
exit: ;
}

8.4.

399

8.4.
- , . ,
.
-:

, . , ,
. , .
goto ( 8.9.3),
. , goto
,
.
. :
int F(int x)
{
if (x >= 0) goto x;
x = -x;
x: return x;
}

, .
, .
, goto,
. ( , goto
try, nally, try,
nally . try
goto.)

, nally .
goto ,
.


8.9.3 goto.

400

8.

8.5.
- .
-:
--
-

;
;

8.5.1.
:
--:
--
--
--:

var
--:
--
--

--

--:

--

--:

-- -- , var ,
.
--, . -- ,
, --,
= .
var - ( 2.4.3). -- var
var,
. .
:
--.
-.
.

8.5.

401

.
.


.
var a = 1, b = 2.5;

C#, , ,

double a = 1, b = 2.5;

, :
int a = 1; double b = 2.5;

.
, (
).


, ,
, .


,
.
, int j=M(out j); , .
var j=M(out j),
, , , j, . ,
. ,
, .


:
var
var
var
var
var

x;
y =
z =
u =
v =

{1, 2, 3};
null;
x => x + 1;
v++;

//
//
//
//
//

:
:
:
:
:

,

null

, - ( 7.6.2),
( 7.17).
( 5.3) , .

402

8.


,
( )
( , )!
:
var
var
var
var

a
b
v
d

=
=
=
=

new { Name = "Bob", Age = 42 }; //


1;
//
new Person();
//
GetPerson();
// !

d,
, .


, :
var , , . , . ,
: var attributes=ParseMethodAttributes();. ,
, AttributeSyntax[] List<AttributeSyntax>. ,
.


,
var. var ,
. , , . GetPerson() Person
, IPerson, -
Person IPerson.
. , -
( ), .

, - - , .
, -, .
.
.

. :
void F()
{
int x = 1, y, z = x * 2;
}

8.5.

403

:
void F()
{
int x; x = 1;
int y;
int z; z = x * 2;
}

var
, . :
var
var
var
var
var

i = 5;
s = "Hello";
d = 1.0;
numbers = new int[] {1, 2, 3};
orders = new Dictionary<int,Order>();


:
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>();


C# ,
.
var StandartML Scala, let F# nal Java.
, .
- ,
. using ( 8.13)
,
. ,
.

8.5.2.
-- .
--:
onst

-
-:
-
-
-:

-- , . -,

404

8.

. - , ,
= - ( 7.19),
.
- , ( 10.4).

- ( 7.6.2).
, . , -, .

.

.

8.6. -
- . , ( ), .
-:
-

-:
-
-

--
--
--
--

. , x+y ==1, (
), .
- . - ,
-.

- , ,
.
.
Assert.Fail unit-.

8.7.

405

, -,
, .
,
. , ,
.


, ECMAScript, -
, , . , C# ,
.

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();
}
}

406

8.

if :
- ( 7.20).
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-, .
-switch:
switch (
switch-:
{ switch-opt
switch-:
switch-
switch-

switch-

switch-

switch-:
switch-

switch-:
switch-
switch-

switch-

switch-:
case
-
default
:

-switch switch, , , switch-. Switch-

8.7.

407

switch-. switch-
switch-, ( 8.2.1).
switch .
switch- sbyte, byte, short, ushort, int, uint, long, ulong,
bool, char, string, ,
switch.

( 6.4) switch-
: sbyte, byte, short, ushort, int, uint, long, ulong, char,
string .
, , .
case
switch. case
switch , .
switch default.
switch :
.
case
, , case.
case , default ,
, default.
case , default , switch.
switch , . no fall
through, .
:
switch (i)
{
case 0:
CaseZero();
break;
case 1:
CaseOne();
break;
default:
CaseOthers();
break;
}

408

8.

, switch
. ++,
. :
switch (i)
{
case 0:
CaseZero();
case 1:
CaseZeroOrOne();
default:
CaseAny();
}

. 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 .
switch
++, break . ,
switch - . , switch switch
:

8.7.

409

switch (i)
{
default:
CaseAny();
break;
case 1:
CaseZeroOrOne();
goto default;
case 0:
CaseZero();
goto case 1;
}

switch break,
goto case goto default,
switch . , while
true
. throw return
. ,
:
switch (i)
{
case 0:
while (true) F();
case 1:
throw new ArgumentException();
case 2:
return;
}


, #
switch/case.
, break .
, switch ,
?
case 0:
{
// case 0 break
}

, case
, case ?

switch string. :
void DoCommand(string command)
{
switch (command.ToLower())
{
case "run":
DoRun();
break;

410

8.
case "save":
DoSave();
break;
case "quit":
DoQuit();
break;
default:
InvalidCommand(command);
break;

}
}

( 7.10.7),
switch . switch ,
case.
switch string, case
null.
- switch ( 8.5). switch .

, switch
.

switch ,
, ( 7.6.2.1).
switch ,
switch :
switch .
switch ,
switch.
, switch , default.
switch goto case
goto default.

, goto case goto default try catch, try nally,
, switch try.

switch ,
:
switch break,
switch .

8.8.

411


, break try
catch, try nally, ,
break try.

switch , default.
switch , ,
case, default.

8.8.
.
-:
-while
-do
-for
-foreach

8.8.1. while
while
.
-while
while
( -

while :
- ( 7.20).
true,
. (, continue),
while.
false,
while.
break ( 8.9.1) while,
. continue ( 8.9.2)
, while.
, while
false.
while ,
:

412

8.

while break, while.



, break try
catch, try nally, ,
break try.

while true.

8.8.2. do
do , , .
-do:
do
-

while

do :
.
(, continue), -
( 7.20). true,
do.
do.
break ( 8.9.1) do,
. continue ( 8.9.2)
,
do.
do , do.
do ,
:
do break,
.

, break try
catch, try nally, ,
break try.


true.

8.8.

413

8.8.3. for
for ,
, true,
.
-for
for ( -foropt
-

-foropt

-foropt

-for:
--
--
-for:
-
-for:
--
--
-
--

-for, , - ( 8.5.1), - ( 8.6), . ,


-for, -- , -for -for.
-for, , - ( 7.20).
-for, , - ( 8.6), .
for :
-for , - , .
.
-for , .
-for true, .
(, continue),
-for, . , -for (. ).
-for false, for.
break ( 8.9.1) for, . continue (
8.9.2) -

414

8.

( -for
for, -for).
, :
for -for.
for , -for false.
for , :
for break,
for.

, break try
catch, try nally, ,
break try.

for , -for
true.

8.8.4. foreach
foreach , .
-foreach:
foreach ( --
-

in

foreach .
- var var, ,
foreach
. , ,
.
foreach ,
.
++ -- .

ref out .

foreach for.

8.8.

415


(
), , .
, "a", "b", "c", "d",
"d".
List<Action> actions = new List<Action>();
foreach (string value in new[] { "a", "b", "c", "d" })
{
actions.Add(() => Console.WriteLine(value));
}
foreach (Action action in actions)
{
action();
}


foreach, . , .
:
foreach (string value in new[] { "a", "b", "c", "d" })
{
String copy = value;
actions.Add(() => Console.WriteLine(copy));
}


foreach - , . V v while try-while- nally,
. ,
C# . C#
2.0 v ,
, C# Microsoft,
.

foreach ,
. :
, System.Collections.IEnumerable, System.
Array .
System.Collections.IEnumerable, System.
Collections.IEnumerator, .
dynamic, System.Collections.IEnumerable ( 6.1.8).
System.Collections.IEnumerable,

416

8.

System.Collections.IEnumerator.
- var, dynamic,
object.
,
GetEnumerator:
 GetEnumerator
. ,
, IEnumerable, .
, ,
.

, , ,
IEnumerable<T> .
IEnumerable
,
Current object. , , GetEnumerable, MoveNext Current,
Current .
, , IEnumerable<T>,
.
.


. ,
public,
IEnumerable, . , public , .
 GetEnumerator ,
,
.
 Current
. , ,
public , , .
 MoveNext
. , ,
,
.

8.8.

417


. ,
, public bool, .
 , ,
Current.
:
 , Sistem.Collections.Generic.IEnunerable<T>,
,
Sistem.Collections.Generic.IEnunerable<T>, .
 , , .
 , System.
Collections.IEnumerable, ,
System.Collections.IEnumerator,
object.
, .

, foreach ,
. ,
I,
, :
C[] xs = ...;
foreach (I x in xs)
...

, , , I . ,
InvalidCastException
- , foreach.
, -
, I. ,
I ,
.

,
, . foreach

foreach(V v in x) -

418

8.

{
E e = ((C)(x)).GetEnumerator();
try
{
V v;
while (e.MoveNext())
{
v = (V)(T)e.Current;
-
}
}
nally
{
... // e
}
}

,
. v
. ( 6.2)
V ( - foreach),
.
null, System.
NullReferenceException.

foreach . C# 1.0 IEnumerable<T>
. C# 2.0
foreach . C# 3.0 , ,
,

InvalidCastException .

C# foreach
-, , ,
.
, nally, :

SystemIDsposable, :
 -, nally :
nally
{
((System.IDisposable)e).Dispose();
}

 nally :

8.8.

419

nally
{
if (e != null) ((System.IDisposable)e).Dispose();
}

- -, , SystemIDsposable
.
, , nally :
nally
{
}

nally :
nally
{
System.IDisposable d = e as System.IDisposable;
if (d != null) d.Dispose();
}

d . ,
nally.

C# 2.0 foreach ,
: ,
, .

, 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();
}
}

420

8.

:
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

, ,
(target) .
,
, .
, .

try. try , . try
.
try, nally, nally try.
nally ,
nally try. ,
nally try.

using System;
class Test
{
static void Main()
{
while (true)
{
try
{
try
{
Console.WriteLine(" break");
break;
}

8.9.

421

nally
{
Console.WriteLine(" nally ");
}
}
nally
{
Console.WriteLine(" nally ");
}
}
Console.WriteLine(" break");
}
}

,
nally try.
:
Before break
Innermost nally block
Outermost nally block
After break

8.9.1. break
break switch,
while, do, for foreach.
-break:
break
;

break switch, while, do, for foreach, . break , .


switch, while, do, for foreach
, break
. , goto ( 8.9.3).
break nally,
.
.
break :
break
try, nally, nally try.
nally, nally
try. , nally
try.
break.
break ,
.

422

8.


, , ,
( ).
break, continue goto . , ,
, .

8.9.2. continue
continue while, do, for foreach.
-continue:
continue ;

continue
while, do, for foreach.
continue ,
.
switch, while, do, for, foreach
, continue
. ,
goto ( 8.9.3).
continue nally,
.
.
continue :
continue
try, nally, nally try. nally,
nally try. , nally try.
continue.
continue , .

, Java, break
continue, ,
. C# goto.
break continue
goto, . , . 78 79
Java Precisely, .

8.9.

423

8.9.3. goto
goto , .
-goto:
goto
;
goto case -
goto default
;

goto , .
goto ,
. goto , .

using System;
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:
Console.WriteLine("Found {0} at [{1}][{2}]", str, row, colm);
}
}
}

goto .
goto case switch ( 8.7.2),
. goto case
switch, -
( 6.1) switch switch
case ,
.
goto default switch ( 8.7.2),
default. goto default switch
default, .

424

8.

goto nally ( 8.10).


goto nally,
nally, .

goto C#, ,
, .

goto :
goto
try, nally, nally
try.
nally, nally try. , nally
try.
goto.
goto ,
.

, goto. ,
.


goto switch
, .


1974 goto
( goto) .
goto.
, goto ,
.
,
.
goto C# ,
, goto ,
, , ,
, .

8.9.

425

8.9.4. return
return , .
-return:
return
opt

return , , ,
void, set , add remove , , .
return , , ,
, void, get . ( 6.1) , .

return , 6.5.

return nally,
.
return :
return ,
, .
, .
return try,
nally, nally
try. nally,
nally try.
, nally try.
.
return , , ,
.

8.9.5. throw
throw .
-throw:
throw opt

throw , . System.Exception,

426

8.

System.Exception ,
System.Exception .
null, System.
NullReferenceException.

, C#, null,
try-catch .
.NET/CLI throw ,
throw C#. ,
.NET/CLI, null try-catch.

throw
catch. , catch.

throw nally,
catch.
delegate void F();
class Program
{
static void Main()
{
try
{
}
catch
{
F f = () => { throw; }; // CS0156
try
{
}
nally
{
throw; // CS0724
}
}
}
}

throw , ,
.
, catch try, .
, , , ,
, .

8.10. try

427

, catch,
. , .
try, .
try, ,
:
 try- S S
catch, catch
. catch,
, .
catch ( 8.10) .
,
catch.
 , try catch S S nally,
nally. nally ,
. , nally,
.
, .
, ,
.
, ,
, .
.

,
.
- , .

8.10. try
try ,
. , try
, ,
try.

428

8.

-try:
try

try

try

catch-
nally-
catch-

nally-

catch-:
-catch-
-catch-opt
-catch-opt
-catch-
-catch-
-catch-
-catch-
-catch-:
catch
( -

-catch-

opt

-catch-:
catch

nally-:
nally

try:
try, catch.
try, nally.
try, catch,
nally.
catch -, System.
Exception, System.Exception, , System.Exception,
.
catch - , .
, catch.
catch . catch.
catch ,
catch .
catch, , ,
catch. try catch,
, catch.
, - System.Exception,
C#.
catch. , catch
, System.Exception,
, .

8.10. try

429


C# CLR Microsoft , System.Exception, RutimeWrappedException.
, catch(Exception e) .
C# 1.0, ,
Exception, , :
[assembly:System.Runtime.CompilerServices.RuntimeCompatibility(WrapNonException
Throws = false)]

catch .
- catch ,
catch try, .
catch.
catch throw ( 8.9.5) , catch.
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;
//
}
}
static void G()
{
throw new Exception("G");
}
static void Main()
{
try
{
F();
}
catch (Exception e)

430

8.
{
Console.WriteLine("Exception in Main: " + e.Message);
}

}
}

F , , ,
. , , , :
Exception in F: G
Exception in Main: G

catch , :
Exception in F: G
Exception in Main: F

- break, continue goto


nally, . break, continue
goto nally, nally.
nally return,
.
try :
try.
try:
 try nally, .
 try.
try
try, :
 catch, , . catch,
, . catch
. catch :
 catch ,
.
 catch.
 catch:
try nally, ;
try.
 try
catch, :
try nally, ;

try.

8.10. try

431

 try catch catch


:
 try nally, ;

try.

, try-catch,
(, Visual Basic),
catch. , , nally.
, nally
, , ,
,
, .

nally , try. , try ,


break, continue, goto return
try.
nally , try.
, .
throw
( 8.9.5).

, nally ,
.


finally .
, , .Net framework inner
exception ( ). , ,
, , ,
.
. Java checked exception, ,
, .
, ( IT-)
,
.

432

8.

try try , .
catch try , .
nally try , .
try , :
try
catch.
nally , .

8.11. checked unchecked


checked unchecked
.
-checked:
checked

-unchecked:
unchecked

checked
. unchecked .
checked unchecked checked unchecked
( 7.6.12), , .

8.12. lock
lock ,
.
-lock
lock
(

lock -.
( 6.1.7),
-,
.
lock
lock (x)

-,
:
bool __lockWasTaken = false;
try
{
System.Threading.Monitor.Enter(x, __lockWasTaken);

8.12. lock

433

...
}
nally
{
if (__lockWasTaken)System.Threading.Monitor.Exit(x);
}

, lock .

lok()
-.

, . ,
.
System.Type
.
, .
private . :
class Cache
{
private static readonly object synchronizationObject = new object();
public static void Add(object x)
{
lock (Cache.synchronizationObject)
{
...
}
}
public static void Remove(object x)
{
lock (Cache.synchronizationObject)
{
...
}
}
}


( , ).
, , ,
,
, .

434

8.

8.13. using
using , , .

, using . , .
.
. using . ,
:
using(new TemporarilyStopReportingErrors()) AttemptSomething();

TemporarilyStopReportingErrors ,
,
. , .
,
. ,
try-nally.
-using
using
(
-

-:
--

, System.
IDisposable, Dispose . , , Dispose ,
. Dispose ,
.

Dispose . ,
. ,
Dispose, (),
.
, Dispose,
Dispose ,
.
,
. IDispose
.

8.13. using

435

- --,
dynamic ,
System.IDisposable. - ,
System.IDisposable.
-
.
, ++
-- .
ref out ,
.
using : , . try
nally. nally .
null, Dispose
. dynamic,
( 6.1.8) IDisposable
.
using
using (ResourceType = )

. ResourceType -:
{
ResourceType = ;
try
{
;
}
nally
{
((IDisposable) ).Dispose();
}
}

, ResourceType - ,
dynamic:
{
ResourceType = ;
try
{
;
}
nally
{
if (resource != null) ((IDisposable)).Dispose();
}
}

436

8.

, ResourceType dynamic:
{
ResourceType = ;
IDisposable d = (IDisposable) ;
try
{
;
}
nally
{
if (d != null) d.Dispose();
}
}


, d
.

using
:
using (MyClass v = ...)
using (MyStruct s = ...)
{
...
}

C#
. .
, IDisposable,
, , , int, string . . ,
( 10.5.2 7.6.4), s MyStruct , . s.SetX() s,
. ,
C# , .

using , , ,
.
using
using

, ResourceType ,
.

,
using .
using,
:

8.13. using

437

using (TextWriter output = File.CreateText("log.txt"))


using (TextReader input = File.OpenText("log.txt"))
{
//
}

- --, . using
using (ResourceType r1 = e1, r2 = e2, ..., rN = eN)

using:
using (ResourceType r1 = e1)
using (ResourceType r2 = e2)
...
using (ResourceType rN = eN)

log.txt .
.
using System;
using System.IO;
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);
}
}
}
}

TextWriter TextReader IDisposable,


using,
.

using ,
IDisposable, .


438

8.

.NET ,
.
,
using.

8.14. yield

yield return yield break C#. . , ,
.
LINQ to Objects yield.
, ,
.


. , yield return,
:
IEnumerable<int> GetSomeNumbers()
{
yield return 1;
yield return 2;
yield return 3;
}

, , .

yield ( 8.2) ( 10.14.4) ( 10.14.5) .


-yield:
yield
return
yield
break

, yield . , return break. yield


.
yield. :
, yield
-, - --.

8.14. yield

439

, yield
.
, yield
nally try.
, yield return
try, catch.

yield:
delegate IEnumerable<int> D();
IEnumerator<int> GetEnumerator()
{
try
{
yield return 1; //
yield break;
//
}
nally
{
yield return 2; // : yield
yield break;
// : yield
}
try
{
yield return 3; // : yield
yield break;
//
}
catch
{
yield return 4; // : yield
yield break;
//
}

nally
nally

return try...catch

return try...catch

D d = delegate
{
yield return 5; // : yield
};
}
int MyMethod()
{
yield return 1;
}

// :

( 6.1)
yield return ( 10.14.3).
yield return :
,
Current .
. yield return
try,
nally .

440

8.

MoveNext true, ,
.
MoveNext
, .
yield break :
yield break
try nally,
nally try.
nally, nally try. , nally
try.
.
MoveNext, Dispose .
yield break ,
.

, yield return , return,
yield return . return
:
int F()
{
return 1;
return 2; //
}

, yield return :
IEnumerable<int> F() {
yield return 1;
yield return 2; //
}

if:
IEnumerable<int> F()
{
if(...) { yield return 1; } // ,
//
yield return 2;
// !
}

, yield return
( return).


C# 1.0 ( foreach). C# 2.0 ( yield).


C# . , .
using ( 9.4) .

, .
,
.

9.1.
- .
using,
-, ---.
-:
--opt
-usingopt
---

-opt

C# ,
.
C# , . , , ,
.

C# ,
++.

-using - ---
.

442

9.


. ,
, ,
. ,
using (
) .


. , C#, . C#
, ,
, Jscript.

- ( 17)
. . .
--- , .
A.cs:
class A {}

B.cs:
class B {}


, .
,
.

9.2.
-- namespace, .
, .
--:
namespace
-
-:

-- opt

--:
-usingopt
{ --opt
--- opt }

; opt

9.2.

443

--
- -. --
-, .
-- --, . ,
, .
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 { }
}

, N1.N2. N1.N2..

444

9.

,
, .

9.3.
-- ,
() . ,
, . ,
, .
--:
--
--
--:
extern alias

--

-- using, - ---
,
.
, -,
, .
global, .
--
,
. , -- ,
, .

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. : X.N.B Y.N.B
X::N.B Y::N.B. ,
, .

9.4. using

445

9.4. using
using
. using --- ( 3.8) - ( 7.6.2), using
,
.
-using:
-using
-using

-using

-using:
using--
using---

Using-- ( 9.4.1)
.
Using--- ( 9.4.2) .
using
.
using
using. , using ,
.

9.4.1. Using-
Using-- , .
using--:
using

---

Using--

, . :
namespace N1.N2
{
class A { }
}
namespace N3
{
using A = N1.N2.A;
class B: A { }
}

N3
N1.N2.A, , ,
N1.N2.A. , R
N1.N2 R.A:

446

9.

namespace N3
{
using R = N1.N2;
class B: R.A { }
}

using
, . :
namespace N3
{
class A { }
}
namespace N3
{
using A = N1.N2.A; // :
}

N3 , , . using
.
Using--
, . , using--
, .

namespace N3
{
using R = N1.N2;
}
namespace N3
{
class B: R.A { } // : R
}

R
, , , R
. , using-- R
,
:
using R = N1.N2;
namespace N3
{
class B: R.A {}
}
namespace N3
{
class C: R.A {}
}

9.4. using

447

, , using-, ,
. :
using R = N1.N2;
namespace N3
{
class R {}
class B: R.A {} // : R A
}

R.A ,
R N3.R, N1.N2.
using-- , ----, ,
using
. , ----
using-- ,
using. using-- --,
.
:
namespace N1.N2 { }
namespace N3
{
extern alias E;
using R1 = E.N;
using R2 = N1;
using R3 = N1.N2;
using R4 = R2.N2;
}

//
//
//
//

: R2

using ,
using--. using- ,
.
Using--
, , , .

, . :
namespace N1.N2
{
class A { }
}
namespace N3
{
using R1 = N1;
using R2 = N1.N2;
class B

448

9.

{
N1.N2.A a;
R1.N2.A b;
R2.A c;

// N1.N2.A
// N1.N2.A
// N1.N2.A

}
}

N1.N2.A, R1.N2.A, R2.A ,


N1.N2.A.
,
.
:
namespace N1
{
class A<T>
{
class B
}
}
namespace N2
{
using W =
using X =
using Y =

{}

N1.A;
// :
N1.A.B; // :
N1.A<int>; // :
//
using Z<T> = N1.A<T>; // : using-
// -

9.4.2. Using-
Using--- , , .
,
.
using---:
using
--
;

,
using---,
. :
namespace N1.N2
{
class A { }
}
namespace N3
{
using N1.N2;
class B: A { }
}

N3
N1.N2, N3.B N1.N2..

9.4. using

449

Using---
, . :
namespace N1.N2
{
class A { }
}
namespace N3
{
using N1;
class B: N2.A { } // : N2
}

Using--- N1 ,
. ,
N2.A ,
N2.
using--, using-- , ,
.
.
:
namespace N1.N2
{
class A { }
class B { }
}
namespace N3
{
using N1.N2;
class A { }
}

N3
N3.A, N1.N2..
,
. :
namespace N1
{
class A { }
}
namespace N2
{
class A { }
}
namespace N3
{
using N1;
using N2;
class B : A { } // : A
}

450

9.

N1 N2 , N3,
.
, using--, . :
namespace N3
{
using N1;
using N2;
using A = N1.A;
class B: A { } // A N1.A
}

using--, using---
.
--, using---, , ---- using--. using---
.

9.5.
--- -- ( 9.2), - ( 9.6).
---:
---
---

---

---:
--
-

--, .

9.6.
- - ( 10.1), - ( 11.1), - ( 13.1), -
( 14.1) - ( 15.1).
-:
-
-
-
-
-

9.7.

451

- , .
, .
, ,
N.T, N , ,
.
, ,
( 10.3.8).
, ,
, , ( 3.5.1):
 , ,
public internal. internal.
 , , public, protected
internal, internal private. private.
 , , public,
internal private. private.

9.7.

:: ,
. , .
.,
:: , using.
- :
-:

::

--opt

- --- ( 3.8) -- ( 7.6.4).


- :
N :: I <A1,,Ak> , N I , <A1,,Ak>
- (k 1).
N :: I , N I ( k 0).
- :

452

9.

N global, I :
 I k
, - .
 ,
I k , -
.
 , I,
k -, - , -.
 -
.
, ( 9.2),
- ( ), ( ), , , ,
:
 using-, N - ,
- ,
.
 ,
-- using--, N - , :
 , N,
I K , -
.
 , , N, I K , -
.
 , , N, I, K -, -
, -.
 - , .
- ,
.
,
, , .
, N global, , , global
- .

9.7.

453

9.7.1.

using-. ,
using-
using-,
, ,
,
::.

namespace N
{
public class A { }
public class B { }
}
namespace N
{
using A = System.IO;
class X
{
A.Stream s1;
// : A
A::Stream s2;
//
}
}

, , using- .
.stream
. ::
, .

10

, ( ), (, , , , , , ), . ,
.

10.1.
- - ( 9.6),
.
-:
opt
-opt
partialopt
class
--opt
-opt
---op
-

; opt

- ( 17),
- ( 10.1.1),
partial, class , , - ( 10.1.3), -
( 10.1.4), --- ( 10.1.5), - ( 10.1.6) .
---
--.
, --, . , ,
,
, -.

10.1.1
- :
-:
-
-

10.1.

455

-:
new
public
protected
internal
private
abstract
sealed
static


, .
new.
10.3.4, ,
. new
, , .
public, protected, internal private
. , , ( 3.5.1).
abstract, sealed static
.

10.1.1.1.
abstract ,

. :
, new .
,
, null
,
.
( )
.
(sealed).
,
,
. :
abstract class A
{
public abstract void F();
}
abstract class B: A
{
public void G() {}
}

456

10.

class C: B
{
public override void F() {
// F
}
}

A F. B
G, F,
B . C F . C ,
( ) .

10.1.1.2.
sealed ,
.
, .
.
sealed ,
. ,

, , .

( )
. , .
:
, , , . , ,
, , .


.
, ,
. ,
, , , ,
, .
sealed , , : . ,
- .

10.1.

457

10.1.1.3.
static , . ,
.
( 10.6.9) .
:
sealed abstract.
,
, , .
- ( 10.1.4)
. object.
( 10.3.7). ,
.
protected
protected internal.
.
.
( 10.11.4).
,
static (
). ,
,
static.

C# 1.0
. ,
,

.

10.1.1.3.1.
---- ( 3.8)
, :
T ---- T.I,

458

10.

T -typeof ( 7.5.11)
typeof(T).
- ( 7.5) ,
E --- ( 7.5.4)
E.I.
. ,
, ( 10.3.8) ,
-. , , , new,
, is, as, sizeof,
.

10.1.2. partial
partial , -
.

,
10.2.
,
. , , .
.

,
, , . ,
, . ,
, , ,
.

10.1.3. -
- ,
-,
. - , . , - ( 4.4.1) ,
- .
--:
<
-
>

10.1.
-:
opt
-
-
,
opt

459

-:

- ( 3.3) . , -
- ,
. .

10.1.4.
-, , ( 13), .
-:
:

:
-
:

,
-
-:

, , ( 4.4). , -,
.
class Extend<V>: V {}

// : -
//


, . ,
(mixin). ,
, V . , V
, Extend<V> .

10.1.4.1.
-,
. - ,
, object.
,
10.3.3.

460

10.

:
class A {}
class B: A {}

A B, B
A. A ,
object.
,
, -
- . :
class B<U,V> {}
class G<T>: B<string,T[]> {}

G < i n t >
B<string,int[]>.

, ( 3.5.2). , public
private internal , .

: System.Array, System.Delegate, System.MulticastDelegate, System.
Enum System.ValueType. , System.Attribute.
A B , B object.
, ,
. :
class A<T> {
public class B{}
}
class C : A<C.B> {}

, A<C.B> , C object; ( 3.8), , C


B.
,
. ,
. B A
object. :
class A {...}
class B<T>: A {...}

10.1.

461

class C<T>: B<IComparable<T>> {...}


class D<T>: C<T[]> {...}

D<int> C<int[]>, B<IComparable<int[]>>,


A object.
object, . object
.
B A A B,
. ( ) ,
( ).
, , ,

.

-, . ,
A<T> A<A<T>> ,
:
class A<T> : A<A<T>> { }

, -
, , :
class A<T> { }
class B : A<B[]> { } //

:
class A: A {}

, . ,

class A: B {}
class B: C {}
class C: A {}

, . ,

class A: B.C {}
class B: A
{
public class C {}
}

, A B.C ( ), B ( ), A.

462

10.

, . :
class A
{
class B: A {}
}

B A ( A , ), A B ( B , A).
, .
sealed. :
sealed class A {}
class B: A {}

// :

B , A.

10.1.4.2.
- .
, .
13.4.

10.1.5. -
- ---:
---:
---
---
---:
where
-
:

---

----

----:
-
-
-
-
,
-
-
,
-
-
,
-
-
,
-
,
-:

class
struct
-:

-
-

10.1.
-
-:
new
(
)

463

--- where,
-, . -
where, .
get set , where .
where
: , , new().
,
class - struct.
- .
, -,
-, . , , , , -,
( ),
.
- , -,
-, -.
, -, -, .
- ( 4.1.10),
-. -,
-, -.

- .
, , . , C# 3.0
-
, . - , C#
.

-
, -.
, -, ,
-, -.
-
- .
- , 4.4.4.

464

10.

:
.
.
System.Array, System.Delegate, System.Enum System.
ValueType.

System.Enum
System.Delegate, , ,
where T : enum, where T : struct, Enum,
. C# CLI, ,
CLI, . ,
ECMA-335 , C#. ,
; ,
.

object. object,
, .
- .
, , :
.
where.
, -,
, , .
, , ( 3.5.4),
.
, -,
:
-.
where.
, - .
, :
- T - S,
S T.
- S - T T U, S U.

10.1.

465

( ) -
.
. - 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.6.10.1). -, - ,
(
-) -, ( . 10.1.5).
:
interface IPrintable
{
void Print();
}
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> {...}

466

10.

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 struct
{
...
}

- T :
T -,
object.
T -,
System.ValueType.
T C, -,
C.
T ,
-, -

10.1.

467

(encompassed) ( 6.4.3)
-. , .
T -,
( 6.4.3) , T -.
,
.
T , ,
object.
, T V , V, . , ,

.
,
.

, , :
class B<T> { public virtual void M<U>() where U : T {} }
class D : B<DateTime> { public override void M<V>() }

V System.ValueType,
DateTime. , DateTime
DateTime[], System.Array,
DateTime[].

- T :
T -,
.
T , , .
T , ,
-.
T ,
-.

468

10.

- ,
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

10.2.
. , ,
.
-, - -
, partial. ,
partial ,
class, struct
interface void . .

partial.
, . partial ,
- ,
; partial ,
.
,
.
.

10.2.

469

partial.
,
.
partial
.

, , , .
.
,
.


, ,
,
;
, . . , , , ASP.NET
WinForms. , , ,
. , ,
.
, .

, .

10.2.1.
.
. :
[Attr1, Attr2("")]
partial class A {}
[Attr3, Attr2("")]
partial class A {}

:
[Attr1, Attr2(""), Attr3, Attr2("")]
class A {}

-.

470

10.

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). , .
, .
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>

10.2.

471

{
...
}
partial class Dictionary<K,V>
{
...
}

, , (
),
-.

-
. .

10.2.4.
,
, .
, System.Object ( 10.1.4.1).

10.2.5.
, , ,
. ,
.
.
:
partial class C: IA, IB {...}
partial class C: IC {...}
partial class C: IA, IB {...}

C IA, IB IC.
()
(), .
, :

472

10.

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 Inner


{
int y;
}

// : x
//
// : Inner

}
partial class A
{
int x;

partial class Inner


{
int z;
}
}

// : x
//
// : Inner

10.2.

473

C#,
. ,
, .

, C# , : ,
( 10.5.5), .
,
: , ,
, , .
, , ,
,
.

10.2.7.
. ;
,
, .


. , , .

,
. void,
out. partial ,
void;
.
.
.
, . ,
.
.
,

474

10.

,
:
( ), , -
.
( ) (
-).
-
( -).
,
.

. , , , .
void, -. ,
,
, .

, -, , .
, , ,
.
.
,
. , , :
partial.
,
. .


, . .
M ,
, :
( 7.6.10.5) .
M ,
( 6.5.2), .

10.2.

475

, 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);
partial void OnNameChanged();
}

- ,
,
:
class Customer
{
string name;
public string Name {
get { return name; }
set { name = value; }
}
}

, , , :
partial class Customer
{
partial void OnNameChanging(string newName)
{
Console.WriteLine(name + " " + newName);
}
partial void OnNameChanged()
{
Console.WriteLine(" " + name);
}
}

476

10.

:
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);
}
}



:
class Base {
public void Foo() { HelpWithFoo(); ... }
//
protected virtual void HelpWithFoo() {}
//
}
class Derived : Base {
protected override void HelpWithFoo() { ... }
}

,

, .
:
partial class MyClass {
//
public void Foo() { HelpWithFoo(); ... }
//
partial void HelpWithFoo();
//
}

10.2.

477

partial class MyClass {


//
partial void HelpWithFoo() { ... }
//
}


. , ,
,
. , , , , .
,
.
:
, .

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;
partial class A
{
List y;
}
}

// y Widgets.LinkedList

478

10.

10.3.
,
--, , .
--:
--
--

--

--:
-
-
-
-
-
-
-
-
-
--
-

:
, , ( 10.4).
, ( 10.5).
, ,
( 10.6).
, ,
( 10.7).
, ,
( 10.8).
,
(), ( 10.9).
, ,
( 10.10).
, , ( 10.11).
, , ( 10.13).
, , ( 10.12).
, , ( 10.3.8).
, , . , , ,

10.3.

479

, , ,
.
- ( 3.3.), -, -,
. --
:
,
, .
.
, , ,
, .
, . ,
( 3.6) ,
, ,
, ref out.

, , , ,
ref out.
, .
,
.
( 10.3.3)
. ,
,
( , , ).

10.3.1.
(bound)
( 4.4.3), .

( 4.4) , - -.
-, ,
. ,
, this. .
:

480

10.

class A<T>

// : A<T>

class B {}

// : A<T>.B

class C<U> {}

// : A<T>.C<U>

}
class D {}

// : D

10.3.2.

-
- .

.
:
class Gen<T,U>
{
public T[,] a;
public void G(int i, T t, Gen<U,T> gt) {...}
public U Prop { get {...} set {...} }
public int H(double d) {...}
}

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) .

. ?

10.3.

481

public class Puzzle<T> {


public void Method(int i) {}
public void Method(T t) {}
}
...
new Puzzle<int>().Method(10);

, Method(10)? ,
, .
.
, , ,
, (int),
(-).


C# 2.0
, ,
. , ,
, , :
class C<T> {
public C(T t) { ... }
public C(Stream serializedState) { ... }
}

C<T> , -
- C<Stream>? .
, ,
. CLR, .

- ,
. (closed) ( 4.4.2)
, - -, .
:
class C<V>
{
public V f1;
public C<V> f2 = null;
public C(V x) {
this.f1 = x;
this.f2 = this;
}
}

482

10.

class Application
{
static void Main() {
C<int> x1 = new C<int>(1);
Console.WriteLine(x1.f1);

// 1

C<double> x2 = new C<double>(3.1415);


Console.WriteLine(x2.f1);
// 3.1415
}
}

10.3.3.
. , , , .
:
. C B, B A,
C , B, , A.
. ,
.
, ,
( 3.5). .

, . ,
. ,
,
( object). FileStream
?

( 3.7.1.2) ,
. ,
.
,
, ( 6.1.6)
. ,

10.3.

483


.
, ,
. , ,
, ,
,
.

( 10.1.4.1),
- --
- . , , - - --.
class B<U>
{
public U F(long index) {...}
}
class D<T>: B<T[]>
{
public T G(string s) {...}
}

D<int> public int G(string s), - T


- int. D<int> B. :
T int B<T[]>
D<int>, B<int[]>; U public U F(long
index) - B int[], public int[] F(long index).

10.3.4. new
--
, . , , .
,
. ,
new, ,
.
3.7.1.2.
new , , . ,
new.

484

10.

10.3.5.
--
( 3.5.1): public, protected internal, protected, internal
private. protected internal,
. - ,
private.

10.3.6.
, ,
. ,
, , , , , ,
.
,
( 3.5.4).

10.3.7.


. ,
,
( ).
, , , , static, .
, .
:
M --
( 7.6.4) E.M, E , M. E
, .
, . ,
, .
(, , ,
) ,
this .
, , , , ,
static, ( ).
:

10.3.

485

M --
( 7.6.4) E.M, E , M.
E , .
.
(, , , )
this ( 7.6.7).

:
class Test
{
int x;
static int y;
void F() {
x = 1;
y = 1;
}

// : , this.x = 1
// : , Test.y = 1

static void G() {


x = 1;
y = 1;
}

// : this.x
// : , Test.y = 1

static void Main() {


Test t = new Test();
t.x = 1;
t.y = 1;
Test.x = 1;
Test.y = 1;

//
//
//
//
//
//


:

:

}
}

F , -
( 7.6.2) ,
.
G , - .
Main , -- ( 7.6.4) , .

486

10.

10.3.8.
, , . ,
, .

, , , . ,
, ,
,
. , ,
.

:
using System;
class A
{
class B
{
static void F() {
Console.WriteLine("A.B.F");
}
}
}

B , A, B ,
.

,
.
. ,
,
.

10.3.8.1.
( 3.8.1) S.N, S
, N.

10.3.8.2.
public internal,
internal.

10.3.

487

, ,
, :
, , (public, protected internal, protected, internal private) ,
, private.
, ,
(public, internal private) , ,
private.
:
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 rst = null;
private Node last = null;
//
public void AddToFront(object o) {...}
public void AddToBack(object o) {...}
public object RemoveFromFront() {...}
public object RemoveFromBack() {...}
public int Count { get {...} }
}

Node.

10.3.8.3.
( 3.7.1) . new,
. :
class Base
{
public static void M() {
Console.WriteLine("Base.M");
}
}

488

10.

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,
Base M.

10.3.8.4. this-

this- ( 7.6.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);
}
}
}

10.3.

489

class Test
{
static void Main() {
C c = new C();
c.F();
}
}

. C
Nested this Nested,
C.

: , ? -
? , Java
. C# ,
static, C#!

10.3.8.5.

,
, ,
private protected. :
using System;
class C
{
private static void F() {
Console.WriteLine("C.F");
}
public class Nested
{
public static void G() {
F();
}
}
}
class Test
{
static void Main() {
C.Nested.G();
}
}

C, Nested. G, Nested, F, C
private.

490

10.


Java,
, . , C#,
. ,
:
. ,
Java.

,
. :
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,
Derived Base,
Derived.

10.3.8.6.
.
- .
-,
.
,
, . ,

10.3.

491

,
-.
; .

, 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#, , , ,
,
(kind) , . ,
,
, .
; ,
. ( 10.3.3)
new ( 10.3.4).

492

10.

:
get set C#;
get set C#;
, ,
, , C#.
( 10.13)
( 10.3.9.4).

10.3.9.1. ,

P ( 10.7) T :
T get_P();
void set_P(T value);

,
.

get_<-> set_<->, <> , . ,
.

:
using System;
class A
{
public int P {
get { return 123; }
}
}
class B: A
{
new public int get_P() {
return 456;
}
new public void set_P(int value) {
}
}
class Test
{

10.4.

493

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

10.3.9.2. ,

E ( 10.8), T, :
void add_E(T handler);
void remove_E(T handler);

10.3.9.3. ,

( 10.9) T L
:
T get_Item(L);
void set_Item(L, T value);

, . ,
Item.

10.3.9.4. ,

, ( 10.13),
:
void Finalize();

10.4.
, ,
, . -
.

494

10.

-:
opt
-opt
-:
-
-

const

-:
new
public
protected
internal
private
-:
-
-
-:

- ( 17), new ( 10.3.4) ( 10.3.5). ,


-.
, -
static. , .
- , . -,
. - , , = - ( 7.19),
.
, , sbyte, byte, short,
ushort, int, uint, long, ulong, char, oat, double, decimal, bool, string, -. -
, ( 6.1).

decimal,
. CLI
, decimal . Microsoft C# ,
, DecimalConstantAttribute. DateTime, C# .

,
( 3.5.4).

10.4.

495

, -
( 7.6.2) -- ( 7.6.4).
- . , ,
-.
case, goto case, enum,
.
7.19, - ,
.
-, string, new, -
, -,
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;
}

496

10.

class B
{
public const int Z = A.Y + 1;
}

A.Y, B.Z , A.X,


10, 11 12, .
,
. , A B
, A.X B.Z, B.Z
A.Y.

10.5.
, ,
. - .
-:
-opt
opt
-:
-
-

-:
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.

497

- , .
-, . - ,
, = ( 10.5.5), .
, ( 3.5.4).
, - ( 7.6.2) -- ( 7.6.4). , , ( 7.17).
, , ,
( 7.6.9) ( 7.7.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;
}

10.5.1.
static, , , . static ,
, , .
( 5), C#, .
; , ( 4.4.2).
, ,
. :
class C<V>
{
static int count = 0;
public C() {
count++;
}

498

10.
public static int Count {
get { return count; }

}
}
class Application
{
static void Main() {
C<int> x1 = new C<int>();
Console.WriteLine(C<int>.Count);

// 1

C<double> x2 = new C<double>();


Console.WriteLine(C<int>.Count);

// 1

C<int> x3 = new C<int>();


Console.WriteLine(C<int>.Count);

// 2

}
}

. ,
.
-- ( 7.6.4)
E.M, M , E , M;
M , E , M.
10.3.7.

10.5.2. ,
- readonly, ,
, , . ,

( readonly
). , , readonly,
:
-, (
-).
, ; ,
. , readonly
ref out .
, , ref out
.

10.5.

499


readonly
this, :
class A
{
readonly int x;
A(A a)
{
this.x = 1;
a.x = 1;

//
//
//
//


CS0191:
,

}
}

, readonly (
ref out) , .

10.5.2.1. ,
,
, static readonly, ,
, const

.
:
public class Color
{
public static readonly
public static readonly
public static readonly
public static readonly
public static readonly

Color
Color
Color
Color
Color

Black = new Color(0, 0, 0);


White = new Color(255, 255, 255);
Red = new Color(255, 0, 0);
Green = new Color(0, 255, 0);
Blue = new Color(0, 0, 255);

private byte red, green, blue;


public Color(byte r, byte g, byte b) {
red = r;
green = g;
blue = b;
}
}

Black, White, Red, Green Blue


,
. static readonly
.

500

10.

10.5.2.2.
,
readonly
. ,
, readonly,
. , :
using System;
namespace Program1
{
public class Utils
{
public static readonly int X = 1;
}
}
namespace Program2
{
class Test
{
static void Main() {
Console.WriteLine(Program1.Utils.X);
}
}
}

Program1 Program2 , . Program1.Utils.X static


readonly, , Console.WriteLine,
. , X Program1, Console.WriteLine
, Program2 .
X ,
Program2 Program1 Program2.

, readonly,
const.


, readonly
, const . , , ,
. -

10.5.

501

. ,
int. ,
, const. ,
readonly.

10.5.3. -
- volatile, ,
, - .
, -, ,
, ,
, , , -lock ( 8.12). ,
. - :
- (volatile
read). (acquire semantics),
,
, .
- (volatile write). (release
semantics), ,
, .
, , , , .
,
. -
:
-.
byte, sbyte, short, ushort, int, uint, char, oat, bool, System.IntPtr
System.UIntPtr.
-, byte,
sbyte, short, ushort, int uint.
:
using System;
using System.Threading;

502

10.

class Test
{
public static int result;
public static volatile bool nished;
static void Thread2() {
result = 143;
nished = true;
}
static void Main() {
nished = false;
// Thread2()
new Thread(new ThreadStart(Thread2)).Start();
// , Thread2 ,
// nished true
for (;;) {
if (nished) {
Console.WriteLine("result = {0}", result);
return;
}
}
}
}

:
result = 143

Main ,
Thread2. result, , true -
nished. , nished true,
result. nished volatile,
result 143. nished
volatile, result
nished,
0. nished
volatile .

, lock
( 8.12), volatile . ,
,
, Monitor.Enter Monitor.Exit,
.


, , .
,

10.5.

503

. , , , . , , - , .

10.5.4.
, , ,
( 5.2) .
, , . :
using System;
class Test
{
static bool b;
int i;
static void Main() {
Test t = new Test();
Console.WriteLine("b = {0}, i = {1}", b, t.i);
}
}


b = False, i = 0

b i .

10.5.5.
-.
, . , . :
using System;
class Test
{
static double x = Math.Sqrt(2.0);
int i = 100;
string s = "";
static void Main() {
Test a = new Test();
Console.WriteLine("x = {0}, i = {1}, s = {2}", x, a.i, a.s);
}
}

504

10.


x = 1.4142135623731, i = 100, s =

x
, i s
.
, 10.5.4, , , .
, ,
. ,

, .
, , , ,
. :
using System;
class Test
{
static int a = b + 1;
static int b = a + 1;
static void Main() {
Console.WriteLine("a = {0}, b = {1}", a, b);
}
}

.
a b, .
a = 1, b = 2

,
a b, 0 (
int). a, b 0, a
1. b, a
1, b 2.

,
. , , , , .

10.5.5.1.

, ,

10.5.

505

.
( 10.12), .
,
. :
using System;
class Test
{
static void Main() {
Console.WriteLine("{0} {1}", B.Y, A.X);
}
public static int F(string s) {
Console.WriteLine(s);
return 1;
}
}
class A
{
public static int X = Test.F(" A");
}
class B
{
public static int Y = Test.F(" B");
}


A
B
1 1

B
A
1 1

, X Y
; , . ,
, :
using System;
class Test
{
static void Main() {
Console.WriteLine("{0} {1}", B.Y, A.X);
}
public static int F(string s) {
Console.WriteLine(s);
return 1;
}
}

506

10.

class A
{
static A() {}
public static int X = Test.F(" A");
}
class B
{
static B() {}
public static int Y = Test.F(" B");
}


B
A
1 1

, 10.12, B ( ,
B)
A.

10.5.5.2.

,
( 10.11.1) . ,
. 10.11.
. , this ,
- . :
class A
{
int x = 1;
int y = x + 1; // : this
}

y ,
.

10.6.
, , . -.
-:
-

10.6.
-:
opt
-opt
partialopt
--
-
( --opt )
---opt
-:
-
-

507

--opt

-:
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).
.
: static,
virtual override.
: new
override.
abstract,
: static, virtual, sealed extern.

508

10.

private,
: virtual, override abstract.
sealed, override.
partial,
: new, public, protected, internal, private,
virtual, sealed, override, abstract extern.
-- ,
. ,
-- void. void
, partial.
- . ( 13.4.1), - . -
, . .
-- -
( 10.1.3). , . extern, - .
--
( 10.6.1).
---
- ( 10.1.5) ,
-- override.
-- , - , ,
( 3.5.4).
, abstract extern, . , partial,
- , .
- , , .
, - ( 3.6). , ,
-, ,
. - ,
, , - .
, -
.
, . ,
,

10.6.

509

, , ,
ref out.
- -, - ---,
.
- .

10.6.1.
, , --
.
--:
-
-
,
-
-:
-
-

-:
opt
-opt
--opt

--:
=

-:
ref
out
this
-:
opt
params

, ,
-.
- ( 17), ref, out this, ,
--. -
. this
. 10.6.9.
-, --,
, -,
, . --.

510

10.


. ,
. ,
, .

ref out --.


-- :
-.
new S(), S -.
default(S), S -.

.
( 10.2.7), ( 13.4.1) ( 10.9), ,
, .
- ( 17),
params, - . - . -
( 12.1).
-
, ( ) ,
. - 10.6.1.4.
- ,
.
:
public void M(
ref int
decimal
bool
bool?
string
object
T
params
) { }

i,
d,
b = false,
n = false,
s = "",
o = null,
t = default(T),
int[] a

-- M i
ref, d -, b, s, o t
-, a -.
,
- .

10.6.

511

-,
.
, .

, .
( 7.6.5.1)
,
.
- ( 7.6.2).
:
-, - .
-, ref.
, out.
-, params.
3.6, ref out
, params .

10.6.1.1. -
, , -.
- , , .
-, ,
( 6.1) .
-. ,
; ,
.

10.6.1.2. -
, ref, -.
-, -
. - ,
, .
-,
ref,
-- ( 5.3.3) , . ,
-.
- .

512

10.

, ( 10.14),
-.
:
using System;
class Test
{
static void Swap(ref int x, ref int y) {
int temp = x;
x = y;
y = temp;
}
static void Main() {
int i = 1, j = 2;
Swap(ref i, ref j);
Console.WriteLine("i = {0}, j = {1}", i, j);
}
}

:
i = 2, j = 1

Swap Main x i, y j. ,
i j.
, -,
, . :
class A
{
string s;
void
s
a
b
}

F(ref string a, ref string b) {


= "";
= "";
= "";

void G() {
F(ref s, ref s);
}
}

F G s a, b. ,
s, a b ,
s.

10.6.1.3.
, out, .
-, . , , .

10.6.

513

,
out,
-- ( 5.3.3) , . , , , ,
, .
,
.
.
, ( 10.2.7) ( 10.14), .
, .

, struct class,
. ,
Tuple<>.


int.TryParse .NET. : , .
- C# ,
:
bool TryParse(string s, out int result)

:
int? TryParse(string s)

, decimal.TryParse.

:
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--;
}

514

10.
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 , ,
.

10.6.1.4. -
, params, -.
-,
. ,
string[] string[][] , string[,] .
params ref out.
-
:
, -, , ( 6.1) . - ,
-.
- ( ) ,
, ( 6.1)
-. , ,

.

, - - ( 10.6.1.1) .
:
using System;
class Test
{

10.6.

515

static void F(params int[] args) {


Console.Write(" {0} :", args.Length);
foreach (int i in args)
Console.Write(" {0}", i);
Console.WriteLine();
}
static void Main() {
int[] arr = {1, 2, 3};
F(arr);
F(10, 20, 30, 40);
F();
}
}

:
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.5.3.1). , ,
, .
:
using System;
class Test
{
static void F(params object[] a) {
Console.WriteLine("F(object[])");
}
static void F() {
Console.WriteLine("F()");
}
static void F(object a0, object a1) {
Console.WriteLine("F(object,object)");
}
static void Main() {
F();
F(1);

516

10.
F(1, 2);
F(1, 2, 3);
F(1, 2, 3, 4);

}
}

:
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;

10.6.

517

F(a);
F((object)a);
F(o);
F((object[])o);
}
}

:
System.Int32 System.String System.Double
System.Object[]
System.Object[]
System.Int32 System.String System.Double

F ,
(
object[]). , F, -.
,
( object object[]). F,
. object[] ,
(
object[]).

10.6.2.

static, . static ,
.
,
this .

this ( 7.6.7).
-- ( 7.6.4)
E.M M , E , M;
M , E , M.
10.3.7.

10.6.3.
virtual,
. virtual ,
.

518

10.


,
. ,
. .


, , : (
) , .
,
, , ,
, .

:
, ,
, . , .

( 10.6.4).
,
, , . . , N
A ,
C R ( R C, , C), :
C, N A , M , ,
. 7.6.5.1.
, M , M.
M ,
R.
, ,
. M
R :
R M
virtual, M .
, R M (
override), M .
M R M
R.

10.6.

519

:
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"); }
public override void G() { Console.WriteLine("B.G"); }
}
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

, a.G() B.G, A.G. , , , ( B),


( A).
,
.
, ,
, . :
using System;
class A
{
public virtual void F() { Console.WriteLine("A.F"); }
}

520

10.

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. , override D
, C, D
, A. :
B.F
B.F
D.F
D.F

, , D , .

10.6.4.
override, .
.
,
,
.
, override, . M,
C,

10.6.

521

C, C
, , ,
M, .
, public, protected
protected internal internal
, C.
, :
.
. , , -
.
,
. ,
.
.

.

. ,
. protected internal ,
, ,
protected.
---.
, . , , - , -
. ,
, - .

:
abstract class C<T>
{
public virtual T F() {...}
public virtual C<T> G() {...}
public virtual void H(C<T> x) {...}
}

522

10.

class D: C<string>
{
public override string F() {...}

//

public override C<string> G() {...}

//

public override void H(C<T> x) {...}

// : C<string>

}
class E<T,U>: C<U>
{
public override U F() {...}

//

public override C<U> G() {...}

//

public override void H(C<T> x) {...}

// : C<U>

base- ( 7.6.8). :
class A
{
int x;
public virtual void PrintFields() {
Console.WriteLine("x = {0}", x);
}
}
class B: A
{
int y;
public override void PrintFields() {
base.PrintFields();
Console.WriteLine("y = {0}", y);
}
}

base.PrintFields() B PrintFields, A.
Base-
. B ((A)this).
PrintFields(), PrintFields, B,
A, PrintFields ,
((A)this) B.

override. , , .
:
class A
{
public virtual void F() {}
}

10.6.
class B: A
{
public virtual void F() {}

523

// :
// F()

F B override , ,
F A. , 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

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");
}
}

524

10.

class B: A
{
sealed override public void F() {
Console.WriteLine("B.F");
}
override public void G() {
Console.WriteLine("B.G");
}
}
class C: B
{
override public void G() {
Console.WriteLine("C.G");
}
}

B : F sealed G . sealed B
C F .

, ,
,
.
, , ,
. , ,
.
, sealed .
, . , .

10.6.6.
abstract,
.
, virtual.
,
. ,
. ,
- .

( 10.1.1.1).

10.6.

525

:
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 .
base- ( 7.6.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");
}
}

526

10.

abstract class B: A
{
public abstract override void F();
}
class C: B
{
public override void F() {
Console.WriteLine("C.F");
}
}

A , B
, C , .

, ,
private.
, - internal abstract ,
public abstract;
, public.

10.6.7.
extern,
. ,
C# .
, -
. .
extern DllImport
( 17.5.1), (Dynamic Link Library, 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);

10.6.

527

[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,
.

dynamic (
object). T, T -
, :
public static void Foo<T>(this T x) { ... }

, T
, T.

, :
using System;
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;
}
}

528

10.

. ,
,
( 7.6.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());
}
}
}

Slice string[], ToInt32 string,


. ,
,
:
static class Program
{
static void Main() {
string[] strings = { "1", "22", "333", "4444" };
foreach (string s in Extensions.Slice(strings, 1, 2)) {
Console.WriteLine(Extensions.ToInt32(s));
}
}
}


Slice :
T T.
, (
IsSorted), , ( ConcatWith). , , , ,
( 10.6.1):
public static bool IsSorted<T>(this IEnumerable<T> xs)
where T : IComparable<T>
{ ... }
public static string ConcatWith(this IEnumerable<String> xs,
string glue = ", ")
{ ... }


, , , , ,

10.6.

529

, . ,
.
object, ,
-, , .


. toString, t , :
public static string toString(this object x, params object[] args) {
return " ToString(), !";
}

;
ToString ( T) YourType,
. ,
. o.ToString() Object.ToString()
, , :
public static string ToString(this YourType x) { ... }

10.6.10.
- , .
,
, .
( 8.2), , .
void,
return ( 8.9.4) .
, void,
( ), .
void, return ,
. ,
, . , ,
, . :
class A
{
public int F() {}

// :

public int G() {


return 1;
}

530

10.
public int H(bool b) {
if (b) {
return 1;
}
else {
return 0;
}

}
}

F , . G H
,
return, .

10.6.11.
7.5.2.

10.7.
,
. ,
, , . . : ,
, .
, , ,
. ,
; ,
.
-:
-:
opt
-opt
{ -- }
-:
-
-
-:
new
public
protected
internal
private
static
virtual

10.7.

531

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.6.7).

532

10.

-- ( 7.6.4)
E.M M , E , M;
M , E , M.
10.3.7.

10.7.2.
-- , .
--:
--
--

--opt
--opt

--:
opt
--opt

get

--

--:
--opt
opt

set

--

--:
protected
internal
private
protected internal
internal protected
--:

--, - .
get set, --
--.
--
:
--
.
-- ,
override, ,
(get set),
.
-- , override, -- (
) .
-- ,
, . :

10.7.

533

 public, -- protected internal, internal,


protected private.
 protected
internal, -- internal,
protected private.
 internal
protected, -- private.
 private, -- .
-- . , ,
, ,
( 10.7.3).
, , - , ,
.
(get) ,
.
, ,
, ( 7.1.1).
, ,
10.6.10. , return, , , . ,
.
, -
void.
value. ( 7.17) ++ -- ( 7.6.9, 7.7.5),
( ,
, ++ --), ( 7.17.1). , void, 10.6.10.
, return, , .
value,
.
:
, , ,
.

534

10.

, ,
. ,
.
, ,
. ,
,
.
:
public class Button: Control
{
private string caption;
public string Caption {
get {
return caption;
}
set {
if (caption != value) {
caption = value;
Repaint();
}
}
}
public override void Paint(Graphics g, Rectangle r) {
//
}
}

Button
Caption. , caption. ,
, , . : , ,
,
.
Caption
Button:
Button okButton = new Button();
okButton.Caption = "OK";
string s = okButton.Caption;

//
//

,
.

. ,
,
. :

10.7.

535

class A
{
private string name;
public string Name {
get { return name; }
}

// :

public string Name {


set { name = value; }
}

// :

, . , , ,
. , ,
, .
, , ,
.
:
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
((A)b).P = 1;

// : B.P
// : A.P

b.P , P B
P A. , P
.
, public, . :
class Label
{
private int x, y;
private string caption;

536

10.
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); }
}
public string Caption {
get { return caption; }
}
}

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; }
}
public Point Location {
get { return location; }
}
public string Caption {
get { return caption; }
}
}

10.7.

537

x y , public readonly,
Label .

, . ,
,
.

, ,
.

,
.
:
class Counter
{
private int next;
public int Next {
get { return next++; }
}
}

Next .
, ,
.

, , .
,
. , .
, .
:
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;
}
}

538

10.

public static TextWriter Out {


get {
if (writer == null) {
writer = new StreamWriter(Console.OpenStandardOutput());
}
return writer;
}
}
public static TextWriter Error {
get {
if (error == null) {
error = new StreamWriter(Console.OpenStandardError());
}
return error;
}
}
}

Console : In, Out Error


,
. ,
Console
. , Out
:
Console.Out.WriteLine(", ");

TextWriter . In Error,
.

10.7.3.

C# 3.0 . ,
, , .

,
.
:
public class Point {
public int X { get; set; }
public int Y { get; set; }
}

//
//

10.7.

539

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; } }
}

,

. , ,
, .
. ,

:
public class ReadOnlyPoint {
public int X { get; private set; }
public int Y { get; private set; }
public ReadOnlyPoint(int x, int y) { X = x; Y = y; }
}


,
. ReadOnlyPoint
.


,
, (
), .
, , :
public int X { get; readonly set; }

; ,
. , , .


, ( 7.6.10.2).
, ,
.
, ,
.
, , ,
.

540

10.

,
,
, . , ,
, .

10.7.4.
--,
( 3.5.2)
--. , .
--
( 7.3) ( 7.5.3).
.
, :
( 7.1.1),
.
( 7.17.1), .
( 7.17.2) ++ -- ( 7.5.9, 7.6.5), .
A.Text B.Text
, . B.Count, , M,
A.Count.
class A
{
public string Text {
get { return ""; }
set { }
}
public int Count {
get { return 5; }
set { }
}
}
class B: A
{
private string text = "";
private int count = 0;

10.7.

541

new public string Text {


get { return text; }
protected set { text = value; }
}
new protected int Count {
get { return count; }
set { count = value; }
}
}
class M
{
static void Main() {
B b = new B();
b.Count = 12;
int i = b.Count;
b.Text = "!";
string s = b.Text;

//
//
//
//
//

A.Count
A.Count
: B.Text

B.Text

}
}

, , --.
, -:
public interface I
{
string Prop { get; }
}
public class C: I
{
public Prop {
get { return ""; }
internal set {...}

//
// I.Prop
//

}
}

10.7.5. , ,

virtual ,
. virtual , .
abstract ,
, . -

542

10.

, .
,
-- .
, abstract
override, ,
. .

( 10.1.1.1). ,
override. .
.
.
, , .
( ),
. ( ),
, .
sealed.

.
.
,
,
, , , .
, , 10.6.310.6.6, ,
:
, ,
.
- , void ,
.
:
abstract class A
{
int y;
public virtual int X {
get { return 0; }
}
public virtual int Y {
get { return y; }
set { y = value; }
}

10.7.

543

public abstract int Z { get; set; }


}

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; }
}
public override int Z {
get { return z; }
set { z = value; }
}
}

X, Y Z
. ,
. X Y base . Z
, B ,
.
override, . ,
.
:
public class D: B
{
public override int P {
protected set {...}
get {...}
}
}
public class B
{
public virtual int P {
protected set {...}
get {...}
}
}

// protected
//

544

10.

10.8.
, . ,
.
-:
-:
opt
-opt
event
-
;
opt
-opt
event
{
---
}
-:
-
-

-:
new
public
protected
internal
private
static
virtual
sealed
override
abstract
extern
---:
---add
---remove
---remove
---add
---add:
opt
add

---remove:
opt
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);
.

10.8.

545

, --,
-. , -.
- abstract
---,
.
extern, . , extern
--- .
+= -=
( 7.17.3). ,
, .
, , += -=,
, -
.
, x += y x -= y, x ,
, x,
void ( ,
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;
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
}

546

10.

void CancelButtonClick(object sender, EventArgs e) {


// CancelButton.Click
}
}

LoginDialog Button
Click.

. , ,
, , ,
. , (1) ,
, (2)
add remove, WeakReference ,
(3) (Weak Event),
WPF IWeakEventListener.


( ) -, : (add remove),
(+= -=) .
, , ,
add remove;
, , ,
. ,
,
. , ,
.

10.8.1. ,
, , .
--. ,
. ( 15),
, .
, null.

C# ,
, , ,
, :

10.8.

547

public event EventHandler Click { add; remove; }

,
.

:
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.
,
,
. , ,
.
Button Click
+= -=,
b.Click += new EventHandler(...);

Click,
b.Click -= new EventHandler(...);

Click.

, ,
.
( )
( 8.12)
( 7.6.10.6) .
,
class X
{
public event D Ev;
}

-, :
class X
{
private D __Ev;

//

548

10.

public event D Ev {
add {
/* */
}
remove {
/* */
}
}
}

X Ev += -=
add remove. Ev , __Ev. __Ev
;
.

10.8.2.
---,
Button. , , ,
. --
.
---
, .
---add ---remove. add
remove, . , --add, , ,
, ---remove, , .
---add ---remove
-, ,
void.
value. ,
: +=
add, -= remove.
. - - -add ---re mo ve
, void,
10.6.10. , return
.
value,
.

10.8.

549

:
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);
}
}

Control
. AddEventHandler , GetEventHandler , ,
RemoveEventHandler ,
. ,
,
null , ,
.

10.8.3.

static,
. static , .

550

10.

, this .
, this ( 7.6.7).
-- ( 7.6.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.9.

551

, , , .
, , 10.6.310.6.6, ,
.
- , void , .

10.9.
,
, . :
-:
opt
-opt
{
--
}
-:
-
-

-:
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 , .
,
. ,

552

10.

this. , . this.
.
-- .

( 10.6.1), ,
ref out.
, -, ,
( 3.5.4).
-- ( 10.7.2),
{ }, . , .
,
. ,
ref out.
( 3.6)
. ,
.
.
, .
,
:
, .
- ( 7.6.2) - ( 7.6.4), -- ( 7.6.6.2).
,
.
, ,
.

value,
, ,
value.

, , .

, base.P, P . -

10.9.

553


base[E], E ,
.
, , 10.7.2 10.7.3,
, .

, ( ), . , Encoding.GetEncoding(string)
. ,
-
, .
, .

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;
}
public int Length {
get { return length; }
}
public bool this[int index] {
get {
if (index < 0 || index >= length) {
throw new IndexOutOfRangeException();
}
return (bits[index >> 5] & 1 << index) != 0;
}
set {
if (index < 0 || index >= length) {
throw new IndexOutOfRangeException();
}

554

10.
if (value) {
bits[index >> 5] |= 1 << index;
}
else {
bits[index >> 5] &= ~(1 << index);
}
}

}
}

BitArray ,
bool[] (
, ),
, bool[].
CountPrimes BitArray
1
:
class CountPrimes
{
static int Count(int max) {
BitArray ags = new BitArray(max + 1);
int count = 1;
for (int i = 2; i <= max; i++) {
if (!ags[i]) {
for (int j = i * 2; j <= max; j += i) ags[j] = true;
count++;
}
}
return count;
}
static void Main(string[] args) {
int max = int.Parse(args[0]);
int count = Count(max);
Console.WriteLine(" {0} 1 {1}",
count, max);
}
}

, BitArray bool[].
, 26x10 .
A-Z, 0-9.
using System;
class Grid
{
const int NumRows = 26;
const int NumCols = 10;
int[,] cells = new int[NumRows, NumCols];

10.10.

555

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.5.2.

10.10.
, , .
-:
-:
-
opt
-:
-
-

-:
public
static
extern
-:
--
--
--

556

10.

--:

operator
--
(

)
--:
+
!
~
++
-true
false
--:

operator
--
(

,

--:
+

*
/
%
&
|
^
<<
-
==
!=
>
<
>=
<=
--:
implicit
operator

explicit
operator

)
)

-:

: ( 10.10.1), ( 10.10.2)
( 10.10.3).
extern, . , - .
- , ,
. , , 10.6.10.
:
public
static.
-.
ref out, .
( 10.10.110.10.3)
, .

10.10.

557

, ,
, ( 3.5.4).

, .
,
.
, , ,
. , , , ,
. , new
.

7.3.

6.4.

10.10.1.
, T , :
+, -, ! ~ T T?
.
++ -- T T?
.
true false T
T? bool.
(+, -, !, ~, ++, --,
true false) .
,
.
true false .
, , .
true false 7.12.2 7.20.

++ , :
public class IntVector
{
public IntVector(int length) {...}
public int Length {...}

//

public int this[int index] {...}

// -

558

10.
public static IntVector operator ++(IntVector iv) {
IntVector temp = new IntVector(iv.Length);
for (int i = 0; i < iv.Length; i++)
temp[i] = iv[i] + 1;
return temp;

}
}
class Test
{
static void Main() {
IntVector iv1 = new IntVector(4);
IntVector iv2;
iv2 = iv1++;
iv2 = ++iv1;

// 4-

//
//
//
//

iv2
iv1
iv2
iv1

4
4
4
4

}
}

, , , ,
1 , ,
( 7.6.9) ( 7.7.5). C++
.
.

10.10.2.
, T , :
, , ,
T T?, .
<< >> ,
T T?, int int?, .
(+, -, *, /, %, &, |,
^, <<, >>, ==, !=, >, <, >= <=) .
.
. . ,
.
:

10.10.

559

operator == operator !=
operator > operator <
operator >= operator <=

Clothing (), Socks (), Shoes (), Shirt (),
Tie () Hat (), , Socks <
Shoes, Shirt < Tie, .
, Socks Shoes, Shirts Ties, . : Hat
Socks, Hat Shoes, Socks , Shoes, Hat
, Hat, .
, ;
,
, .
, - , .

10.10.3.

( 6.4), .
, implicit,
. , , .
6.1.


, ,
, .
.


6.12,
oat double .

.

560

10.

, explicit,
.
6.2.

, , .

, , , .
S T, S T , S0 T0 ;
S0 T0 S T. S T,
:
S0 T0 .
S0, T0 ,
.
S0, T0 .
S T T S, .
-,
S T, , , - .
:
class C<T> {...}
class D<T>: C<T>
{
public static implicit operator C<int>(D<T> value) {...}
public static implicit operator C<string>(D<T> value) {...}
public static implicit operator C<T>(D<T> value) {...}

//
//
//

, ,
10.10.3, T int string;
. , C<T> D<T>.
, , .

10.10.

561

, C C
int int C, int bool.
. ,
object,
. ,
, ,
.
,
- , . :
struct Convertible<T>
{
public static implicit operator Convertible<T>(T value) {...}
public static explicit operator T(Convertible<T> value) {...}
}

, - T object,
( , , , object).
, ,
. :
( 6.1) S
T, ( )
S T .
( 6.2) S
T, S T . :
 T ,
S T .

S T .
o b j e c t , ,
Convertible<T>, . :
void F(int i, Convertible<int> n) {
i = n;
i = (int)n;
n = i;
n = (Convertible<int>)i;

//
//
//
//
//
//
//

object
, :

562

10.

void F(object o, Convertible<object> n) {


o = n;
//
//
o = (object)n;
//
//
n = o;
//
//
n = (Convertible<object>)o;
//
//
}


. , ,
,
.

(, , ).
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);
}
}

Digit byte ,
, byte Digit , Digit
byte.

10.11.

563

10.11.
, ,
.
-:
-:
-opt
opt
-
-:
-
-

-:
public
protected
internal
private
extern
-:

(
--opt
-opt
-:
:
base
(
-opt
:
this
(
-opt

)
)

-:

- ( 17)
( 10.3.5) extern.
.
-
, .
, .
--
, --
( 10.6). ( 3.6)

( 7.5.2).
, -- , ,
( 3.5.4).
-
, ,
-. 10.11.1.

564

10.

extern, . , - .
- , , .
, void ( 10.6.10).
. ,
, .
, ( 10.11.4).
- ( 7.6.10.1), -.

10.11.1.
( object)
-.
-:
base(-opt)
-
. -
, 7.5.3.
, ,
( 10.11.4).

, .
this(-opt)
. -
, 7.5.3.
,
.
, . , , .

- dynamic,
ref out;
(CS1975). , .

10.11.

565

,
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 ,

-.

10.11.2.


base(...), , - , .
,
. ,
.

10.11.3.
,
.
,
,
. :

566

10.

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);
}
}

B new B(), :
x = 1, y = 0

x 1 ,
. y 0
( int),
.
,
-. :
using System;
using System.Collections;
class A
{
int x = 1, y = -1, count;
public A() {
count = 0;
}
public A(int n) {
count = n;
}
}
class B: A
{
double sqrt2 = Math.Sqrt(2.0);
ArrayList items = new ArrayList(100);
int max;

10.11.

567

public B(): this(100) {


items.Add(" ");
}
public B(int n): base(n 1) {
max = n;
}
}

;
(base this).
,
(,
,
).
using System.Collections;
class A
{
int x, y, count;
public A(int n) {
x = 1;
y = -1;
object();
count = n;
}
public A() {
x = 1;
y = -1;
object();
count = 0;
}

//
//
// object()

//
//
// object()

}
class B: A
{
double sqrt2;
ArrayList items;
int max;
public B(): this(100) {
B(100);
items.Add("default");
}
public B(int n): base(n 1) {
sqrt2 = Math.Sqrt(2.0);
items = new ArrayList(100);
A(n 1);
max = n;
}
}

// B(int)

//
//
// A(int)

568

10.

10.11.4.
,
. .
,
. ,
protected. public. ,
protected C(): base() {}

public C(): base() {}

C .
:
class Message
{
object sender;
string text;
}

, . ,
:
class Message
{
object sender;
string text;
public Message(): base() {}
}

10.11.5.
T ,
private, , T,
. ,
, ,
. :
public class Trig
{
private Trig() {}

//

public const double PI = 3.14159265358979323846;


public static double Sin(double x) {...}
public static double Cos(double x) {...}
public static double Tan(double x) {...}
}

10.11.

569

Trig , .
. ,
.

, .
(Singleton),
, .


,
static.
,
,
.
. ,
.

10.11.6.

this(...)
. :
class Text
{
public Text(): this(0, 0, null) {}
public Text(int x, int y): this(x, y, null) {}
public Text(int x, int y, string s) {
//
}
}


. this(...) ,
.
:

570

10.

Text t1 = new Text();


Text t2 = new Text(5, 10);
Text t3 = new Text(5, 20, "");

// , Text(0, 0, null)
// , Text(5, 10, null)

10.12.
, ,
.
--:
--:
opt
--
(
)
--

--:
static
externopt
static
externopt
--:

--
( 17) extern ( 10.6.7).
--
, . ,
.
extern,
.

, --
. - , ,
. , void ( 10.6.10).
.

. :
.
.

,
, , .

10.12.

571

, , , , ,
, .

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(" A");
}
public static void F()
{
Console.WriteLine("A.F");
}
}
class B
{
static B()
{
Console.WriteLine(" B");
}
public static void F()
{
Console.WriteLine("B.F");
}
}

:
A
A.F
B
B.F

572

10.

, A.F
A, B.F
B.
, ,
.
:
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() { }
static void Main()
{
Console.WriteLine("X = {0}, Y = {1}", A.X, B.Y);
}
}

:
X = 1, Y = 2

Main
B.Y, B. Y A,
A.X. , A
X Y, . A.X .

A , Y,
2.

,
-, ( 10.1.5) .
, , , - :
class Gen<T> where T : struct
{

10.13.

573

static Gen()
{
if (!typeof(T).IsEnum)
{
throw new ArgumentException("T ");
}
}
}

10.13.
, , . :
-:
opt
externopt

-:

- ( 10.17).
- , . , .
extern, .
, -
. - ,
, .
- - ,
void ( 10.6.10).
. , , , .
, ,
, .
. , .
, .
,
,
. .
, , ,
3.9.

574

10.


, , , , ,
. - , ,
. ,
, ,
,
.

:
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();
}
}

:
B
A

,
.

,
,
. C#;
, Dispose
IDisposable .

10.14.

575


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.5), , .
, - ( 10.14.1)
( 10.14.2).
-, - --, ,
,
.
,
ref out
.

10.14.1. -
- System.
Collections.IEnumerator System.

576

10.

Collections.Generic.IEnumerator<T>. IEnumerator IEnumerator<T>.



,
, IIterable<T>
IIterator<T>. , I ,
. .

10.14.2.
System.
Collections.IEnumerable System.
Collections.Generic.IEnumerable<T>. IEnumerable IEnumerable<T>.

10.14.3.
.
.
, I E n u m e r a t o r
IEnumerable, object.
, IEnumerator<T>
IEnumerable<T>, T.

10.14.4. -
, -,
, .
-. , ,
MoveNext
-. - :
IEnumerator IEnumerator<T>, T .
System.IDisposable.
( ) , .
: , ,
.

10.14.

577


, MoveNext(),
. , - , : ,
,
.
,
.

- , , -,
. - ,
,
, private ,
( 2.4.2).
-
.
MoveNext, Current
Dispose IEnumerable IEnumerable<T>, -.
, - IEnumerator.
Reset. System.
NotSupportedException.

10.14.4.1. MoveNext
MoveNext - .
Current . , MoveNext,
- :
- , MoveNext
:
 .
 ( this)
,
-.
 , ( ).
- ,
MoveNext .
- ,
MoveNext :

578

10.

 .
 (
this) ,
. , ,
, MoveNext.
 ,
yield return, , , (
).
- , MoveNext false.
MoveNext ,
: yield return, yield break, ,
.
yield return ( 8.14):
 , , , Current .
 .
( this),
yield return. yield return
try,
nally .
 - .
 MoveNext true, ,
.
yield break ( 8.14):
 try, nally.
 - .
 MoveNext false, , .
:
 - .
 MoveNext false, , .
, :
 nally .

10.14.

579

 - .
 , MoveNext.

10.14.4.2. Current
Current - yield
return .
- ,
Current , MoveNext.
- , , Current .
, object,
Current IEnumerable -
Current IEnumerator<T> object.

10.14.4.3. Dispose
Dispose
- .
- , Dispose
.
- ,
Dispose .
- ,
Dispose :
 .
 nally,
yield return yield break.
,
- ,
Dispose.
 .
- , Dispose
.

10.14.5.
, ,
, . . GetEnumerator
-, , , -

580

10.

MoveNext . :
IEnumerable IEnumerable<T>, T .
( ) , .

, , ,
. ,
,
, private ,
( 2.4.2).
, . , IEnumerator
IEnumerator<T>, , .
, GetEnumerator
. GetEnumerator,
, . , ,
.

10.14.5.1. GetEnumerator
GetEnumerator
IEnumerable IEnumerable<T>. GetEnumerator , . , ,
, , - ,
10.14.4.

10.14.6.

C#. , Microsoft C#,
.

, , ,
, -.

10.14.

581

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;
}
public IEnumerator<T> GetEnumerator()
{
for (int i = count - 1; i >= 0; --i) yield return items[i];
}
}

GetEnumerated
-, ,
:
class Stack<T>: IEnumerable<T>
{
...
public IEnumerator<T> GetEnumerator() {
return new __Enumerator1(this);
}

582

10.

class __Enumerator1: IEnumerator<T>, IEnumerator


{
int __state;
T __current;
Stack<T> __this;
int i;
public __Enumerator1(Stack<T> __this) {
this.__this = __this;
}
public T Current {
get { return __current; }
}
object IEnumerator.Current {
get { return __current; }
}
public bool MoveNext() {
switch (__state) {
case 1: goto __state1;
case 2: goto __state2;
}
i = __this.count - 1;
__loop:
if (i < 0) goto __state2;
__current = __this.items[i];
__state = 1;
return true;
__state1:
--i;
goto __loop;
__state2:
__state = 2;
return false;
}
public void Dispose() {
__state = 2;
}
void IEnumerator.Reset() {
throw new NotSupportedException();
}
}
}


MoveNext -.
, i -,
MoveNext.
1 10.
FromTo .

10.14.

583

using System;
using System.Collections.Generic;
class Test
{
static IEnumerable<int> FromTo(int from, int to)
{
while (from <= to) yield return from++;
}
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;
}

584

10.
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();
}
public int Current {
get { return __current; }
}
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.

10.14.

585

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;
public Tree(T value, Tree<T> left, Tree<T> right)
{
this.value = value;
this.left = left;
this.right = right;
}
public IEnumerator<T> GetEnumerator() {
if (left != null) foreach (T x in left) yield x;
yield value;
if (right != null) foreach (T x in right) yield x;
}
}
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
//

586

10.

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();
Tree<string> strings = MakeTree(
"", "", "", "", "", "", "");
foreach (string s in strings) Console.Write("{0} ", s);
Console.WriteLine();
}
}

GetEnumerator
-, ,
:
class Tree<T>: IEnumerable<T>
{
...
public IEnumerator<T> GetEnumerator() {
return new __Enumerator1(this);
}
class __Enumerator1 : IEnumerator<T>, IEnumerator
{
Node<T> __this;
IEnumerator<T> __left, __right;
int __state;
T __current;
public __Enumerator1(Node<T> __this) {
this.__this = __this;
}
public T Current {
get { return __current; }
}
object IEnumerator.Current {
get { return __current; }
}
public bool MoveNext() {
try {
switch (__state) {
case 0:
__state = -1;
if (__this.left == null)
goto __yield_value;
__left = __this.left.GetEnumerator();
goto case 1;
case 1:
__state = -2;
if (!__left.MoveNext())
goto __left_dispose;

10.14.

587

__current = __left.Current;
__state = 1;
return true;
__left_dispose:
__state = -1;
__left.Dispose();
__yield_value:
__current = __this.value;
__state = 2;
return true;
case 2:
__state = -1;
if (__this.right == null) goto __end;
__right = __this.right.GetEnumerator();
goto case 3;
case 3:
__state = -3;
if (!__right.MoveNext()) goto __right_dispose;
__current = __right.Current;
__state = 3;
return true;
__right_dispose:
__state = -1;
__right.Dispose();
__end:
__state = 4;
break;
}
}
nally {
if (__state < 0) Dispose();
}
return false;
}
public void Dispose() {
try {
switch (__state) {
case 1:
case -2:
__left.Dispose();
break;
case 3:
case -3:
__right.Dispose();
break;
}
}

588

10.
nally {
__state = 4;
}
}
void IEnumerator.Reset() {
throw new NotSupportedException();
}

}
}

, foreach,
__left __right -. __state
,
Dispose(). ,
foreach.

11

,
- -.
- . struct ,
, .


. -,
: DateTime Customer
, . -,
- , .NET.
, ,
.

,
. ,
.
, -,
,
, .
4.1.4, C#, int, double bool,
. C#
.
( 11.4).

-,
. , ,
. , API Java, -, long. Java


590

11.

long,
C# .


.
.

11.1.
- - ( 9.6),
:
-:
opt opt partialopt struct -opt
-opt ---opt - ;opt

- ( 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

, .
, ( 10.1).

11.2.

591

11.1.2. partial
partial , - . ,
, , 10.2.

11.1.3.
, .
-:
:
-

13.4.

11.1.4.
- .
-:
{
--opt

11.2.
, -, , System.ValueType.
--:
--
--

--

--:
-
-
-
-
-
-
-
-
--
-

, 11.3, , 10.310.14, .

592

11.

11.3.

:
- ( 11.3.1).
System.ValueType ( 11.3.2).

( 11.3.3).
, , -,
, null ( 11.3.4).
struct object ( 11.3.5).
this ( 7.6.7).

( 11.3.7).
( 11.3.8).
( 11.3.9).

, ,
C++.

11.3.1.
- ( 4.1), ,
. , ,
( 4.2) .

, , , ,
, .
. , ,
. :
struct Message
{
int code;
string message;

11.3.

593

// message
// string,
}

message, .

struct ,
, . B A, A ,
, A B. X
Y, X Y. ,
,
. ,
struct Node
{
int data;
Node next; // : Node
}

, 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;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
}


Point a = new Point(10, 10);
Point b = a;
a.x = 100;
System.Console.WriteLine(b.x);

594

11.

10. b a
, 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.
struct
. , ,
.
, .
, ref out.

ref out . : ref , : ref,
, .

, , ,
.
, . 7.17.1.

11.3.

595

11.3.4.
5.2, .
null.
- null, ,
, -, ,
null.
Point, ,
Point[] a = new Point[100];

Point , x y.
, ( 4.1.2).
.
,
, ,
-, , null.
,
.
using System;
struct KeyValuePair
{
string key;
string value;
public KeyValuePair(string key, string value) {
if (key == null || value == null)
throw new ArgumentException();
this.key = key;
this.value = value;
}
}


key value null , .
KeyValuePair , null, .

11.3.5.

C# 2.0,
, .

596

11.

object ,
, .

object (,
).
, -.
object , ,
. , object
. ,
.
,
.

, - :
, ,
, .
, .

,
System.Object (, Equals, GetHashCode ToString),
. ,

.
:
using System;
struct Counter
{
int value;
public override string ToString() {
value++;
return value.ToString();
}
}
class Program
{
static void Test<T>() where T: new() {
T x = new T();
Console.WriteLine(x.ToString());
Console.WriteLine(x.ToString());

11.3.

597

Console.WriteLine(x.ToString());
}
static void Main() {
Test<Counter>();
}
}


1
2
3

ToString ,
, x.ToString()
.
- . ICounter, Increment, .
ICounter -,
Increment ,
, .
using System;
interface ICounter
{
void Increment();
}
struct Counter: ICounter
{
int value;
public override string ToString() {
return value.ToString();
}
void ICounter.Increment() {
value++;
}
}
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);
}

598

11.

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.

- this
this.

11.3.7.
11.3.4, , ,
-, , null.

. . .
struct Point
{
public int x = 1; // :
public int y = 1; // :
}

, .

11.3.

599


, 1
x:
struct Point
{
bool initialized; // false
int x;
public int X {
get {
if (!initialized) { x = 1; initialized = true; }
return x;
}
}
}



:
struct Point
{
private int? x; // null
public int X { get { return x ?? 1; }}
}

int , int
bool, , , , .

11.3.8.

.
, ,
, -, ,
null ( 4.1.2). . :
struct Point
{
int x, y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
}


Point p1 = new Point();
Point p2 = new Point(0, 0);

Point, x y .

600

11.

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 ,
, .

, ,
:
public Point(int x, int y) : this() { //
//

11.3.9.
.

11.3.10.

. -

11.4.

601

,
:
.
.
( 11.3.4) (
).

11.4.
struct
, , ,
.


. ,
struct
.

11.4.1.
DBInt ,
int, ,
. .
using System;
public struct DBInt
{
// Null DBInt.
public static readonly DBInt Null = new DBInt();
//
//
//
//

dened true, DBInt


, eld.
DBInt
, eld 0.

int value;
bool dened;
// .
// DBInt .

602

11.

DBInt(int value) {
this.value = value;
this.dened = true;
}
// IsNull true,
// DBInt .
public bool IsNull { get { return !dened; } }
// Value DBInt,
// 0, DBInt .
public int Value { get { return value; } }
// int DBInt.
public static implicit operator DBInt(int x) {
return new DBInt(x);
}
// DBInt int. ,
// DBInt .
public static explicit operator int(DBInt x) {
if (!x.dened) throw new InvalidOperationException();
return x.value;
}
public static DBInt operator +(DBInt x) {
return x;
}
public static DBInt operator -( DBInt x) {
return x.dened ? -x.value : Null;
}
public static DBInt operator +( DBInt x, DBInt y) {
return x.dened && y.dened? x.value + y.value: Null;
}
public static DBInt operator -( DBInt x, DBInt y) {
return x.dened && y.dened? x.value - y.value: Null;
}
public static DBInt operator *( DBInt x, DBInt y) {
return x.dened && y.dened? x.value * y.value: Null;
}
public static DBInt operator /(DBInt x, DBInt y) {
return x.dened && y.dened? x.value / y.value: Null;
}
public static DBInt operator %(DBInt x, DBInt y) {
return x.dened && y.dened? x.value % y.value: Null;
}

11.4.

603

public static DBBool operator ==(DBInt x, DBInt y) {


return x.dened &&
y.dened? x.value == y.value: DBBool.Null;
}
public static DBBool operator !=(DBInt x, DBInt y) {
return x.dened &&
y.dened? x.value != y.value: DBBool.Null;
}
public static DBBool operator >(DBInt x, DBInt y) {
return x.dened &&
y.dened? x.value > y.value: DBBool.Null;
}
public static DBBool operator <(DBInt x, DBInt y) {
return x.dened &&
y.dened? x.value < y.value: DBBool.Null;
}
public static DBBool operator >=(DBInt x, DBInt y) {
return x.dened &&
y.dened? x.value >= y.value: DBBool.Null;
}
public static DBBool operator <=(DBInt x, DBInt y) {
return x.dened &&
y.dened? x.value <= y.value: DBBool.Null;
}
public override bool Equals(object obj) {
if (!(obj is DBInt)) return false;
DBInt x = (DBInt)obj;
return value == x.value && dened == x.dened;
}
public override int GetHashCode() {
return value;
}
public override string ToString() {
return dened? value.ToString():"DBInt.Null";
}
}


System.Data.SqlTypes .NET , SqlBoolean SqlInt32, - DBInt DBBool. , .NET 1.0,
, null,
, . .NET 2.0 ,
int?
bool?.

604

11.

11.4.2. boolean
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;
// .
// 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;
}
// bool DBBool.
// , DBBool
// Null; true false.
public static explicit operator bool(DBBool x) {
if (x.value == 0) throw new InvalidOperationException();
return x.value > 0;
}
// . Null,
// Null;
// True False.

11.4.

605

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.

606

11.

public static bool operator false(DBBool x) {


return x.value < 0;
}
public override bool Equals(object obj) {
if (!(obj is DBBool)) return false;
return value == ((DBBool)obj).value;
}
public override int GetHashCode() {
return value;
}
public override string ToString() {
if (value > 0) return "DBBool.True";
if (value < 0) return "DBBool.False";
return "DBBool.Null";
}
}


.NET , -
Java.
, ,
.


, -
, ,
, .
, , .
:
, . -
. ,
. -
, .
- , , .
.
- ( ) ,
, ,
.

12

,
, . , ,
, .
, , . .
1 . 1,
. , . .
,
, . ,
.
: N 0
N-1 .
.
, .
, .

12.1.
, ,
-:
:
-

-:

-:
-
-

-:
[
-opt
-:
,
-

608

12.

- , .
-
-: - ,
1 , -.

-:
T[R] R
, .
T[R][R1]...[Rn] R
[R1]...[Rn].
, -
, . int[][,,][,] , , , ,
int.

, , .
, , ; arr[a][b,c,d]
[e,f].

- 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).

12.1.

609

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;
IList<string> lst1 = sa;
IList<string> lst2 = oa1;
// :
IList<object> lst3 = sa;
IList<object> lst4 = oa1;
IList<string> lst5 = (IList<string>)oa1;
IList<string> lst6 = (IList<string>)oa2;
}
}

//

//
//
//
//

lst2 = oa1 , object[] IList<string> , . (IList<string>)oa1 ,


oa1 object[], string[].
(IList<string>)oa2 ,
oa2 string[].
S[]
IList<T>, IList<T>
S[] ( 6.2.4).
S[] IList<T>,
. .

, CLR 1 . , , , ,
Array<T>, Array2<T> . .
: Array<Array2<int>> ,
.


, T[] IList<T>,
, ,
. IList<T> .
, ,
. ,
IList<T>.

610

12.

12.2.
-- ( 7.6.10.4) ,
- ( 12.6).

,
. ,
, .
, . System.Array
, .
, --,
( 5.2).

12.3.

--- ( 7.6.6.1) A[I1, I2, ..., In],
Ix int, uint, long
ulong .
, ,
.
foreach ( 8.8.4).

12.4.
, System.Array.

12.5.
- ( 6.1.6)
( 6.2.4) , A[R] B[R], R
- ( ). . , ,
A[R]
B[R], .
-
, ,
, ( 7.17.1). :

12.5.

611

class Test
{
static void Fill(object[] array, int index, int count, object value) {
for (int i = index; i < index + count; i++)
array[i] = value;
}
static void Main() {
string[] strings = new string[100];
Fill(strings, 0, 100, "Undened");
Fill(strings, 0, 10, null);
Fill(strings, 90, 10, 0);
}
}

array[i] Fill
, , value,
null, , array.
Main Fill ,
System.ArrayTypeMismatchException
array[i]. , int
string.

C#. ,
- ,
. ,
; , ,
, .
, IEnumerable<T>.


, , , .
, List<T>, Dictionary<K, V> IEnumerable<T>,
.


, , (Student[]
Person[], Student Person). C#
Java, : - , Java , , - , , new
T[...] - new Stack<Person>[...],
. , Java ,


612

12.

- , . C# -
, .
Scala, , , (
, Scala - Java).


, , C# 4.0.
, .

-. ,
, int[]
object[].

12.6.
( 10.5),
( 8.5.1)
( 7.6.10.4):
-:
{
--opt
,
}
{
--
,
}
--:

--

-:

- , { } ,.
, , .
, , .

. .
int[] a = {0, 2, 4, 6, 8};

12.6.

613

:
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}};

5 2
:
int[,] b = new int[5, 2];

:
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;

, , 0, , 0.
int[,] c = {};

,
:
int[,] c = new int[0, 0];

,
,

. :
int i
int[]
int[]
int[]

=
x
y
z

3;
= new int[3] {0, 1, 2};
= new int[i] {0, 1, 2};
= new int[3] {0, 1, 2, 3};

//
// : i
// : /

y ,
. z
- ,
.

13

. , , . ,
.

, , . ,
, . .


, , . ,
. , , ,
. : , ,
, ,
Drive() , StartEngine(),
null, . .
, -, .
, CLR 4.0,
, ,
.

, , .
, .
,
.

13.1.
- - ( 9.6),
.

13.1.

615

-:
opt
-opt
partialopt
interface

---opt
-opt
---opt
-
;opt

- ( 17), -
( 13.1.1), partial, interface
, , --- ( 13.1.3),
- ( 13.1.4), -- ( 10.1.5), - ( 13.1.5) .

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.

616

13.

13.1.3.

. --
- .
---:
<
--
>
--:
opt
-opt
-
--
,
opt
-opt
-
-:
in
out

out,
. in,
. , .


. ,
object. T IEnumerable<T> ; IEnumerable<string>
IEnumerable<object>. ,
; .
c T IEnumerable<T>
T, ,
T ,
.

:
interface C<out X, in Y, Z>
{
X M(Y y);
Z P { get; set; }
}

X , Y , Z .

13.1.3.1.
, .

13.1.

617

T (output-unsafe),
:
T .
T , .
T S<A1,, AK>, S<X1,, XK>, Ai
:
 Xi , Ai
.
 X i , A i
(input-safe).
T (input-unsafe), :
T .
T , .
T S<A1,, AK>, S<X1,, XK>, Ai
:
 Xi , Ai
.
 Xi , Ai
.
,
,
.
(output-safe), .
(input-safe), .

13.1.3.2.
, ( )
.
( 6.1) ( 6.2) , :
T<A1,, An> T<B1,, Bn>, T
,
T<X1,, Xn>, Xi
:
Xi , Ai Bi.

618

13.

Xi , Bi Ai.
Xi , Ai Bi.

, . ,
, , , ,
, , C# 3.0 .

13.1.4.

, .
,
, .
-:
:
--

: -
-
.

, ( 3.5.4). ,
public -
private internal .
,
.

( ). ,
I<T> I<I<T>> , :
interface I<T> : I<I<T>> { }

,
:
interface IA<T> { }
interface IB : IA<IB[]> { } //

,
. ,

13.1.

619


, . .
. :
interface IControl
{
void Paint();
}
interface ITextBox : IControl
{
void SetText(string text);
}
interface IListBox : IControl
{
void SetItems(string[] items);
}
interface IComboBox : ITextBox, IListBox { }

IComboBox IControl, ITextBox


IListBox.
, IComboBox SetText SetItems,
Paint.
-
( 13.1.3.1). , , .

. ,
, , .
, ,
, , .


I (IControl,
IWriteable, IClaudius). ,

. I .

13.1.5.
- .
-:
{
--opt

620

13.

13.2.
,
, , .
--:
--
--

--

--:
--
--
--
--

.
, , .
, , , , , .
.

. ,
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;
string this[int index] { get; set; }
}

,
: , , .
- ( 3.3),
--, , . - :

13.2.

621

,
. , ( 3.6) , ,
, , , ref out.
, .
, .

. ,
, . ,
,
. ,
. , new, ,
. 3.7.1.2.
new , , . ,
new.
, object, ,
- ( 13.2).
( 7.4).

13.2.1.
--:
--:
newopt
--

opt
)
--
(
--opt
;
---opt

, --, - , ( 10.6).
, .

, -- , .
-- --.

( 13.1.3.1),

622

13.

void. ,
,
.
, . ,
interface <I<out T> { void M<U>() where U : T; }

, T
U .
,
:
class B {}
class D : B {}
class E : B {}
class C : I<D> { public void M<U>() {...} }
...
I<B> b = new C();
b.M<E>();

C.M<E>. , E
D, , .

13.2.2.
--:
--:
opt
newopt

--:
opt
get
;
set
;
opt
opt
get
;
opt
set
;

;
;

opt
opt

set
get

--

,
, ( 10.7).

( 10.7.2), ,
. ,
, ,
.
,
get, , set.

13.2.3.
--:
--:
opt
newopt
event

13.2.

623

,
, ( 10.8).
.

13.2.4.
--:
--:
newopt

this
opt
{
--

[
}

--

, --
, ( 10.9).

( 10.9), ,
. ,
, ,
.
. , out ref . ,
- out .
,
get, , set.

13.2.5.

( 7.6.4) ( 7.6.6.2) I.M
I[A] , I , ,
, A .
, (
, ),
( 7.4),
( 7.6.5.1) ( 7.6.6.2), : , , ,
.
,
.
.
.

624

13.

:
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.4) Count IListCounter .


, 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);
n.Add(1.0);
((IInteger)n).Add(1);

//
//
//
//

IInteger.Add
IDouble.Add

IInteger.Add

13.3.
((IDouble)n).Add(1);

625

//
// IDouble.Add

}
}

n.Add(1) , 7.5.3, IInteger.Add. ,


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);
((IBase)d).F(1);
((ILeft)d).F(1);
((IRight)d).F(1);
}
}

//
//
//
//

ILeft.F
IBase.F
ILeft.F
IBase.F

IBase.F ILeft.F. ,
d.F(1) ILeft.F,
IRight IBase.F .

:
, . IDerived
ILeft IBase IBase.F,
IDerived IRight IBase.

13.3.
.
, , .

626

13.

, . :
interface IControl
{
void Paint();
}
interface ITextBox : IControl
{
void SetText(string text);
}

Paint IControl.Paint, SetText ITextBox.SetText.


Paint ITextBox.Paint.
,
.
:
namespace System
{
public interface ICloneable
{
object Clone();
}
}

Clone System.ICloneable.Clone.

13.4.
. ,
,
.
:
interface ICloneable
{
object Clone();
}
interface IComparable
{
int CompareTo(object other);
}
class ListEntry : ICloneable, IComparable
{
public object Clone() {...}
public int CompareTo(object other) {...}
}

13.4.

627


, .
, CompareTo Clone - , . ,
, override : , ,
, - .
.
, . , ,
, :
interface IFoo { void Foo(); }
class Base
{
public void Foo();
}
// IFoo.Foo Base.Foo
class Derived : Base, IFoo { }

, , ,
( ) .

, ,
.
,
. :
interface IControl
{
void Paint();
}
interface ITextBox : IControl
{
void SetText(string text);
}
class TextBox : ITextBox
{
public void Paint() {...}
public void SetText(string text) {...}
}

TextBox IControl, ITextBox.


C , C
. ,
,
( 4.4). , ,
. ,
:

628

13.

class C<U, V> { }


interface I1<V> { }
class D : C<string, int>, I1<string> { }
class E<T> : C<int, T>, I1<T> { }


, 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] {...}
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()

13.4.

629

{
Close();
}
public void Close()
{
// ,
System.GC.SuppressFinalize(this);
}
}

, .
, .
.
abstract, virtual, override static.

. ,
.
, .

(. 3.5.4) .

:
, . ,
, .
.
.
-
, .

IEnumerable<T>,
IEnumerable. ,
:


630

13.

IEnumerator GetEnumerator()
IEnumerator<T> GetEnumerator()

IEnumerable.GetEnumerator()
, IEnumerator<T> IEnumerator
.

, ,
, , . :
class Shape: ICloneable
{
object ICloneable.Clone() {...}
int IComparable.CompareTo(object other) {...}
}

//

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() {...}
void ITextBox.SetText(string text) {...}
}

Paint
IControl.Paint.

13.4.

631

13.4.2.

, ,
.
,
. , ,

:
interface I<T>
{
void F();
}
class X<U,V>: I<U>, I<V>
{
void I<U>.F() {...}
void I<V>.F() {...}
}

// : I<U> I<V>

, , :
I<int> x = new X<int,int>();
x.F();


:
L ,
, C.
L , L.
L .
- , C, L ,
L , C .
.
X L
I<U> I<V>. , , U V , , .
,
:
interface I<T>
{
void F();
}

632

13.

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();

Derived, Derived<int,int> I<int> ( 13.4.6).



,
, C# 4.0 , , . , ,
, IEnumerable<object>, ,
IEnumerable<string>. C# 3.0 ,
. IEnumerable<string>
IEnumerable<object>, ,
IEnumerable<Object>
.
, ,
.

13.4.3.
,

( ), ,
.
, .
:
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;
}

13.4.
class C : I<object, C,
{
public void F<T>(T
public void G<T>(T
public void H<T>(T
}

633

string>
t) {...}
t) where T : C {...}
t) where T : string {...}

//
//
//

I<object,C,string>.F<T> C.F<T>.
C.F<T> ( ) T: object,
object . I<object,C,string>.
G<T> C.G<T>,
. C.H<T> ,
( string)
. ,
. , I<object,C,string>.

:
class C: I<object,C,string>
{
...
public void H<U>(U u) where U: class {...}
void I<object,C,string>.H<T>(T t) {
string s = t;
//
H<T>(t);
}
}


, . ,
s = t , T T: string,
.

13.4.4.
, .
(interface mapping).
C ,
C. I.M,
I , M,
S, C
C, :

634

13.

S ,
I M, I.M.
, S
, M, I.M. , , I.M. , S
, , ,
, .
C, . ,
,
.
A -
B , :
A B , , .
A B , , A ,
B ( A , A ).
A B , .
A B , A B ,
A , B ( A
, A ).

:
, ,
.
, , , .
:
interface ICloneable
{
object Clone();
}
class C : ICloneable
{
object ICloneable.Clone() {...}
public object Clone() {...}
}

Clone ICloneable ICloneable.


Clone C,
.

13.4.

635

,
, , . :
interface IControl
{
void Paint();
}
interface IForm
{
void Paint();
}
class Page : IControl, IForm
{
public void Paint() {...}
}

Paint IControl IForm


Paint Page. ,
.
, ,

. :
interface IBase
{
int P { get; }
}
interface IDerived : IBase
{
new int P();
}

:
class C : IDerived
{
int IBase.P { get {...} }
int IDerived.P() {...}
}
class C : IDerived
{
public int P { get {...} }
int IDerived.P() {...}
}
class C : IDerived
{
int IBase.P { get {...} }
public int P() {...}
}

636

13.

,
,
.
:
interface IControl
{
void Paint();
}
interface ITextBox : IControl
{
void SetText(string text);
}
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() { }
}

F Class1 Interface1 Class2.

13.4.

637

13.4.5.
,
.

, , .
, ,
. , , ,
.

, .


,
. , , :
interface IControl
{
void Paint();
}
class Control : IControl
{
public void Paint() {...}
}
class TextBox : Control
{
new public void Paint() {...}
}

Paint TextBox Paint Control,


Control.Paint IControl.Paint,
Paint
:
Control c =
TextBox t =
IControl ic
IControl it
c.Paint();
t.Paint();
ic.Paint();
it.Paint();

new Control();
new TextBox();
= c;
= t;
//
//
//
//

Control.Paint();
TextBox.Paint();
Control.Paint();
Control.Paint();

,

. , :

638

13.

interface IControl
{
void Paint();
}
class Control: IControl
{
public virtual void Paint() {...}
}
class TextBox: Control
{
public override void Paint() {...}
}

:
Control c =
TextBox t =
IControl ic
IControl it
c.Paint();
t.Paint();
ic.Paint();
it.Paint();

new Control();
new TextBox();
= c;
= t;
// Control.Paint();
// TextBox.Paint();
// Control.Paint();
// TextBox.Paint();


virtual, . , virtual,
.
:
interface IControl
{
void Paint();
}
class Control: IControl
{
void IControl.Paint() { PaintControl(); }
protected virtual void PaintControl() {...}
}
class TextBox: Control
{
protected override void PaintControl() {...}
}

, Control,
IControl.Paint, PaintControl.

13.4.6.
, , , .

13.4.

639

, . ,

.
:
interface IControl
{
void Paint();
}
class Control: IControl
{
void IControl.Paint() {...}
}
class MyControl: Control, IControl
{
public void Paint() {}
}

, Control IControl.Paint
Control.IControl.Paint, MyControl,
IControl.Paint MyControl.Paint.

. :
interface IMethods
{
void F();
void G();
void H();
void I();
}
class Base: IMethods
{
void IMethods.F() {}
void IMethods.G() {}
public void H() {}
public void I() {}
}
class Derived: Base, IMethods
{
public void F() {}
void IMethods.H() {}
}

IMethods Derived
Derived.F, Base.IMethods.G, Derived.IMethods.H Base.I.
,
. , . :

640

13.

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() {...}
public void G() {...}
}

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();
}

13.4.

641

IMethods F G ,
,
C.
, , , , . :
interface IMethods
{
void F();
void G();
}
abstract class C: IMethods
{
void IMethods.F() { FF(); }
void IMethods.G() { GG(); }
protected abstract void FF();
protected abstract void GG();
}

, C, FF GG,
IMethods.

14

- ( 4.1),
.

, .
, C#.
, Java , C#. Java : ,
, , . Java- C#,
( ) .

:
enum Color
{
Red,
Green,
Blue
}

Color, Red,
Green Blue.

14.1.
. enum ,
.
-:
opt
opt

enum

-opt

-:
:
-
-:
{
--opt
{
--opt

}
,

;opt

14.2.

643

,
.
, .
byte, sbyte, short, ushort, int, uint,
long ulong.

,
. , Color
:
enum Color: System.Int64 { ... }

:
enum Color : long
{
Red,
Green,
Blue
}

long. long, , ,
long, int,
.

14.2.
- :
-:
-
-

-:
new
public
protected
internal
private

, .
,
( 10.1.1). :
abstract sealed.
.

644

14.

14.3.
, .
.
--:
--
--
--:

opt

=
opt

--


Microsoft # value__, .

.
, .
. :
enum Color : uint
{
Red = -1,
Green = -2,
Blue = -3
}

,
-1, -2 -3 uint.

. :
enum Color
{
Red,
Green,
Blue,
Max = Blue
}

Blue Max .
, .
-, , ,
. ,
, :

14.3.

645

 0.

, 1.
,
.

,
. [Flags] ,
1, 2, 4, 8 . ., None 0.
,
, , ,
.


,
. Color.Red.StringFromColor() ,
StringFromColor(Color.Red).

.

:
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);

646

14.
default:
return "Invalid color";
}

}
}

.
Red = 0
Green = 10
Blue = 11

:
 Red
(
).
 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.

14.5.

647

Enum ( 4.3.2) System.Enum .


, System.Enum . ,
, . System.Enum System.ValueType ( 4.1.1), , ,
object. System.Enum nul
.

14.5.
;

( 6.2.2). , , . ,
.
( ,
;
. 14.3). , E,
v, (E)v.
:
==, !=, <, >, <=, >= ( 7.10.5), + ( 7.8.4), - ( 7.8.5), ^, &, | ( 7.11.2), ~ ( 7.7.4), ++ -- ( 7.6.9 7.7.5).

, [Flags],
.

System.Enum (, , System.ValueType object).



.

15

, C++, Pascal Modula . C++


-. ,
C++ -,
, .

,
. C#, 1.0.
C# ( )
, . C# .
LINQ-, , . .

.NET, ,
( ) .

, System.
Delegate. ,
,
. .
.
.
,
;
( 15.1) .
.

. -
; ,
.

15.1.

649

15.1.
- - ( 9.6),
.
-:
opt -opt
delegate
--
---opt
( --opt ) ---opt
-:
-
-

-:
new
public
protected
internal
private


, .
new , . ,
, 10.3.4.
public, protected, internal private
. , ,
( 3.5.1).
.
-- , -- .
--- ( 13.1.3) .
void, (output-safe, 13.1.3.1).
(input-safe). , out ref
. , out

.

, out ref: ,
out. , out ,
, C#, .
out


650

15.

,
. ,
.

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) {...}
public static void M4(int g) {...}
}

D1 D2 A.M1 B.M1,
; , .
D1 D2 B.M2, B.M3 B.M4,
.

, , . - ,
( , ).
.
, ;
Predicate<int> Func<int,
bool>, . , ,
.

, .

15.1.

651

.
,
. :
delegate bool Predicate<T>(T value);
class X
{
static bool F(int i) {...}
static bool G(string s) {...}
}

Predicate<int> X.F,
Predicate<string> X.G.
-. , System.Delegate. -
sealed - . System.Delegate ,
. , System.Delegate
; , .
C#
. , ,
,
. ,
System.Delegate, .
, , . ( 15.2)
,
. ,
,
,
.


, . ,
, . ,
.

+ ( 7.8.4) +=
( 7.17.2).
- ( 7.8.5) -= ( 7.17.2).
( 7.10.8).

:

652

15.

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
// M2
// M1 + M2
// M1 + M2 + M1
// 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.

15.3.
-- ( 7.6.10.5)
.
:

15.4.

653

, --;
( 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.6.5.3
.)
,
catch ,
, .
, , . ,
.
- ( 10.6.1.2),
; ,
, . -

654

15.

,
.

, catch , ,
.

,
, ,
. , , , .
, , .


, , . , GetInvocationList,
. ,
try-catch. - .

, null, System.NullReferenceException.

; , , . , , ,
Invoke ,
, .
, C#
. ,
, void
out.
,
, , .


-
:
var temp = this.mydelegate;
if (temp != null) temp();

15.4.

655

:
if (this.mydelegate != null) this.mydelegate();

,
mydelegate , null
. , .
, .
mydelegate. mydelegate
null, , mydelegate
, . ! ( , , )
, - , , .
, , ,
, .

, ,
, :
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);
cd3 += cd1;
cd3(20);

// M1, M2
// M1, M2, M1

656

15.
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;,
.
.
.
, cd3 -= cd1;,
cd3 .
( )
.
:
C.M1:
C.M2:
C.M1:
C.M2:
C.M1:
C.M2:
C.M1:
C.M1:
C.M2:
C.M1:
C.M3:
C.M1:
C.M2:
C.M3:
C.M1:
C.M2:
C.M1:
C.M1:

-1
-2
10
10
20
20
20
30
30
30
30
40
40
40
50
50
60
60

16

C# ,

, . C#
C++, .


, ,
ApplicationException. ,
. , .

C# , System.Exception. C++
.

CLR -
C#, catch .
C#, . CLR ,
, -,
.

C# nally ( 8.10) , ,
. C++
.

, nally . , . nally , (1) ,
try, , (2)
nally, (fail-fast)
, (3) - .


658

16.

, , , nally.
, ,
, .

C# , ,
null, .

, ,
C# C++.
C# , C++ (
), , .



.NET, . .NET ,
- -, ,
. ,
,
.


( )
, NaN,
NaN (.
7.8.1). 3 4 ,
NaN. , , . ,

; .

16.1.
.
throw ( 8.9.5) . , throw.
,
C#, -

16.3.

659

, .
, ( 7.8.2) System.DivideByZeroException, .
, , 16.4.

Exception; Exception .

16.2. System.Exception
System.Exception .
, :
Message string, , .
InnerException Exception, .
null, , , , catch, InnerException. null
,
.
.
System.Exception.

, , . ,
, ,
, . , , , , , ,
. , .

16.3.
try ( 8.10).
, catch, , .
try,
catch. , ,

660

16.

, try,
. ,
catch, ,
,
,
. catch, ,
.
catch,
catch.
catch nally, try, , .
catch , :
catch ( 10.12) , System.
TypeInitializationException.
System.TypeInitializationException.
catch ,
, . .

CLR
. , ,
. ,
, , , ,
, .
:
. ,
, ; - .

, .
,
( ). (
object) , .

C# .
try-nally, nally.

16.4.

661

16.4.

C#.

System.ArithmeticException

System.ArrayTypeMismatchException

System.DivideByZeroException
System.IndexOutOfRangeException

System.InvalidCastException

System.NullReferenceException

System.OutOfMemoryException
System.OverowException

System.StackOverowException

System.TypeInitializationException

,
, System.DivideByZeroException System.OverflowException

- ,


,
,

, ,
null, ,
,


( new)
, ,
(checked),

,
;

,
catch,


,
,
C#.
( ). , , System.ArgumentException , ,
.

662

16.


: , , .
,
; , . .
; ,
.
,
; , . . ;
- . .
, , API , . ,
GUID?;
GUID , ,
. ,
.
, ,
: CD
, . .

17

C#
. ,
-
public, protected, internal private.
C#
, .
. ,
HelpAttribute,
( ), .

Silverlight
WPF, (view states), .

. -
, .

( 17.1), ( 17.12). C#
( 17.2)
( 17.3).

, . , , Book,
Author. : ,
. Book AuthorAttribute,
, . Television
Obsolete, , .
ObsoleteAttribute , ,
, , .

664

17.

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 {...}

A t t r i b u t e U s a g e ( 17.1.2)
AllowMultiple, ,
. AllowMultiple
true, . AllowMultiple
false ,

. :
using System;
[AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
public class AuthorAttribute: Attribute
{
private string name;

17.1.

665

public AuthorAttribute(string name) {


this.name = name;
}
public string Name {
get { return name; }
}
}

AuthorAttribute.
[Author("Brian Kernighan"), Author("Dennis Ritchie")]
class Class1
{
...
}

, Author .

, , :
! .


, , , ,
Class1.

AttributeUsage Inherited,
, , , . Inherited -
true, . false,
. ,
true.
X, AttributeUsage,

using System;
class X: Attribute {...}

:
using System;
[AttributeUsage(
AttributeTargets.All,
AllowMultiple = false,
Inherited = true)
]
class X: Attribute {...}

666

17.

17.1.2.


.
, ,
AuthorAttribute.

.
.

.
:
using System;
[AttributeUsage(AttributeTargets.Class)]
public class HelpAttribute: Attribute
{
public HelpAttribute(string url) { //
...
}
public string Topic {
get {...}
set {...}
}

//

public string Url {


get {...}
}
}

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.2.

667

17.1.3.

:
: bool, byte, char, double, oat, int, long, sbyte,
short, string, uint, ulong, ushort.
object.
System.Type.
, , , ,
( ), ( 17.2).
.

, decimal ,
decimal.
CLI C#.


,
. , C# 4.0
dynamic , object.

, , ,
.

17.2.
. ,
. (
), - ( 9.6), -- ( 10.1.5), -- ( 13.2),
-- ( 11.2), -- ( 14.3), -- ( 10.7.2), --- ( 10.8.1) --
( 10.6.1).
.
,

668

17.

, . ,
,
, . , [A][B], [B][A],
[A, B] [B, A] .
-:
--
--:
--
-- --
--:
[ ----
[ ----

-
-

----
---
:
---:
assembly
module
:
-
-:
-
-

-:
[
---opt
[
---opt
---:
--
:
--:
eld
event
method
param
property
return
type
-:

-
:
-
-:
-

-opt

-
-

]
,

]
,

669

17.2.
-:
(
--opt
)
(
--
,
--
(
--
)
--:
-
--

-:
-opt
--
--:
-
--
-:

--

--:

-
. ( )
. -; , , --,
, .
.
- . -
-, . . :
class Class1 {}
[Class1] class Class2 {}

//

,
Class1,
.

. - .
, ----.
,
---
(
). , -- , .
:
, ,
, .

670

17.

, ---. assembly ,
, module ,
.
, , , . --- .
type , ,
return , .
, , , . --- .
method , ,
return ,
.
, , , . --- .
method , ,
return , .
, ,
, ,
( )
(add) (remove). --- .
event , ;
eld , , method , .
, get , ,
. ---
. method ,
, return ,
.
, set , ,
. --- .
method , ;
param , , return , .

17.2.

671

,
, , . --- . method ,
; param ,
, return ,
.
---
, . ,
, type:
[type: Author("Brian Kernighan")]
class Class1 {}
[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 {}

// X

[@XAttribute]
class Class4 {}

// XAttribute

672

17.

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;
[AttributeUsage(AttributeTargets.Class)]
public class HelpStringAttribute: Attribute
{
string value;
public HelpStringAttribute(string value) {
this.value = value;
}
public string Value {
get {...}
}
}
[HelpString("Description of Class1")]
[HelpString("Another description of Class1")]
public class Class1 {}

, Class1 HelpString, .
E --,
:
E ( 17.1.3).

17.2.

673

E
:
 .
 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(oat))]
class MyClass {}

-typeof ( 7.6.11), , ,
(closed) , (open) . ,
.
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;
}

// :

//
//
//
//

:

:

674

17.

17.3.
, . ,
. ,
.
, , .

17.3.1.
- T, -- P
- N :

--, new T(P). , T
C,
.
C , , .
- Arg N:
 Name - Arg.
 Name T, . T , .

: T, C T,
-- P -- N.

.
,
, ,
.

17.3.2.

T,
C T, -- P --

17.4.

675

N.
:

--, new T(P), C, . , O T.
- Arg N, :
 Name - Arg.
Name O,
, .
 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

676

17.

{
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,
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 {...} }
}
}

17.4.2.1.
, Conditional, .
Conditional ,

17.4.

677

. , , .
, ; (
) .
:
- . Conditional ,
, .
void.
override.
virtual . , , Conditional.
. .
, --. :
#dene DEBUG
using System;
using System.Diagnostics;
class Class1
{
[Conditional("DEBUG")]
public static void M() {
Console.WriteLine("Executed Class1.M");
}
}
class Class2
{
public static void Test() {
Class1.M();
}
}

Class1.M .
Test Class2. DEBUG ,
Class2.Test M. DEBUG , Class2.Test Class1.M.
, . .
class1.cs:
using System.Diagnostics;
class Class1
{
[Conditional("DEBUG")]
public static void F() {

678

17.
Console.WriteLine("Executed Class1.F");
}

class2.cs:
#dene DEBUG
class Class2
{
public static void G() {
Class1.F();
}
}

// F

class3.cs:
#undef DEBUG
class Class3
{
public static void H() {
Class1.F();
}
}

// F

Class2 Class3
Class1.F, DEBUG.
Class2,
Class3, F Class2 , Class3 .
. , base base.M,
.
.
class1.cs:
using System;
using System.Diagnostics;
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 !
}
}

17.4.

679

class3.cs:
#dene DEBUG
using System;
class Class3
{
public static void Test() {
Class2 c = new Class2();
c.M();
// M
}
}

Class2 M, .
, DEBUG, . ,
Class2.M. - (pp-declarations)
.

17.4.2.2.
( 17.1),
Conditional, . , ,
Conditional. :
using System;
using System.Diagnostics;
[Conditional("ALPHA")]
[Conditional("BETA")]
public class TestAttribute : Attribute {}

TestAttribute ,
ALPHA BETA.
( 17.2) ,

;
.
,

. :
test.cs:
using System;
using System.Diagnostics;
[Conditional("DEBUG")]
public class TestAttribute : Attribute {}

680

17.

class1.cs:
#dene DEBUG
[Test]
class Class1 {}

// TestAttribute

class2.cs:
#undef DEBUG
[Test]
class Class2 {}

// TestAttribute

Class1 Class2 Test,


, DEBUG.
Class1 Class2,
Test Class1 , Class2 .

, , , , .
- :
#if DEBUG
int counter;
#endif
[Conditional("DEBUG")] void DoIt(int x) { ... }
...
DoIt(this.counter);

counter , DEBUG
. , DoIt? ,
DoInt(int) , DEBUG .
, DoIt(int), - ?
, counter int , ,
. , .

17.4.3. Obsolete
Obsolete , ,
.
namespace System
{
[AttributeUsage(
AttributeTargets.Class |
AttributeTargets.Struct |
AttributeTargets.Enum |
AttributeTargets.Interface |
AttributeTargets.Delegate |
AttributeTargets.Method |
AttributeTargets.Constructor |
AttributeTargets.Property |
AttributeTargets.Field |
AttributeTargets.Event,

17.4.

681

Inherited = false)
]
public class ObsoleteAttribute : Attribute
{
public ObsoleteAttribute() {...}
public ObsoleteAttribute(string message) {...}
public ObsoleteAttribute(string message, bool error) {...}
public string Message { get {...} }
public bool IsError { get {...} }
}
}

,
Obsolete, . , error , false,
. error 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.

: - ,
,
. -
(, , ) ,

682

17.

. , ,
, .



. , Obsolete,
, ,
Obsolete.

17.5.
. C# .NET.

17.5.1.
COM Win32
.NET , C# ,
COM (DLL) Win32. , DllImport ,
static extern, ,
Win32 DLL. System.Runtime.
InteropServices, .NET.

17.5.2. .NET
17.5.2.1. 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 {...} }
}
}

18

C#, , C C++ . C#
,
. C# , C C++. C#
,
, , .
, ,
C C++.
C
C++ C#, , . ,
,
,
.
, C#
.

, .NET (
1.0), .
.

,
,
. . C C#.

, .
unsafe,
,
, .

684

18.

18.1.
C# .
unsafe -unsafe:
, , unsafe,
( , )
.
, , , , , , ,
unsafe,
.
-unsafe .
.
. ,
, ().
-:

unsafe
-:

unsafe
-:

unsafe
-:

unsafe
-:

unsafe
-:

unsafe
-:

unsafe
-:

unsafe

18.1.

685

-:

unsafe
-:

unsafe
-:

unsafe
-:
externopt
unsafeopt
~
opt
-
unsafeopt
externopt
~
opt
-

--:
unsafeopt
static
externopt
externopt
static
unsafeopt
static
unsafeopt
externopt
unsafeopt
static
externopt
static
externopt
unsafeopt
static
unsafeopt
externopt
-:

-unsafe
-unsafe:
unsafe

:
public unsafe struct Node
{
public int Value;
public Node* Left;
public Node* Right;
}

unsafe, , . Left Right, .


:
public struct Node
{
public int Value;
public unsafe Node* Left;
public unsafe Node* Right;
}

unsafe ,
.

686

18.

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 , .
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.6.10.6)
, .

18.2.

687

- - void,
*:
-:
-
void *

-:

, * ,
. , .
( )
,
.
, ,
-.
- , -
- . , :
sbyte, byte, short, ushort, int, uint, long, ulong, char, oat, double, decimal
bool.
-.
-.
-, , .

: ,
.
.

byte*
char*
int**
int*[]
void*

byte
char
int
int

688

18.

C C++, , C# * ,
. :
int* pi, pj;

// int *pi, *pj;

, T*,
T. * ( 18.5.1). , P
int* *P int, ,
P.
, null. .
null .
void* . , , - .
void* ( ).
. -, object,
object. ,
( 4.3). ,
. 18.4.
- ( 4.4), ( 7.5.2) , .
- -
( 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;
xed (int* pj = &value)
{
// ...
pi2 = pj;
}
}

18.2.

689

static void Main()


{
int i = 10;
unsafe
{
int* px1;
int* px2 = &i;
F(out px1, ref px2);
Console.WriteLine("*px1 = {0}, *px2 = {1}", *px1, *px2);
//
}
}
}

- ,
. , , int,
- int, , , null:
unsafe static int* Find(int* pi, int size, int value) {
for (int i = 0; i < size; ++i) {
if (*pi == value)
return pi;
++pi;
}
return null;
}

:
* ( 18.5.1).
->
( 18.5.2).
[]
( 18.5.3).
& ( 18.5.4).
++ -- ( 18.5.5).
+ -
( 18.5.6).
==, !=, <, >, <= >= ( 18.5.7).
stackalloc
( 18.7).
xed ,
( 18.6).

690

18.

18.3.

( 18.5.4) xed ( 18.6)


:
.
, . (
, - , .) , , , .
( .)
& ( 18.5.4) .
xed ( 18.6), ,
xed.
, :
, - ( 7.6.2),
-,
.
, -- ( 7.6.4)
V.I, V -.
, -- ( 18.5.1) *P, ----- ( 18.5.2) P->I ---- ( 18.5.3) P[E].
.
, . , ref out , , ,
. , , ,
,
.

18.4.
( 6.1)
:
- void*.
null -.

18.4.

691

,
( 6.2) :
- -.
sbyte, byte, short, ushort, int, uint, long ulong .
- sbyte, byte, short, ushort, int, uint, long
ulong.
, ( 6.3.1) :
- void*.
. , , .

, ,
.
: A
B, , , C,
A .
, :
char c = A;
char* pc = &c;
void* pv = pc;
int* pi = (int*)pv;
int i = *pi;
*pi = 123456;

//
//

byte, .
, ,
. ,
double :
using System;
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();
}
}
}

692

18.

, .

, , , ,
. . ,
NaN
IEEE (. 7.8.1):
unsafe static int GetNaNPayload(oat f) {
oat* p = &f;
return *((int*)p) & 0x003FFFFF;
}
unsafe static oat MakeNaNPayload(int nanbits) {
oat nan = Single.NaN;
oat* p = &nan;
*((int*)p) |= (nanbits & 0x003FFFFF);
return nan;
}

, , ,
, .

.
32- 64-

, uint ulong
.

18.4.1.
. ,
:
( 6.1.6) -
System.Array
. System.
Array
, object.

( 6.1.6, 6.2.4) S[] System.Collections.
Generic.IList<T> ,
,
, .

18.5.

693

( 6.2.4) System.Array
- .
( 6.2.4.) System.Collections.Generic.IList<S> T[],
,
, .
, foreach , 8.8.4, .
foreach
foreach (V v in x) -

x T[,,,], n 1,
T V ,
for :
{
T[,,...,] a = x;
V v;
for (int i0 = a.GetLowerBound(0); i0 <= a.GetUpperBound(0); i0++)
for (int i1 = a.GetLowerBound(1); i1 <= a.GetUpperBound(1); i1++)
...
for (int in = a.GetLowerBound(n); in <= a.GetUpperBound(n); in++) {
v = (V)a.GetValue(i0,i1,...,in);
-
}
}

a, i0, i1, , in x, -
. v
-.
( 18.4) T ( ) V,
. x null,
System.NullReferenceException.

18.5.

.
,
. ,
, - ( 7.6.2), -- ( 7.6.4), - ( 7.6.5)
-- ( 7.6.6) .
--- ( 7.6) - ( 7.7)
:

694

18.

----:

-----
----
-sizeof
-:

--
--

. .

18.5.1.
-- (*),
-.
--:
*
-

*
, . *P,
P T*, T. * void* ,
, .
* ,
null, . , , System.NullReferenceException.
,
* . , ,
(. 18.4), ,
.
, , ,
*P, ( 5.3.1).

18.5.2.
----- -,
-> :
-----:
-
->

--opt

P->I P
, void*, I
, P.

18.5.

695


----- , (, int**),
.

P->I , (*P).I. (*)


18.5.1. (.)
7.6.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 :
class Test
{
static void Main()
{
Point point;
unsafe
{
Point* p = &point;
(*p).x = 10;
(*p).y = 20;
Console.WriteLine((*p).ToString());
}
}
}

696

18.

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.
-- (&),
-.

18.5.

697

--:
&
-

E, T
( 18.3), &E ,
. T*,
. E , , ,
.
( 18.6), ,
. 7.6.4,
,
(readonly) , ,
. .
.
& , , , ,
,
.
.
:
using System;
class Test
{
static void Main()
{
int i;
unsafe
{
int* p = &i;
*p = 123;
}
Console.WriteLine(i);
}
}

i , p
&i. *p i, , ,
,
.
&, . ,
API ,
. API
, .

698

18.

18.5.5.
++ -- ( 7.6.9 7.7.5) , void*. , T*
:
T* operator ++(T* x);
T* operator --(T* x);

x + 1 x 1
( 18.5.6). , , T*,
++ sizeof(T) , , -- sizeof(T) .

, , .

18.5.6.
+ - ( 7.8.4 7.8.5) void*. ,
T* :
T*
T*
T*
T*

operator
operator
operator
operator

+(T*
+(T*
+(T*
+(T*

x,
x,
x,
x,

T*
T*
T*
T*

operator
operator
operator
operator

+(int x, T* y);
+(uint x, T* y);
+(long x, T* y);
+(ulong x, T* y);

T*
T*
T*
T*

operator
operator
operator
operator

-(T*
-(T*
-(T*
-(T*

x,
x,
x,
x,

int y);
uint y);
long y);
ulong y);

int y);
uint y);
long y);
ulong y);

long operator -(T* x, T* y);

P, T*, N,
int, uint, long ulong, P + N N + P T*, N * sizeof(T) , P.
, P - N T*,
N * sizeof(T) , P.
P Q, T*, P - Q , P Q, sizeof(T).
long. , P - Q ((long)(P)
- (long)(Q)) / sizeof(T). :

18.5.

699

using System;
class Test
{
static void Main()
{
unsafe
{
int* values = stackalloc int[20];
int* p = &values[1];
int* q = &values[15];
Console.WriteLine("p - q = {0}", p - q);
Console.WriteLine("q - p = {0}", q - p);
}
}
}

:
p - q = -14
q - p = 14

, , ,
.

18.5.7.
==, !=, <, >, <= >= ( 7.10) .
:
bool
bool
bool
bool
bool
bool

operator
operator
operator
operator
operator
operator

==(void* x, void* y);


!=(void* x, void* y);
<(void* x, void* y);
>(void* x, void* y);
<=(void* x, void* y);
>=(void* x, void* y);


void*, . , ,
.

18.5.8. sizeof
sizeof ,
. , sizeof, ( 18.2).
-sizeof:
sizeof
(
-

700

18.

sizeof int.
sizeof
.

sizeof(sbyte)
sizeof(byte)
sizeof(short)
sizeof(ushort)
sizeof(int)
sizeof(uint)
sizeof(long)
sizeof(ulong)
sizeof(char)
sizeof(oat)
sizeof(double)
sizeof(bool)

1
1
2
2
4
4
8
8
2
4
8
1

sizeof
, .
, , .
, (padding). ,
, .
sizeof , ,
.

18.6. fixed
-
( 8)
xed, ,
.
-:

-xed
-xed:
xed
(
-
-

--

--:
--
--

--

18.6. fixed

701

--:

=
--
--:
&
--

-- - ,
--. , xed, -, ,
- xed. , xed, . (
++ --) ref out
.
--
:
&, -- ( 5.3.3), ( 18.3) T,
, T* ,
xed. , ,
xed.
- T, ,
T* , xed.
, , xed.
null , xed
.
string, , char* , xed. , ,

xed. null, xed .
- ---, , ,
, , , xed.
( 18.7.2), , xed.

702

18.


,
: xed
, .
,
, . ,
.

, -, xed , ,
,
. , , -, , xed ,
.
, , xed,
, . ,
, xed, API, , API .

, ,
xed, , GCHandle. -
.

(
). ,

. :
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
{

18.6. fixed
xed
xed
xed
xed

(int*
(int*
(int*
(int*

p
p
p
p

=
=
=
=

703

&x) F(p);
&t.y) F(p);
&a[0]) F(p);
a) F(p);

}
}
}


fixed.
, ,
.
& ,
.
xed ,
.
xed 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
{
xed (char* p = name) F(p);
xed (char* p = "xx") F(p);
}
}
}


, 0 Length - 1. ,
, ,
. xed, p
a, , p p + a.Length - 1,
. , p[0] p[a.Length - 1] .

.
:
using System;
class Test
{
static void Main()

704

18.

{
int[, ,] a = new int[2, 3, 4];
unsafe
{
xed (int* p = a)
{
for (int i = 0; i < a.Length; ++i)

//
//

p[i] = i;
}
}
for (int i = 0; i < 2; ++i)
for (int j = 0; j < 3; ++j)
{
for (int k = 0; k < 4; ++k)
Console.Write("[{0},{1},{2}] = {3,2} ", i, j,
k, a[i, j, k]);
Console.WriteLine();
}
}
}

:
[0,0,0]
[0,1,0]
[0,2,0]
[1,0,0]
[1,1,0]
[1,2,0]

= 0 [0,0,1] = 1 [0,0,2] =
= 4 [0,1,1] = 5 [0,1,2] =
= 8 [0,2,1] = 9 [0,2,2] =
= 12 [1,0,1] = 13 [1,0,2] =
= 16 [1,1,1] = 17 [1,1,2] =
= 20 [1,2,1] = 21 [1,2,2] =

2
6
10
14
18
22

[0,0,3]
[0,1,3]
[0,2,3]
[1,0,3]
[1,1,3]
[1,2,3]

=
=
=
=
=
=

3
7
11
15
19
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
{
xed (int* p = a) Fill(p, 100, -1);
}
}
}

xed ,
, .
:
unsafe struct Font
{
public int size;
public xed char name[32];
}

18.7.

705

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;
unsafe static void Main()
{
Test test = new Test();
test.f.size = 10;
xed (char* p = test.f.name)
{
PutString("Times New Roman", p, 32);
}
}
}

xed , ,
.
char*, ,
, . xed, p s, ,
p p + Length - 1, ,
p + s.Length (
'\0').
. ,
, , , , ,
.

API, C. ,
. ,
char*, ,
.

18.7.
C
API.

706

18.

18.7.1.

-- , , .

.
( 18.1).
--:

---
---:
---opt
xed
opt
--
---

---:
---
---
---
---:
new
public
protected
internal
private
unsafe
--:

---:
---
---

---

---:

[ - ]

( 17), new ( 10.2.2), ( 10.2.3) unsafe
( 18.1). , . , .

static.
(-), (-) .

18.7.

707

sbyte, byte, short, ushort, int, uint, long,


ulong, char, oat, double bool.
, .
,
, , [
]. ,
. int,
.
.
,
,
.
:
unsafe struct A
{
public xed int x[5], y[10], z[100];
}

:
unsafe struct A
{
public xed int x[5];
public xed int y[10];
public xed 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 , .

708

18.

, E ( 18.3),
E.I --
( 18.6), .
E
I E. S*, S I,
.
.
,
, .
,
:
unsafe struct Font
{
public int size;
public xed 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;
}
unsafe static void Main()
{
Font f;
f.size = 10;
PutString("Times New Roman", f.name, 32);
}
}

18.7.3.

(5.3), ,
, , .
, , ,
, ( 5.2).
.

18.8.

709

18.8.
( 8.5.1)
,
.

; ,
.
, CLR ,
, . ( ,
, x86.)
, stackalloc
. ,
.
--:

-stackalloc
-stackalloc:
stackalloc
-

- , , .
. , -,
,
.
stackalloc T[E] ,
T ( 18.2), E int.
E * sizeof(T)
T* . E ,
. E , , . ,
System.StackOverowException.
.
catch nally ( 8.10).
,
stackalloc. ,
-, .
alloca , C C++.

710

18.

:
using System;
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();

18.9.

711

// ,
// .
private Memory() { }
// .
// .
public static void* Alloc(int size)
{
void* result = HeapAlloc(ph, HEAP_ZERO_MEMORY, size);
if (result == null) throw new OutOfMemoryException();
return result;
}
// count src dst.
// .
public static void Copy(void* src, void* dst, int count)
{
byte* ps = (byte*)src;
byte* pd = (byte*)dst;
if (ps > pd)
{
for (; count != 0; count--) *pd++ = *ps++;
}
else if (ps < pd)
{
for (ps += count, pd += count; count != 0; count--)
*--pd = *--ps;
}
}
// .
public static void Free(void* block)
{
if (!HeapFree(ph, 0, block)) throw new InvalidOperationException();
}
// .
// ,
// .
public static void* ReAlloc(void* block, int size)
{
void* result = HeapReAlloc(ph, HEAP_ZERO_MEMORY, block, size);
if (result == null) throw new OutOfMemoryException();
return result;
}
// .
public static int SizeOf(void* block)
{
int result = HeapSize(ph, 0, block);

712

18.
if (result == -1) throw new InvalidOperationException();
return result;

}
// , API .
const int HEAP_ZERO_MEMORY = 0x00000008;
// API .
[DllImport("kernel32")]
static extern int GetProcessHeap();
[DllImport("kernel32")]
static extern void* HeapAlloc(int hHeap, int ags, int size);
[DllImport("kernel32")]
static extern bool HeapFree(int hHeap, int ags, void* block);
[DllImport("kernel32")]
static extern void* HeapReAlloc(int hHeap, int ags,
void* block, int size);
[DllImport("kernel32")]
static extern int HeapSize(int hHeap, int ags, void* block);
}

, Memory:
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];
xed (byte* p = array) Memory.Copy(buffer, p, 256);
}
nally
{
Memory.Free(buffer);
}
for (int i = 0; i < 256; i++) Console.WriteLine(array[i]);
}
}
}

Memory.Alloc 256 ,
0 255.
, 256 , Memory.Copy . ,
Memory.Free, .

C# , , XML.
, ,
,
, , XML. , , .

(, , ) (, , , ). , XML, XML. ( , C#).
,
, .

. , , XML.

.1.
, ,
XML , .
, (///),
, (/**).
(, , ) (, , ,
), . ( 17.2)
,
, .
:
--:
/// -opt
--:
/** -opt */

714

, ,
/// ,
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). ,
,
, .
, .2
.
:
<param> . ,
, .
, .
cref .

. ,
. , cref,

using . (, List<T>) ,
XML.
(, List{T}) XML (,
List&lt;T&gt;).
<summary> .
<include> XML.
,
(, ). -

.2.

715

.2.
, XML. . .1 . (
.)
.1.

<c>
<code>

A.2.1
A.2.2

<example>
<exception>

A.2.3
A.2.4

<include>
<list>
<para>
<param>
<paramref>
<permission>
<remark>

A.2.5
A.2.6
A.2.7
A.2.8
A.2.9
A.2.10
A.2.11

<returns>
<see>
<seealso>
<summary>
<value>
<typeparam>
<typeparamref>

A.2.12
A.2.13
A.2.14
A.2.15
A.2.16
A.2.17
A.2.18

,



,

XML









See Also



-

A.2.1. <c>
,
, . <code> ( A.2.2).
:
<c></c>

716

:
/// <summary> <c>Point</c>
/// </summary>
public class Point
{
// ...
}

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>
public void Translate(int xor, int yor) {
X += xor;
Y += yor;
}

A.2.3. <example>
. .
<code> ( A.2.2).
:
<example></example>

:
. <code> ( A.2.2).

A.2.4. <exception>
, .
:
<exception cref=""></exception>

cref=""

.2.

717

.
,
.

, .
:
public class DataBaseOperations
{
/// <exception cref="MasterFileFormatCorruptException"></exception>
/// <exception cref="MasterFileLockedOpenException"></exception>
public static void ReadRecord(int ag) {
if (ag == 1)
throw new MasterFileFormatCorruptException();
else if (ag == 2)
throw new MasterFileLockedOpenException();
//
}
}

A.2.5. <include>
XML,
. XML.
, XML ,
XPath. <include> XML
.
:
<include le=" " path="xpath" />

le=" "

XML. ,
.
path="xpath"

XPath, XML XML.


:
:
/// <include le="docs.xml" path=extradoc/class[@name="IntList"]/* />
public class IntList { }

docs.xml :
<?xml version="1.0"?>
<extradoc>
<class name="IntList">
<summary>
.
</summary>
</class>
<class name="StringList">

718

<summary>
.
</summary>
</class>
</extradoc>

, :
/// <summary>
/// .
/// </summary>
public class IntList { }

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>Item 1.</description>

.2.

719

/// </item>
/// <item>
/// <description>Item 2.</description>
/// </item>
/// </list>
/// </summary>
public static void Main () {
// ...
}
}

A.2.7. <para>
,
<summary> ( A.2.11) <returns> ( A.2.12). .
:
<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>

720

public void Move(int xor, int yor) {


X = xor;
Y = yor;
}

A.2.9. <paramref>
, .

- .
:
<paramref name=""/>

.
:
/// <summary>
/// (<paramref name="xor"/>,<paramref name="yor"/>).</summary>
/// <param name="xor"> x.</param>
/// <param name="yor"> y.</param>
public Point(int xor, int yor) {
X = xor;
Y = yor;
}

A.2.10. <permission>
.
:
<permission cref=""></permission>

cref=""

.
,
.

.
:
/// <permission cref="System.Security.PermissionSet">
/// .</permission>
public static void Test() {
// ...
}

.2.

721

A.2.11. <remark>
. ( <summary> ( A.2.15) .)
:
<remark></remark>

.
:
/// <summary> <c>Point</c>
/// .</summary>
/// <remark> </remark>
public class Point
{
// ...
}

A.2.12. <returns>
.
:
<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=""

.
,
.

722

:
/// <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=""

.
,
.
:
/// <summary> ,
/// .</summary>
/// <seealso cref="operator=="/>
/// <seealso cref="operator!="/>
public override bool Equals(object o) {
// ...
}

A.2.15. <summary>
. <remark> ( A.2.11) .
:
<summary></summary>

.2.

723

.
:
/// <summary> (0,0).</summary>
public Point() : this(0,0) {
}

A.2.16. <value>
.
:
<value> </value>

.
:
/// <value> <c>X</c> .</value>
public int X
{
get { return x; }
set { x = value; }
}

A.2.17. <typeparam>
,
, , .
:
<typeparam name=""></typeparam>

name

-.

-.
:
/// <summary> .</summary>
/// <typeparam name="T"> .</typeparam>
public class MyList<T> {
...
}

A.2.18. <typeparamref>
, .
- .

724

:
<typeparamref name=""/>

-.
:
/// <summary>
/// <typeparamref name="T"> "/>"> .</summary>
/// <param name="string"> </param>
public List<T> FetchData<T>(string query) {
...
}

A.3.
ID-,
. ID- .
ID- , .

, , ID-
.

A.3.1. ID-
ID-
:
.

, . . .2.
.2.

E
F
M
N
P
T
!

( , )

(, , , , )
. . ,
.

A.3.

725

, . ,
. , # (U+0023). (,
.)
, . , .
.
CLI :
 ,
, :
 , ,
, -.
 , ref out, @. ,
params, .
 , ,
[ : , , : ], 1, , , .
- ,
. []
.
 , void,
*.
System.Void.
 , , ,
`, - (
).
 , , , ``
, .

A.3.2. ID-

ID- ,

,
:

726

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;
}
class Widget: IProcess
{
public class NestedClass
{
private int value;
}
private string message;
private static Color defaultColor;
private const double PI = 3.14159;
protected readonly double monthlyAverage;
private long[] array1;
private Widget[,] array2;
private unsafe int *pCount;
private unsafe oat **ppValues;
}
}
"F:Acme.ValueType.total"
"F:Acme.Widget.NestedClass.value"
"F:Acme.Widget.message"
"F:Acme.Widget.defaultColor"

A.3.

727

"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() {...}
public Widget(string s) {...}
}
}
"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) {...}
}
class Widget: IProcess
{
public class NestedClass
{
public void M(int i) {...}
}
public static void M0() {...}
public void M1(char c, out oat f, ref ValueType v) {...}
public void M2(short[] x1, int[,] x2, long[][] x3) {...}
public void M3(long[][] x3, Widget[][,,] x4) {...}
public unsafe void M4(char *pc, Color **pf) {...}
public unsafe void M5(void *pv, double *[][,] pd) {...}
public void M6(int i, params object[] args) {...}
}
class MyList<T>
{
public void Test(T t) { }
}

728

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)"

:
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.

A.4.

729

:
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:
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; }
}

730

.
/// <value> <c>Y</c>
/// y .</value>
public int Y
{
get { return y; }
set { y = value; }
}
/// <summary> (0,0).
/// </summary>
public Point() : this(0, 0) { }
/// <summary>
/// (<paramref name="xor"/>,<paramref name="yor"/>).</summary>
/// <param><c>xor</c> x .</param>
/// <param><c>yor</c> y .</param>
public Point(int xor, int yor)
{
X = xor;
Y = yor;
}
/// <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;
}
/// <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> ,
/// .</summary>
/// <param><c>o</c> .
/// </param>
/// <returns>True
/// , false.</returns>
/// <seealso cref="operator=="/>
/// <seealso cref="operator!="/>
public override bool Equals(object o)

A.4.

731

{
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;
}
/// <summary> .</summary>
/// <returns>
/// (x,y), ,
/// .</returns>
public override string ToString()
{
return "(" + X + "," + Y + ")";
}
/// <summary> ,
/// </summary>
/// <param><c>p1</c> .</param>
/// <param><c>p2</c> .</param>
/// <returns>True
/// , 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;
}
/// <summary> ,
/// </summary>
/// <param><c>p1</c> .</param>
/// <param><c>p2</c> .</param>
/// <returns>True
/// , false.</returns>
/// <seealso cref="Equals"/>
/// <seealso cref="operator=="/>
public static bool operator !=(Point p1, Point p2)
{
return !(p1 == p2);
}

732

.
/// <summary> Point.
/// <para>
///
/// Point.</para></summary>
public static void Main()
{
//
}

}
}

A.4.2. XML
Point, :
<?xml version="1.0"?>
<doc>
<assembly>
<name>Point</name>
</assembly>
<members>
<member name="T:Graphics.Point">
<summary>
Class <c>Point</c> .
</summary>
</member>
<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>
(0,0).
</summary>
</member>
<member name="M:Graphics.Point.#ctor(System.Int32,System.Int32)">
<summary>

(<paramref name="xor"/>,<paramref name="yor"/>).
</summary>
<param>
<c>xor</c> x .
</param>
<param>
<c>yor</c> y .
</param>

A.4.
</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>

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>
,
.
</summary>
<param>
<c>o</c> .
</param>
<returns>
True,
, 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>
<member name="M:Graphics.Point.ToString">
<summary> .</summary>
<returns>

733

734

(x,y),
, .
</returns>
</member>
<member
name="M:Graphics.Point.op_Equality(Graphics.Point,Graphics.Point)">
<summary>
,
.
</summary>
<param>
<c>p1</c> .
</param>
<param>
<c>p2</c> .
</param>
<returns>
True,
, 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>
,
.
</summary>
<param>
<c>p1</c> .
</param>
<param>
<c>p2</c> .
</param>
<returns>
True,
, 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>

A.4.
<c>X</c>
x .
</value>
</member>
<member name="P:Graphics.Point.Y">
<value>
<c>Y</c>
y .
</value>
</member>
</members>
</doc>

735

736

, . ,
.

.1.
-:
-- opt
--:
---
--
---
---:
-
-opt
-
-:
-
-

-:
-

.1.1.
-:
(U+000D)
(U+000A)
(U+000D),
(U+000A)
(U+0085)
(U+2028)
(U+2029)

.1.2.
:
-
-

.1.
-:
//
-opt
-:
-
-

-:
Unicode, --
--:
(U+000D)
(U+000A)
(U+0085)
(U+2028)
(U+2029)
-:
/*
--opt
--:
--
--

--

--:
/
opt
---
:
*

---:
Unicode, / *

.1.3.
-:
Unicode Zs
(U+0009)
(U+000B)
(U+000C)

.1.4.
:

-
-
-
-
-
---

737

738

.1.5. Unicode
--unicode:
\u - - -
-
\U - - -
- - -
- -

.1.6.
:
-
@
---
-:
---, -
---:
--

-opt

--:
-
_
( U+005F)
-:
-
-

-:
-
-
-
-
--
-:
Unicode Lu, Ll, Lt, Lm, Lo Nl
--unicode, Lu, Ll,
Lt, Lm, Lo Nl
-:
Unicode Mn Mc
--unicode, Mn Mc
-:
Unicode Nd
--unicode, Nd
-:
Unicode Pc
--unicode, Pc
--:
Unicode Cf
--unicode, Cf

.1.

.1.7.
-:
abstract
as
byte
case
class
const
delegate
do
event
explicit
xed
oat
if
implicit
internal
is
new
null
override
params
readonly
ref
short
sizeof
struct
switch
try
typeof
unsafe
ushort
volatile
while

base
catch
continue
double
extern
for
in
lock
object
private
return
stackalloc
this
uint
using

bool
char
decimal
else
false
foreach
int
long
operator
protected
sbyte
static
throw
ulong
virtual

break
checked
default
enum
nally
goto
interface
namespace
out
public
sealed
string
true
unchecked
void

.1.8.
:
-
-
-
-
-
-
-:
true
false
-:
--
--
--:
-
--opt
-:
-
-

-:
0 1 2 3 4 5 6 7

--:
U u L l UL Ul uL ul

LU

Lu

lU

lu

--:
0x
-
--opt
0X
-
--opt

739

740

-:
-
-

-:
0 1 2 3 4 5 6 7 8 9 A B C D E F a b c d e f
-:
-
.
-
-opt
--opt
.

-opt
--opt
-
-
--opt
-
--
-:
-
e
opt
E
opt
-
:
+ --:
F f D d M m
-:
'

'
:
-
--
--
--unicode
-:
, ' (U+0027), \ (U+005C) --
--:
\' \" \\ \0 \a \b \f \n \r \t \v
--:
\x
-
-opt
-opt
-opt
-:
--
--
--:
"
---opt
---:
---
---

"

---

.1.
---:
----
--
--
--unicode
----:
, " (U+0022), \ (U+005C) --
--:
@"
---opt

"

---:
---
---
---
---:
----
--
----:
"
--:
""
-:
null

.1.9.
---:
{
}
[
]
(
)
+
*
/
%
&
=
<
>
?
?? ::
-> == != <= >= +=
&= |= ^= << <<= =>

.
|
++
-=

,
^
-*=

:
!
&&
/=

;
~
||
%=

-:
>|>
---:
>|>=

.1.10.
-:
-
-
-line
-
-region
-pragma

741

742

--:
--- true false
-:
-or
-opt
-opt
-or:
-and
-or

-opt

-and:
-
-and -opt

||

&&

-opt

-opt

-:
--
-
-opt
-
-
-opt
-

==

-and

-opt

!=

-opt

--:
--
!
-opt
--
--:
true
false
--
-
(
-opt
-:
#
-opt
-opt
-
--
-opt
#
-opt
-
--
--:
-opt

-opt

dene

-:
-if-
--elif-opt
-endif-
-if-:
#
-opt
-
-opt
-elif-:
-elif-
-elif-
-elif- :
-opt
#
-
-opt

-opt
--

undef

-opt

-else-opt

if

-elif-

-opt
elif
--

.1.
-else-:
#
-opt
-opt
--
-opt

else

-endif:
-opt
#
--

endif

-opt

743

-:
--
-
-:
--
-
--
--:
-opt
-
-:
-opt

-opt

-:
-, #
-:
#
-opt
-opt
-
-opt
#
-opt
-
-:
-
-
--opt
-region:
-

-opt

error
warning

-:
-opt
#
-opt
-

region

-:
-opt
#
-opt
-

endregion

-line:
-opt
-

line

-:
-
-
default
hidden

#
-opt
-

- -

744

-:
"
--
--:
--
--

"

--

--:
- "
-pragma:
-opt
#
-opt
-pragma
-

pragma

-pragma:
-pragma-warning
-pragma-warning:
warning
-
warning
-
-warning

-warning
-warning

-warning:
disable
restore
-warning:
-
-warning
-opt
-

-opt

.2.
.2.1.
--:
----
-:
----
----:

--opt
----
.
--

.2.2.
:
-
-
-

--opt

.2.
-:

:
-
-
-
-:
-
bool
-:
-
-
decimal
-:
sbyte
byte
short
ushort
int
uint
long
ulong
char
-:
oat
double
-:
--
--:

:
-
-:

:
-
object
dynamic
string
:
-

745

746

:
-

-:

-:
-
-

-:
[
-opt
-:
,
-

:
-
--:
<
-
-:
-
-

>

-:

-:

.2.3.
--:

.2.4.
-:

-
:
-opt

-:
:
-:

ref
--
out
--

.2.
-:
---
--
----:

-
--
--
-
---
this-
base-
--
--
--
--
---
-typeof
-checked
-unchecked
---
--
-:

--opt

--:
(

)
--:
-
.

--opt
-
.

--opt
--
.

--opt
-:
bool
byte
char
decimal
object
sbyte
short
string
-:
-

double
uint

-opt

---:
----
this-:
this
base-:
base
.
base
[

--:
-
++

oat
ulong

int
ushort

long

747

748

--:
-
---:
)
---opt
new

(
-opt
new

---
---:
-
-
-:
}
{
--opt
{
--
,
}
--:
-
--

-:

=
-
-:

---
-:
{
--
{
--

}
,

--:
-
--

-:
--
{
-
}
-:

--:
new
--
[
-
-opt
new
-
-
new

-
--:
new
-
(

---:
new
--
--:
{
- opt
}
{
--
,
}

opt

.2.
--:
-
--
-:
-
--
base-

-typeof:
typeof
(

)
typeof
(
--
typeof
(
void
)

--:

--opt

::

--opt
--
.

--opt
--:
<
opt
>
:
,

-checked:
checked
(
-unchecked:
unchecked
(

---:
default
(

)
-:
-
+
-
-
!
-
~
-
--
--
-
--:
++
-
--:
--
-:
(
)
-

749

750

-:
-
-
-
-

*
/
%

-
-
-

-:
-
-
-

-
-

-:
-
-
-
-:
-
-
-
-
-
-
-

<<
-
-
-

<
>
<=
>=
is
as

-:
-
-
-

-
-
-
-

==
!=

-
-

-:
-
-
&
-
--:
-
--

--:
--
--

--
--
--
&&
--:
--
--

||

--:
--
--
??
-:
--

--

--

--

--

=>

.2.
--:
delegate
---opt

751

--:
---
---
---
(
----opt
----:
---
----

---:
---opt

---

---:
ref
out
---:
(
----opt
---
----:
---
----

---

---:

--:

-:
-from
-from
from
opt

in

-:
--opt

-select--group

--:
--
--

--

--:
-from
-let
-where
-join
-join-into
-orderby

-opt

752

-let
let

-where:
where
-
-join:
join
opt

in

on

equals

-join-into

join
opt
into

in

on

equals

-orderby:
orderby

-opt

-:
ascending
descending
select--group:
-select
-group
-select:
select

-group:
group

-:
into

:
-

by

-:
=
+=
-=
*=
/=
%=
&=
|=
^=
<<=
---

.2.

--

--:
-
-
-
-:

-:

.2.5.
:
-
-
-
-:

-
-
-
-
-
-try
-checked
-unchecked
-lock
-using
-yield
:
{

opt

-:

-:
;
-:

-:
--
-

;
;

--:
--
--

753

754

-- :

var
--:
--
--

--

--:

--

--:

-
--:
onst

-
-:
-
-
-:

-:
-

-:
-
-

--
--
--
--
-:
-if
-switch
-if:
if
( -
if
( -
-switch:
switch
(
switch-:
{
switch-opt
switch-:
switch-
switch-

)
)

-
- else -

switch-

switch-

.2.
switch-:
switch-
switch-:
switch-
switch-

switch-

switch-:
case
-
default
:

-:
-while
-do
-for
-foreach
-while
while
(

-do:
do
-

while

-for
for ( -foropt
-

-foropt ;

-foropt

in

-for:
--
--
-for:
-
-for:
--
--
-
--

-foreach:
foreach ( --
-
-:
-break
-continue
-goto
-return
-throw
-break:
break
;

755

756

-continue:
continue
;
-goto:
goto

;
goto case -
goto default
;
-return:
return
opt

-throw:
throw
opt
-try:
try

try

try

catch-
nally-
catch-

nally-

catch-:
-catch-
-catch-opt
-catch-opt
-catch-
-catch-
-catch-
-catch-

-catch-

-catch-:
catch
(
-

opt

-catch-:
catch

nally-:
nally

-checked:
checked

-unchecked:
unchecked

-lock
lock
(
-using
using
(

-:
--

-yield:
yield
return
yield
break

.2.

757

.2.6.
-:
--opt
-usingopt
---opt
--:
namespace
-
-:

-opt

-- opt

; opt

--:
{
--opt
-usingopt
--- opt }
--:
--
--
--:
extern alias

-using:
-using
-using

--

-using

-using:
using--
using---
using--:
using

- ---

using---:
using
--;
---:
---
---

---

---:
--
-
-:
-
-
-
-
-
-:
::

--opt

758

.2.7.
-:
opt
-opt
partialopt
class
--opt
-opt
---op
-
-:
-
-

;opt

-:
new
public
protected
internal
private
abstract
sealed
static
--:
<
-

>

-:
opt
-
-
,
opt

-:

-:
:

:
-
:

,
-
-:

---:
---
---
---:
where
-
:

---

----

----:
-
-
-
-
,
-
-
,
-
-
,
-
-
,
-
,
-

.2.
-:

class
struct
-:

-
-
-

,
,

-:
new
(
)
-:
{
--opt
--:
--
--

--

--:
-
-
-
-
-
-
-
-
-
--
-
-:
-opt
opt
-
;
-:
-
-

const

-:
new
public
protected
internal
private
-:
-
-
-:

759

760

-:
opt

-opt

-:
-
-

- ;

-:
new
public
protected
internal
private
static
readonly
volatile
-:
-
-
-:

-:

-
-:
-

-:
opt
-opt
partialopt
--
-
--opt
(
--opt
)
---opt
-:
-
-
-:
new
public
protected
internal
private
static
virtual
sealed
override
abstract
extern

.2.
--:

void
-:

-:

;
--:
-
-
-
-:
-
-

-:
opt
-opt
--opt

--:
=

-:
ref
out
this
-:
opt

params

-:
opt
-opt
{
--
}
-:
-
-
-:
new
public
protected
internal
private
static
virtual
sealed
override
abstract
extern

761

762

-:

--:
--
--

--opt
--opt

--:
opt
--opt

get

--

--:
opt
--opt

set

--

--:
protected
internal
private
protected internal
internal protected
--:

;
-:
opt
-opt
event
-
;
opt
-opt
event
{
---
}
-:
-
-

-:
new
public
protected
internal
private
static
virtual
sealed
override
abstract
extern
---:
---add
---remove
---add:
opt
add

---remove
---add

.2.
---remove:
remove

opt
-:
-opt
opt
{
--
}
-:
-
-

-:
new
public
protected
internal
private
virtual
sealed
override
abstract
extern
-:

this
[

--
]
.
this
[
--

-:
-
opt
-
-:
-
-

-:
public
static
extern
-:
--
--
--
--:

operator
--
(

)
--:
+ - ! ~ ++ -- true false
--:

operator
--
(

,

763

764

--:
+
*
/
%
&
|
^
<<
-
==
!=
>
<
>=
<=
--:
implicit
operator

(
explicit
operator

)
)

-:

;
-:
opt
-opt
-
-:
-
-

-:
public
protected
internal
private
extern
-:

(
--opt
-opt
-:
:
base
(
-opt
:
this
(
-opt

)
)

-:

;
--:
opt
--
(
)
--

.2.
--:
externopt
static
static
externopt
--:

;
-:
opt
externopt

-:

.2.8.
-:
opt opt partialopt struct
--opt -opt
---opt -
;opt
-:
-
-

-:
new
public
protected
internal
private
-:
: -
-:
{ --opt
--:
--
--

--

--:
-
-
-
-
-
-
-
-
--
-

765

766

.2.9.
:
-

-:

-:
-
-

-:
[
-opt
-:
,
-

-:
{
--opt,
{
--
,
--:

--

}
}

-:

.2.10.
-:
opt
-opt partialopt interface

---opt
-opt
---opt
-
;opt
-:
-
-

-:
new
public
protected
internal
private
---:
<
--

>

767

.2.
--:
-opt
-
opt
-opt
-- ,
opt
-
-:
in
out
-:
:
--
-:
{
--opt
--:
--
--

--

--:
--
--
--
--
--:
newopt
--

opt
--
(
--opt
;
---opt
--:
newopt

opt

--:
opt
get
;
set
;
opt
get
;
opt
set
;
opt

;
;

opt
opt

--:
newopt
event
opt

set
get

--:
newopt

this
opt
{
--

--

[
}

--

.2.11.
-:
opt
opt
-:
: -

enum

-opt

- ;opt

768

-:
{ --opt
{ --opt
-:
-
-

}
,

-:
new
public
protected
internal
private
--:
--
--
:
opt

opt

--

.2.12.
-:
opt -opt
delegate
--

---opt
( --opt ) ---opt ;
-:
-
-

-:
new
public
protected
internal
private

.2.13.
-:
--
--:
--
-- --
--:
[
----
[
----

- ]
- ,

769

.2.
----
---
:
---:
assembly
module
:
-
-:
-
-

-:
[ ---opt
[ ---opt

-
-

]
, ]

---:
--
:
--:
eld
event
method
param
property
return
type
-:

-
:
-

-opt

-:
-
-:
)
(
--opt
(
-- ,
--
(
--
)
--:
-
--
-:
-opt

--

--:
-
--

770

-:

--

--:

.3.

-:

unsafe
-:

unsafe
-:

unsafe
-:

unsafe
-:

unsafe
-:

unsafe
-:

unsafe
-:

unsafe
-:

unsafe
-:

unsafe
-:

unsafe

.3.
-:
opt
externopt
unsafeopt
-
opt
unsafeopt
externopt
-

--:
externopt
unsafeopt
static
unsafeopt
externopt
static
externopt
static
unsafeopt
unsafeopt
static
externopt
static
externopt
unsafeopt
static
unsafeopt
externopt
-:

-unsafe
-unsafe:
unsafe

-
-:
-
void *

-:

----:

-----
----
-sizeof
-:

--
--
--:
*
-
-----:
-
->

----:
----
--:
&
-

--opt
[

771

772

-sizeof:
sizeof
(
-
-xed:
xed
(
-
-

--

--:
--
--

--

--:

=
--
--:
&
--

--:

---
---:
---opt
xed
opt
--
---
---:
---
---
---
---:
new
public
protected
internal
private
unsafe
--:

---:
---
--- ,
---
---:

[
-

--:

-stackalloc
-stackalloc:
stackalloc
-

773

IEEE. IEEE Standard for Binary Floating-Point Arithmetic. ANSI/IEEE Standard


754-1985. http://www.ieee.org.
ISO/IEC. C++. ANSI/ISO/IEC 14882:1998.
Unicode Consortium. The Unicode Standard, Version 3.0. Addison-Wesley, Reading.
Massachusetts, 2000. ISBN 0-201-616335-5.

. , . , . , .

C#. Computers Science


4-
.




.
.
.
.
.
.
.

, 198206, -, , 73, . 29.


005-93, 2; 95 3005 .
30.08.11. 70100/16. . . . 63,210. 1500. 0000.
CtP . . . .
197110, -, ., 15.

?
!
?
?
? ,

?
!


!

www.piter.com/ePartners

www.piter.com,
,

( www.piter.com)
!
.
10% ,
, - c
. ,
, 5%
.
, , 500 ,
. Web.Money.
:
http://www.piter.com/book.phtml?978538800282
http://www.piter.com/book.phtml?978538800282&refer=0000
, 0000


WWW.PITER.COM


:
: www.piter.com
: postbook@piter.com
: (812) 7037374
: 197198, , / 127,
ICQ: 413763617

:

.
.
,
.
. : . Web-money USD
E-Gold, MoneyMail, INOCard, RBK Money (RuPay), USD Bets, Mobile Wallet
.
, ,
.
.
.
e-mail.

:
, , , , , e1mail;
, , , , , ,
, ;
, , .

1997 ,
, .
, , .
:
;
;
10 15%;
1;
.
?
:
www.piter.com
1500 ;
;
, : postbook@piter.com;
( ).
:
10% 6
1500 2500 ,
;
2501 ,
15% .
:
: (812)703173174;
: postbook@piter.com;
: www.piter.com;
: 197198, 1, / 127 .
:
( ), , , , ,
, e1mail;
, , , , , , , ;
, , .


, ,
,

 . , . ., . 29
./: (812) 703173173, 703173172; e1mail: sales@piter.com
. , ., . 2/1, . 1, 61
./: (495) 234138-15, 974134-50; e-mail: sales@msk.piter.com

., . 169; ./: (4732) 39161170


e1mail: piterctr@omch.ru
. , . 11; ./: (343) 378198141, 378198142
1mail: office@ekat.piter.com

. , . 13; .: (8312) 41127131


e1mail: office@nnov.piter.com
. , . 36; .: (383) 363-01-14
: (383) 350119179; e1mail: sib@nsk.piter.com


. , . 26; .: (863) 269191122, 269191130


1mail: piter1ug@rostov.piter.com

. , . 33; 223; .: (846) 277189179


e-mail: pitvolga@samtel.ru

. , . 12, 10; .: (1038057) 751-10-02


758-41-45; : (1038057) 712-27-05; -mail: piter@kharkov.piter.com

., . 6, . 1, 33; .: (1038044) 490135169


: (1038044) 490135168; 1mail: office@kiev.piter.com

. , . 34, 2; ./: (1037517) 201-48-79, 201-48-81


1mail: gv@minsk.piter.com

, .
: (812) 7037373. Email: fuganov@piter.com
.
:  (812) 7037372, (495) 9743450
.: (812) 703173173.
1mail: kozin@piter.com
: www.piter.com; .: (812) 703-73-74
ICQ 413763617

!



.


./: (4232) 23182112
e1mail: bookbase@mail.primorye.ru

,
.: (3952) 20109117, 24117177
e1mail: prodalit@irk.ru
http://www.prodalit.irk.ru

, , . , . 1
.: (4212) 36106165, 33195131
e1mail: dkniga@mail.kht.ru

,
./: (3952) 25125190
e1mail: kkcbooks@bk.ru
http://www.kkcbooks.ru

,
.: (4212) 32185151, : (4212) 32182150
e1mail: postmaster@worldbooks.kht.ru
,
.: (4212) 39149160
e1mail: zakaz@booksmirs.ru

, , . , . 3
.: (8182) 65141134, 65138179
e1mail: marketing@avfkniga.ru

, , . , . 4
.: (4732) 26177177
http://www.amital.ru
, ,

./: (4012) 21156128, 6 5165168
e1mail: nshibkova@vester.ru
http://www.vester.ru
, ,
, .15
.: (846) 331122133
e1mail: chaconne@chaccone.ru
,
. , . 58
.: (4732) 51128193, 47100181
e1mail: manager@kmsvrn.ru

, , . , 424
./: (87934) 6193109
e1mail: rossy@kmw.ru

,
. , . 86
./: (3912) 27139171
e1mail: book1world@public.krasnet.ru
, 1
.: (383) 336110126
: (383) 336110127
e1mail: office@top1kniga.ru
http://www.top1kniga.ru

, ,

.: (843) 272134155
e1mail: tais@bancorp.ru

,
. , . 12
./: (343) 358118198, 358114184
e1mail: domknigi@k66.ru

,
. , . 1
./: (343) 228-10-70
e-mail: igm@lumna.ru
http://www.lumna.ru
,
. , . 124
.: (351) 247174103, 247174109,
247174116
e1mail: zakup@intser.ru
http://www.fkniga.ru, www.intser.ru

CLR via C#.


Microsoft .NET Framework 4.0
C#. 3-
.

ISBN: 978-5-459-00297-3
: 976 .
: 2011

, ,

(CLR) Microsoft .NET
Framework 4.0. ,

.NET Framework Microsoft,
-
, Microsoft Silverlight, ASP.NET, Windows
Presentation Foundation ..

.NET Framework 4.0 .


5-
. , .

ISBN: 978-5-459-00342-0
: 1040 .
: 2011

,
,


.
1980 ,

.
, .

,
.
, .

,
, , , 802.12 802.16,
3G, RFID,
CDN, ,
, -
.

.

. . , .

ISBN: 978-5-459-01099-2
: 592 .
: 2011

IT-

.
,
,

.



.

,
, .
,
, ,
27
.

Java.
Computers Science.
2-
.

ISBN: 978-5-459-00292-8
: 704 .
: 2011


.
, ,
.

,

.

Java,

, C++.

, -
.

,
: ,
,
, , , , ,
.

.