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

.


-
--

2009

#.

:

. .
. . . ,
, ;
. . -
,
,

32.973-018.1
004.43
. .

12

#. . .
.: , 2009. 432 : .
ISBN 978-5-91180-174-8
, #,
. # 2.0 (2005)
, ,
, , .
,
, ,
.
,
.
# .
, , ,

.

,
.

, 2009
.
.

ISBN 978-5-91180-174-8

, 198206, - , , . 73, 2 9
- 005-93, 2; 95 3005 -
04.09.08. 70x100/16. . . . 34,83. 2700. 1 1 0 2 0
Q P . . . .
1 9 7 1 1 0 , - , ., . 15.

1. .NET

11

Visual S t u d i o . N E T

^
'

13
14
21

2.

22

22
31
36

3. ,

38

38
41
42

59
67

4.

69

70

70

75

87

89

checked unchecked

95

95

5. :

100

103

104

this

106
114
114

120
124

6.

126

foreach

126
136

138
139

Random

148
150

7. :

152




Main

152
153
154
156
157
161
169
169
170

8.

172




object

172
178
181
182
183
186

9.

188



. is as

.NET

188
190
194
195
198
212
215
219

10. ,

220

220
232
237
245

1 1 .

246


-


-

()

250
253
255
260
262
263
267
270

'. . .

12. , , ,

272

272
275
278
279
283
285
287
290

13. , -

System.Collections
-


291
291
295
299
308
309
310

14. W i n d o w s

311

-
Windows-

312
314

Control


Form

Application

323
325
337
338
339
342
344
346

15. #

347



XML
,

347
355
365
366

369

370

1.
2 . . . . . . . . . .
3.
4.
5.
6.
7.
8.
9.
10.
1 1 .
12. W i n d o w s -

370
371
379
381
385
389
393
395
400
405
411
412

423

425

427


, # ,
.
# 2.0 ( 2 0 0 5 ) ,
, ,
, .

, , ,
.
,
.
#
. , ,
.
.
. N E T ,
,
. #
, B A S I C + + .
, # , ,
,
.
, # ,
-.
# : -,
1

, -,
, , ,
,
. N E T .
, Microsoft
# Express 2005,
(http://msdn.microsoft.com/vstuclio/express/visualCsharp/),

.

, # Express 2005 W i n d o w s /2000,


Pentium 600 128 .

- -.
Microsoft , ,
,
,
. ,
.
-. ,
+ + --,
!

# (http://
www.ecma-international.org/publications/standards/Ecma-334.htm)

, .
, 14
Windows, 15
- -. , ,
, ,
,
. N E T .
,
# , ,
, , - .

. , , ,
, ,
. -
http://ips.ifmo.ru ,
.
:
,
. , ,
,
.
, , ,
mux@tp2055.spb.edu
- .


,
comp@piter.com ( ) .
! -
www.piter.com.


.NET
, .
, , ,
. ,
, , ,
, , .
:

,
;

, ,
, ;

, ;

,
. . N E T ( )
, Visual Studio.NET,
, , ,
.
,
,
:

riepettocztjvtocTnb


; ;


.NET
, .
, , ,
. ,
, , ,
, , .
:

,
;

, ,
, ;

, ;

,
. . N E T ( )
, Visual Studio.NET,
, , ,
.
,
,
:


; ;

.NET

.
. N E T .
, ,
, (Microsoft Intermediate Language,
M S I L , I L ) , , ,
.
, ,
( C o m m o n Language Runtime, C L R ) .
C L R .
C L R JIT-,
IL ,
. J I T just in t i m e ,
, ,
.

. N E T . 1.1.
(. .dll)


(IL )

CLR


( )

1
. 1 . 1 . .NET

dll,
IL . ,
, .
,
,
.
,
.

10

1. .NET

.
,
, .
.

C L R ,
,
.
.
. N E T *,
. N E T .
. 1.2. .
,
: -, ,
, , .

.NET
Visual Studio.NET
.NET Framework
(FCL)
-

.NET Enterprise
Servers

XML

.NET Building
Block Services

(CLR)



. 1.2. .NET
,
X M L ( X M L ) .
,
- W i n d o w s - .
.
. N E T ,
.
1

. ,
.

11

C L R (framework), .

.


. , , Windows,
Windows- .

. N E T -
, # ,
-
( ) .

.

, , .
. ,
, , ,
.
:
, ,
, .
1

.
-
. ,
.
, .

.
. , , ,
, , .

. , .
.
, ,

,
, !.

12

1. .NET

.
.
( ) .
:
.
, !
, ,
.
, ,
.
:
.

, .
,

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

:

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

13

, .
,

.
,
8.
, .

, ;

,
;


. : ,
,
- .
,
.
, .

, .

# , Java, + + , Delphi . .
,
, , . .
,
, .
, ,
, .
1

, .
. N E T , ,
. N E T , object
.

, , - , , Windows.

14

1. .NET

:
. ,
.
, .NET, System.
, ,
, ,
-.
.
, System. Net ,
, System.Windows.Forms
, , . .
, .
, ,
# , Visual Studio.NET.

Visual Studio.NET
Visual Studio.NET
, , , ,
.NET- . Microsoft
: # , V B . N E T , + + J # .
. N E T . ,
.
. N E T , , Ada, C O B O L , Delphi,
Eiffel, Fortran, Lisp, Oberon, Perl Python.
Bee .NET-
( C o m m o n Language Specification, C L S ) ,
.

.
. N E T .
.
: , ,
. Visual Studio.NET ,
:

Windows-

W i n d o w s ,

, , .;

,
;

,
;

15

Visual Studio.NET

- ,
(, Internet Explorer)
;

- , .

(solution).
.


Visual Studio.NET W i n d o w s
W i n d o w s - -,
.
,
- .
, - .
,
,
.
,
# .
: # .
, Visual Studio.NET,
[ 8 ] , [ 1 6 ] .

, ,
# 7.1 ( .NET
Framework 1.1), . ,
, 2.0,
Visual # 2005 Express Edition ( .NET Framework 2.0).

.
Visual S t u d i o . N E T

File N e w Project....
Visual # Projects, Console
Application. Name , Location
, .

. . 1.3.
( File, Edit, View
. . ) (toolbars).
, (View Toolbars...), .
1

16

1. .NET

"3

. 1.3.
. Solution Ex
plorer ( , View Solution
Explorer ). , :
(System, System.Data, System.XML), (App.ico),
( C l a s s l . c s )

(Assemblylnfo.cs).

,
Class View, . Class View
, , .

,
. Windows ,
,
. ( csproj),
( sin) (Classl .cs).

Properties (
, View Properties ) .
,
.

17

Visual Studio.NET

. ,
, Classl,

FileName ( Enter).
,
, .
, .
()

-, .

:
, .
, , .

.


( 1.1).
, .
, .
1 . 1 .
using System;
namespace ConsoleAppli cati onl
{
/ / / <summary>
/ / / Summary description for Classl.
/ / / </summary>
class Classl
{
/ / / <summary>
/ / / The main entry point for the application.
/ / / </summary>
[STAThread]
static void Main(string[] args)
{
//
// TODO: Add code to start application here
//

}
}

}
, .
, .

1
.

18

1. .NET

using System
System ( ).
namespace ,
ConsoleAppl icationl. ,
, ,
.
, ,
.
# - ,
.
,
Classl. class,
(
, ).

.
,
. ,
, , .
, .

Main.
Main
. .

[ ] _ ( [ ] )
{

: ,


, ,
.
,
, ,
, -,
. :
[ void | int ] _():
, _
,
voi d, i nt, .
, ,
, . ,
, .

Visual Studio.NET

19

, , ,
, .
(. . 106).

- .
,
. .

Main :
// TODO: Add code to start application here
: , .
( !)

Console.WriteLine{"-! -! ( ) ");
Console System.
WriteLine . ,

._
,
Console ,
Console.
, ,
. Enter
.


. ,
.

, 1.2 (
,
).
, ?
,
* ,
, , , ,
. File Save
Save (
). ,
.

20

1. .NET

1.2. #
using System:
namespace ConsoleApplicationl

{
class Classl
{
static void MainO
{

Console.WriteLine( "-! -! ( ) " ):

}
. 1.4 Visual
# 2005 Express Edition. ,
, , 1.1
.

. 1.4.
Visual # 2005 Express Edition


F5 (
Debug Start). ,
-! -! ()
, . ,
,
Ctrl+FS 1 ( Debug Start Without Debugging).
1

Ctrl+F5 , , Ctrl, F5.

21


. ,
.
,
, .
Console.WriteLineL.) .
,
, , (There were build errors.
C o n t i n u e ? ) . No, .

,
, error.

e r r o r CS1002 ; e x p e c t e d
(, )
.
F1,
.
.
D y n a m i c Help,
, Properties (
) .
, .
,
( Visual # 2005 Express Edition
) .
, . N E T , ,
, # .



. N E T .
(
, [ 5 ] , [19], [26], [ 2 7 ] ) .
Visual Studio.NET
, , ,
, . #
,
, .

# ,



.
,
,
.
# .


. ,
( ) , . #
Unicode. , .
,
, ,
() .
, (character set).
. , W i n d o w s
A N S I , 1 2 5 1 .
(8 ) ,
256 .
, ,
. -

23

.
(, ), .
Unicode
, .
. ,
. 128 Unicode A N S I .
# :

( ) (_),
;

, +, *, { &;

( );

: ,
.
1

(token ) ,
. :

{);

().

.
, 128 ( 12), Vasia,
goto +. .
# + + .
,
. ,
(, )
.
# , + + .
(. . 287).

. .
.
. , + b
.
.

24

2.

IL
: .
, ,
( , ,
) .
. , .
,
. :
i n t :
.

, ,
, .
.

, .
, , sysop, SySoP SYS0P .
,
. .
.
#
. , gg
1

. ,
escape-

Unicode,

\, , \u00F2.

: 21 ate, Big g i g , #; ,
( # ) .

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

( .
2
) ;
,
. , , . 182.
,
@. , @i f.

25

,
.


, .

.
.
, ,
, , MaxLength, MyFuzzyShooshpanchik.
( ,
Microsoft) ,
, , i MaxLength, IpfnMyFuzzyShooshpanchik.
Camel, ,
, , , maxLength, myFuzzyShooshpanchik.
,
.
, ,
: max_length, my_fuzzy_shooshpanchik,
.
#
: Camel.
,
. , .
.


,
.
, . # . 2.1.


,
. . ,
+= b += , b .
, , , , &&, |
<, , as new.
,
. -
. , [ ], ( ) ? :,
.

26

2.

2 . 1 . #
abstract

as

base

boo I

break

byte

case

catch

char

checked

class

const

continue

decimal

default

delegate

do

double

else

enum

event

explicit

extern

false

finally

fixed

float

for

foreach

goto

if

implicit

in

int

interface
namespace

internal

is

lock

long

new

null

object

operator

out

override

params

private

protected

public
sealed

readonly

ref

return

sbyte

short

sizeof

stackalloc

static

string
true

struct

switch

this

throw

try

typeof

uint

ulong

unchecked

unsafe

ushort

using

virtual

void

volatile

while

C# (. . 42).
().
(. . 161).

, ,
. : , , .
, # :

{ } [ ] ( ) . .
<

>

&= | =

++

::
||

- * / * & |

. . > >

] = < = > = + =

! -=

=
*=

/=

%=

= = = ->

, , . #
, , , ,
null. , ,
.
1

.
. 2.2. ,
,
( ).
,
.

27

2 . 2 . #

true () false ()

true
false

:
(0, 1, 2, 3, 4, 5,
6, 7, 8, 9),
( U , u, L, 1, UL, Ul, uL, ul,
LU, Lu, 1U, lu)

8u

OLu

: Ox OX,

(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, , , ,
D, , F ) , , ,
( U , u, L, 1,
UL, Ul, uL, ul, LU, Lu, 1U, lu)

0x188

OxAU

0xlB8LU

:
[] [.] [] []
F, f, D,
d, , m

199226
199226L

0X00FF

0X00FF1

5.7

.001

5.7F

.OOld

35
35

5F

.OOlf

35m

0.2E6

.lle+3

[][.][]{|}[+|-][]
[]

0.2E6D

.lle-3

5E-10

5E10

F, f, D,
d, , m

'A'

'\0'

'\n'

'\xF'

'\x74'

'*'

'\uA81B'

" Vasia"
" = \0xF5 \n"
" \u0056\u0061"
": W t e m p W f i l e l . t x t "
@":\temp\filel.txt"

null

null

. 2.2 . .
-.
, , ,
,
.

28

2.

: .
.
( ) , .

2

10 (, 1.32 = 1,3 10 = 130).


, , , ,
.

.
, .
, , ,
10.


, ( - ) , :
-218, -022, -, -4.8, -0.14.
,
. -
, ,
, . 2.3.
, .
2 . 3 .

L, 1

(long)

U,

(unsigned)

F, f

( f l o a t )

D, d

(double)

, m

(decimal)


.
Unicode.
,
. 2.2:

, (
), ' ' , '', ' * ' ;

' \ 0 ' , ' \ ' ;

'VxF', '\74';

escape- Unicode '\uA81B'.

29

escape-,

, .

:

, (, \
);

,
, , '.

. 2.4 .
, ,
.
2 . 4 . #

\f

()

\t

\v

\\

\0

, ,

\0, .

0 2
Escape-

16

- 1, .

Unicode

Unicode
\ \U, , \u00F2, \U00010011. \U10000 \U10FFFF
; ,
\U10FFFF, .

, . ,
, ,
\:
" , \ "

30

2.

:

,

: ,
, ,
:
" \"\""
,
, #
(verbatim strings). @,

, . ,
:
@"
,
"
@" """

,
,
.
:
": WappWbi nWdebugWa. exe"
@":\app\bin\debug\a.exe"
( " " ) ,
.
nul 1 ,
, .


. .
. # :
.

( / / ) ,
- /* */ ,
. :
// /* .

31

Me , , ! ( / / / ) .
X M L .
,
.
15.

/
, , . , , ,
: .
.
:
, ,
;
( ) .
, ,
, ;
, , , .
. ,
, .
.
1, ,
1

: (stack) , (heap) . , ,
.
# .


, . , # ,
i . .
,
ie ) (
) .


. , , ,
, !

32
() (. 2.1).
,
.
,
.

. 2 . 1 . #


.
, ,
. . ,
,
, .
, ,
.
# . 2.5.
. N E T , System
, ?
. ,
,
.
, ,
.

. (
, 1 ).
.

,
, , /

, !
.

33
i. #

.NET

bool

Boolean

true, false

sbyte

SByte

byte

Byte

short

Intl6

ushort

UIntl6

-128 127

0 255

-32 768 32 767

16

0 65 535

16

int

Int32

-2 10 2 10

32

uint

UInt32

0 4 10

32

long

Int64

-9 10
18
9 10

64

ulong

UInt64

0 18 10

64

char

Char

U+0000
U+ffff

Unicode

16

float

Single

1.5 10"
38
3.4 10

32

double

Double

5.0 10
308
1.7 10

15-16

64

decimal

Decimal

1.0 10~
28
7.9 10

28-29

128

string

String

Unicode

object

Object

18

18

45

324

28

, 2"
1 . , 1 1
1 [-2"" ,2 ~ - 1], ,

1 [0, 2 - 1] (. . 2.5).

, ,
, .
, . , .
4

: , 0,381 10

34

2.

381 4, 560,3 10

5603 5 ( ), 0,012 12
,
,
. double,
. i
.

, ,
f 1 oat i nt,
- . 1 ong doub

decimal ,
. . 2.5, f l o a t }
7 , double 15-16.
, ;
,
! decimal 28-29
decimal ,
.
.
.
# .N1
System. , ]
, . , :
# . ,
, , <j
:

double.MaxValue ( System.Doubl.MaxValue) dout

uint.MinValue ( System.UInt32.MinValue) u

, ,
,
P o s i t i v e l n f i n i t y , Negativelnfinity NaN . ]
, . ;
, c
.


, , , .
( ) .
i nt,

35

nt, uint, long ulong,


.
double.
1, 10 i nt (
[ ), 2147483648 ui nt.
, , 1. If, 1UL, 1000m ( . 2.3).
: , .

-
1 # -
1

[ (. 2.2) . - ,
value types), ,
[ . ,
.
, (,
[). .
(
, -,
object.

. 2 . 2 . #
2.3
. -.
. ,
. ,
,
.

36

2.

( b , b
). ,
, .

. 2 . 3 .
, .

, . 9.
5 ,
# , ,
-. nullable # 2.0
13.



,
. # . -
( b o x i n g ) ,

(unboxing).

,
,
: ,
,
.
,
.


, ,
. ,
( !), .

37

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

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

, . ,
,
# ,
.

,

, # ,
-
nporpaj
.

, <
.
. , ,
. ]
.

int ;

float ;

,
. .
# ,

. , i
- , !
quantity , , kolich, , , A, tl7_xz prikol.

, , !
, 1 ( L) I ( i ) .

39

, . , ,
, ,
.
I ,
! , :
a, b = 1;
it = 0.1. = 0 . I f :
:
i nt, ;
b i nt, 1;
f 1 oat, 1 0.1. , double ( ,
),
'loat; 0.1 .
,
, , :
b = 1, = 100:
= b * + 25;
,
, , ,
.
1
.

, ,
. 1,
Z# , . , , .

, 0 .
, , . .
1

.

40

3. ,

, , ,
, <
, .

.
# - : ,
.
.
, , i
,
, :
class X

// X

{
int ;

// X

int ;

// X

void Y ( )

//

{
int ;

// , - Y

int ;

// ( )

// ============ 1 ============
int D;
int :

//
//


= :
// X ( * * )
= this.: // X ( * * * )
// ============ 1 ===============

// ============ 2 ============
int D;

}
}
}

// D, -
! ,

// D, -
// ============ 2 ===============
//

// X

. X, TJ
: , Y. Y .
.
,
,
this ( , **

, X,
>
( ).
,
(. , }

41

* * ) . D ,
, , .

,
, ,
, .

3.1 ,
.
3 . 1 .
using System;
namespace ConsoleAppl i c a t i o n l
{

class Classl
{

static void MainO


{
int

i = 3;

double

= 4.12;

decimal d = 600m;
string

s = "";

Console.Write( "i = " )


Console.WriteC "y = " )
Console.Write( "d = " )
Console.Write(

"s - " )

Consol . Writel_ine(
Consol.WriteLine(
Console.Writel_ine(
Console.Writel_ine(

d
s

)
)
)
)

}
}
}
, Write , WriteLine,
.
- ( . . 5 9 ) .

( )
. ,
. .


,
const, :
const int b = 1;
const float x = 0:1, = 0 . I f :

// const

42

3. , !-

. ,
.
,
.


. 4 n c e j
0 1, ]
, .


. ,
, ,
const int b = 1. = 100:
const int = b * + 25:


. one
, .
, .
, + 2 , + , 2 . ,
, .
, . #
1

. 3.1 .
3 . 1 . #

. 105
. 108, 221

()

[]

++

--

. 47

new

. 48

typeof

. 280

checked

. 46

unchecked

. 46

. 127
. 47

43


( )

. 48

. 48

. 48

++

. 47

--

. 47

()
*

. 49

. 50

. 50

. 50

^
)

(
)

. 53

. 54

. 54

<

. 54

>

. 54

<=

. 54

>=

. 54

is

. 194

as

. 194

. 54

1=

. 54

&

( )

. 55

. 55

( )

. 55

1
&&

. 56
. 56

II
? ;

. 56

. 56

*=

/=

1-

+=

&

44

3. ,

3.1

()

&

<

# 2.0 ??, :
13 (. , . 309).

]
, . . 3.1 ;
, ?
.
.

, b :
int = 2, b = 5;
+ b 7 i nt, = b
, ( 5 ) , ,
.

,
, . on
,
.
, + b + ( + ) + , = b = = ( = ) .
b = ,
.


, F ( i ) + G(i++) * H ( i ) F
, .
,
, i (
1).

, !
.

45

1
-
[
. , ,
, .
/ , ,
. ()
, .
; , ().
. .

, i nt, .
, sbyte,
byte, short ushort, i nt.
, int.

[ . 3.1. , , , . ,
. , , . , .

double

[
long

float

decimal ]
ulong

. 3 . 1 .

46

3. ,

, i nt, .
, float double decimal
.

int, ui nt long float long double


, .
.


, ,
, . # ,

. :

().

,
, ()
. .
# -, ,
Excepti on, System.
, ()
, DivideByZeroException,
OutOfMemoryException, OverflowException.

,
Exception.


, catch (),
.
, ,
.
, ,
.
, ,
. checked unchecked. checked
, unchecked .
, , ,
.
, :
= checked (b + ) ;
unchecked {
= b + ;

//
/ /

47

, .
, ,
, .
,
.

/checked, .
,
.

( . . 8 9 ) .

#
# ,
,
.


( + + ) ( - - ) ,
, ,
, .
,
,
, . 3.2
.
3 . 2 .
using System;
namespace ConsoleApplicationl
{
class Classl
{

static void MainO


{
int x = 3. = 3;
Console.Write( " : " );
Consol.WriteLineC ++ ) ;
Console.Write( " : " );
Console.WriteLineC ) ;
Console.WriteC " : " );
Console.WriteLineC ++ ) ;
Console.WriteC " : " );
Console.WriteLineC ) ;

48

3. ,

: 4
: 4
: 3
: 4

, ,
, .
, (
, 5 7 ) .

new
new . :
new ( [ ] )
,
, :
object z = new o b j e c t O :
int i = new i n t ( ) :

II , int i = 0;

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

.
,
OutOfMemoryException.


( -)

. int, long,
float, double decimal. ,
( . . 3.1).
.


. .
, .

49

( ! )

bool.

false, true, true,


false.
( ~ ) , ,
int, uint, long ulong.
3.3.
3 . 3 .
BSing System;
namespace ConsoleApplicationl
{

class Ciassl
{

static void MainO


sbyte a = 3, b = -63, = 126;
bool

d = true;

Console.WriteLineC -a )

// -3

Console.WriteLineC -c )

//

-126

Console.WriteLineC !d )

//

false

Console.WriteLineC ~a )

// -4

Console.WriteLineC ~b )

// 62

Console.WriteLineC ~c )

//

-127


, ,
. ,
.
,
1

.
:

( )
, ,
, :
long b = 300;
int

= ( i n t ) b;

byte d = (byte) a;

//
//

, .

50

3. ,

(*)

int, ui nt, long, ulong,


float, double decimal. ,
( . . 3.1).
, int.
decimal
,
1

System. OverflowExcepti on .
. 3.2.
, z
.
,
2

, , 0. NaN ( n o t a number)
, .
3 . 2 .
*

+0

-0

+00

00

NaN

+Z

-z

+0

-0

+00

-oo

NaN

-z

+Z

-0

+0

-oo

+oo

NaN

+0

+0

-0

+0

-0

NaN

NaN

NaN

-0

-0

+0

-0

+0

NaN

NaN

NaN

+00

+00

-00

NaN

NaN

+00

-00

NaN

00

00

NaN

NaN

-00

+oo

NaN

NaN

NaN

NaN

NaN

NaN

NaN

NaN

NaN

(I) .
int, uint, long, ulong, float,
double decimal. ,
.
( . . 3.1), int.
,
. ,
System. Di vi deByZeroExcept i on.
,
, . 3.3.
, z
1

. ,
.
. 34.

51

.
,
, , 0.
3 . 3 .
/

+0

-0
-00

+00

00

NaN

+Z

-z

+0

-0

NaN

-z

+Z

-00

+oo

-0

+0

NaN

+0

+0

-0

NaN

NaN

+0

-0

NaN

-0

-0

+0

NaN

NaN

-0

+0

NaN

+00

+00

+oo

00

NaN

NaN

NaN

00

+00

oo

+oo

NaN

NaN

NaN

NaN

NaN

NaN

NaN

NaN

NaN

NaN

NaN

( decimal) 0
, 0.
(%) -
, . ,
- ( / ) * .
, System.DivideByZeroException.
, int ( . . 3.1).
,
- * , ,
.
. 3.4. ,
z .
3 . 4 .
/
/

+0

-0

+00

00

NaN

+Z

NaN

NaN

NaN

- X

-z

-z

NaN

NaN

-x

-x

NaN

+0

+0

+0

NaN

NaN

+0

+0

NaN

-0

-0

-0

NaN

NaN

-0

-0

NaN

+00

NaN

NaN

NaN

NaN

NaN

NaN

NaN

-00

NaN

NaN

NaN

NaN

NaN

NaN

NaN

NaN

NaN

NaN

NaN

NaN

NaN

NaN

NaN

( decimal)
,
0. .

3. ,

52

,
3.4.
3 . 4 . ,
using System;
namespace Consol eApplicationl
{ class Classl
{ s t a t i c void MainO
{
int x = 11, = 4;
float z = 4;
Console.WriteLineC z * ) ;

// 16

Console.WriteLineC z * le308 );

// "

Console.WriteLineC x / ) ;
Console. WriteLineC x / z ) ;

// 2

Console.WriteLineC x I ) :

// 3

Console.WriteLineC le-324 / le-324 );

// NaN

// 2.75

}
,
. 2 /
, 2
, .

2
-
, ,
, ,
2 / ( ) 2 / / .

( + ) .

one

int, ui nt, long, ulong, float, double decimal


,
( . . 3.1). ]
, i nt.
decimal
,
1

System.OverflowException .
1

. ,
.

53

. 3.5.
, z
.
,
, , 0.
3 . 5 .

+0

-0

+00

-oo

NaN

+00

-00

NaN

+0

+0

+0

+00

-00

NaN

-0

+0

-0

+00

-oo

NaN

+00

+00

+00

+00

+00

NaN

NaN

00

00

00

NaN

-00

NaN

NaN

NaN

NaN

NaN

NaN

NaN

NaN

( - ) .
int, ui nt, long, ulong, float, double decimal.
,
( . . 3.1).
, i nt.
decimal
,
System.OverflowException.

. 3.6.
, z .
, .
,
, - , ,
0 , - .
3 . 6 .

+0

-0

+00

00

NaN

00

+00

NaN

+0

+0

+0

-00

+00

NaN

-0

-0

+0

-00

+00

NaN

+00

+00

+00

+00

NaN

+oo

NaN

00

00

00

00

00

NaN

NaN

NaN

NaN

NaN

NaN

NaN

NaN

NaN

54

3. ,


( ) .

1

, .
( ) .
( ) ,
( ),
( ).
.
int, ui nt, long ulong.
3.5.
3 . 5 .
using System;
namespace ConsoleApplicationl
{
class Classl
{
static void MainO
byte = 3, b = 9;
sbyte = 9, d = -9;
Console.WriteLineC a
Console.WriteLineC a
Console.WriteLineC b
Console.WriteLineC
Console.WriteLineC d

1
2
1
1
1

)
)
)
)
)

//
//
//
//
//

6
12
4
4
-5


( < , <=, >, >=, ==, ! = )
.
, true false. . 3.7
3 . 7 .

==

true, , fal se

!=

true, , fal se

<

true, , fal se

>

true, , fal se

<=

true, , fal se

>=

true, , fal se

, 5 ,
int ui nt, 6 , long ulong.

55

,
, .


. , NaN,
, ! = , false ( != true).
, ! =
! ( == ) , NaN,
<, >, <= >= . ,
NaN, < false, ! ( >= ) true.
:

+0 -0 ;

- - ;

+ + .


(&,

|,

.
(
, . . ) .
int, ui nt, long ulong.
, ( &),
1 , 1.
, ( | ) ,
1 ,
1.
(

1 , 1.
3.6.
3 . 6 .
using System:
namespace
{

ConsoleApplicationl

class Classl
{

static void MainO


Console.WriteLineC 6 & 5)
Console.WriteLineC 6 | 5 )
Console.WriteLineC 6

5 )

// 4
// 7
// 3

56

3. , !


(&&) (11) one
. true
false. .
true, ;
true. true
true.

,
, . , \
false, false
, .

3.7.
3 . 7 .
using System;
namespace ConsoleApplicationl
{
class Classl
{

static void MainO


{

Console
Console
Console
Console

WriteLineC
WriteLineC
WriteLineC
WriteLineC

true & &


true & &
1
true 1
true J 1

true
false
true
false

);
);

);
);

//
//
//
//

true
false
true
true

}
}
}


(? :) , .

_1 ? _2 :
,
. true, TI
,
. , .
.
:

, ;
, 2
3, , ;

57

, 3
2, , 2;

if (
) .
3.8.
3 . 8 .
using System;
namespace ConsoleApplicationl
{
class Classl
{

s t a t i c void MainO
{
int a = 11. b = 4;
int max = b > a ? b : a;
Console.WriteLineC max );

// 11

: ,
1, ,
1. :
i = ( i < n) ? i + 1: 1;
, .
, ? b : ? d : a T b ^ c d ^ ) .
1

,
2

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

=
:
,
, . ,
, , . :
<-
, .
1

, .
.
, , .

58

3. , ^

=
=
=
=

b + / 2:
:
:
1;

= + 0.5:
: = = !

, , .
.

, {
. , >
.
( = + 0.5). ,
, . 0.5, ./:
, ,
, .
.

. ,
,
, :

:= ;
]
- ( . . 4 5 ) .
, ]
. .
( +=, * = , /= . . )
, , . !
, j
, += b 6oi
= + .
,
.

,
i nt. , , , b
byte, += b ]
: += (byte). .

59

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


,
, .
.
: , .
, ,
# .
11, .

-

.
, , .
,
11.
# , , .
.
# Console,
System. Write WriteLine
. ,
3.1. 3.9.
3 . 9 .
using System:
namespace ConsoleApplicationl
{
class Classl
{

s t a t i c void MainO
{

int
double
decimal
string

d
s

=
=
=
=

3;
4.12:
600m:
"";

Console WriteLineC "i - " + i ) ;


Console WriteLineC "y = { 0 } \nd - { 1 } " . y. d ) ;
Console WriteLineC "s = " + s ) :

1
2
// 3
//
//

60

3. , :

:
1 = 3
= 4,12
d = 600
s =
WriteLine
. ,
Console Write WriteLine
.
,
. , ,
. Consol
, ,
3.9 )
.
1 3.
. WriteLine ,
, , , . )
, :
, ,
+.

, .
#
ToStringO. ,
:
Console.WriteLineC "i = " + i.ToStringO );
2 .
WriteLine, .
, :
, ,
, ( 2)
,
:
( ) , !
( d) . .


(. . 146]


( \ ) ( \ t ) .

61

. Console
, ,
.
:
1. , ,
.
2. .
Convert,
System, Parse,
. 3.10
.
3 . 1 0 .
using System;
namespace ConsoleApplicationl
class Classl
{
static void MainO
Console.WriteLineC "
string s = Console.ReadLineO:
Console.WriteLineC "s = " + s );

);

// 1

Console.WriteLineC " " );


char = (char)Console.ReadO;
// 2
Console.ReadLineC);
// 3
Console.WriteLineC "c = " + );
string buf;
// -
Console.WriteLineC " " );
buf = Consol.ReadLineC);
int i = Convert.ToInt32( buf );
1/4
Console.WriteLineC i ) ;
Console.WriteLineC " " );
buf = Console.ReadLineO:
double x = Convert.ToDouble buf );
// 5
Console.WriteLineC x ) ;
Console.WriteLineC " " );
buf = Console.ReadLineO;
double - double.ParseC buf );
//
//
Console.WriteLineC ) ;
Console.WriteLineC " " );
buf - Console.ReadLineO;
decimal z = decimal.ParseC buf );
// 7
Console.WriteLineC z ) ;

}
}

62

3. ,

.
1. ,
.
Read,
( 2 ) . int,
, - 1 ,
(, Enter).
int, a char, int char ,
,
( . . 4 9 ) .
2 3,
. ,
. ,
, .
Enter . Read,
ReadLine, ,
, .
4 5 Convert, 6 7
Parse Double Decimal . N E T ,
# double decimal.


, . ,
, . ,
, 1,95-8.

, .

-

, .
2

.
.
, ,
, ,
.
1

, .
,
.
!

63

,
.
11,
. 3.11
3.9, ,
output.txt. ,
, ...\ConsoleApplication1\bin\Debug.
3 . 1 1 .
using System;
// 1

using System.10:
namespace ConsoleApplicationl
{

class Classl
{

static void MainO


{
StreamWriter f = new StreamWriterC
int

i = 3;

double

= 4.12;

"output.txt"

// 2

decimal d = 600m;
string

s = "";

f.WriteLineC "i = " + i );

//3

f.WriteLineC "y = { 0 } \nd = { 1 } " . y, d );

//4

f.WriteLineC "s = " + s ):

lib

f.CloseO;

// 6

}
}
}
, :
1. ,
( 1 ) .
2. ( 2 ) .
3. - ( 3 - 5 ) .
4. ( 6 ) .


, . .

. 3.12
, 3.10,
input.txt, D:\C#. ,
.

64

3. ,

,
Visual Studio.NET.
File New File... Text File.
3 . 1 2 .
using System;
using System.10;
namespace ConsoleApplicationl
{

class Classl
{

s t a t i c void MainO
{
StreamReader f = new StreamReader(

"d:\\C#\\input.txt"

);

string s = f.ReadLineC);
Console.WriteLineC "s = " + s );
char

= (char)f.ReadO;

f.ReadLineC);
Console.WriteLineC "c = " + );
string buf;
buf = f.ReadLineC);
int i = Convert.ToInt32( buf );
Console.WriteLineC i ) ;
buf = f.ReadLineC);
double x = Convert.ToDoubleC buf );
Console.WriteLineC x ) ;
buf = f.ReadLineO;
double = double.ParseC buf );
Console.WriteLineC ) ;
buf = f.ReadLineO;
decimal z = decimal.ParseC buf );
Console.WriteLineC z ) ;
f.CloseC);

Math
,
. Math,
System. :

: Sin, Cos, Tan;

: ASin, ACos, ATan, ATan2;

65

: Tanh, Sinh, Cosh;

: Exp, Log, LoglO;

( ) , , : Abs, Sqrt, Sign;

: Ceiling, Floor, Round;

, : Mi n, Max;

, : Pow, IEEEReminder;

: BigMul;

: Di vRem.

, : .
. 3.8.
3 . 8 . Math

Abs

Acos

Asin

Atan

|x| Abs(x)

double

Acos(double x)

double

Asin(double x)

double

Atan(double x)

Atan2

double

Atan2(double x, double )
,

BigMul

long

BigMul ( i n t , int )

Ceiling

double

Ceiling(double )

Cos

double

Cos(double )

Cosh

double

CoshCdouble )

DivRem

DivRem(x, , rem)

E.


( )

double

2,71828182845905

Exp

double

ex ()

Floor

double

Floor(double )

IEEERemainder

double

IEEERemainder(double ,
double )

Log

double

\ogex
Log(x)

, .
.

&

3. ,

66

3.13 Math.
.
3 . 1 3 . Math
u s i n g System;
namespace C o n s o l e A p p l i c a t i o n l
{

class Classl
{

s t a t i c void MainO
{
Console.Write(

" x:

"

);

s t r i n g buf = Console.ReadLine();
d o u b l e x = d o u b l e . P a r s e ( b u f );
Console.WriteLine(
Console.Write(

" s i n ;

" ;

"

" + Math.Sin(x)

);

:" + Math.Max(x, y)

);

);

buf = Console.ReadLine();
double =

double.Parse

Console.WriteLine(

b u f );

"

}
}
}

2

= Vtv*-' "

+ 2 t g 2 a + l,610

log10x .

67
, 1 . >i, d o u b l e . 3.14.
1 3 . 1 4 .

i n g System:
mespace C o n s o l e A p p l i c a t i o n l
class
{

Classl

s t a t i c void Main()
(
s t r i n g buf:
Console.WriteLineC

" x"

):

buf = Console.ReadLineO:
d o u b l e x = C o n v e r t . T o D o u b l e ( b u f ):
C o n s o l e . W r i t e L i n e C " a i f a "
buf = Console.ReadLineO:
d o u b l e a = d o u b l e . P a r s e C b u f );

);

double = Math.Sqrt( Math.PI * x ) Math.ExpC 0 . 2 * M a t h . S q r t ( a )

) +

2 * Math.TanC 2 * a ) +
1.6e3 * M a t h . L o g ] 0 i ' Math.PowCx.

2)

);

Console.WriteLineC " x { 0 } a!fa - { ! ) " , x. a };


C o n s o l e . W r i t e L i n e C " = " + );

}
}
1, ,
: # , . , . .


1 , ,
.
.
, .
.
, , .
1 . ,
1 , .

68
: ,
. (
, ).
,
, - i .
, ,
, ,
t r u e f a l s e . , b o o l f l a g ,
bool empty.

# ,
, Camel (.
, . 24).

,
. ,

, .
.
.
, c o n s t .
, ,

. , 0 1.
,
.
( , ).
,
.
.
.
,
,
.
.
, .
,
, ,
.

,
. #
.
# ,
.


,
.
, 60-70- ,
,
,
.

,
.
# - ,

, .
.

.
.
,
,
. . , ,
, .

4.

, ,
, .
: ( ,
, ). :
1-;

//'

* b + ;

//

f u ! ) ( i.

//

k ):

w h i l e ( t r u e ):

/ / ()

, , ,
.
, ,
. .


if switch

. if
, switch
.

if
if
. . 4.1.

. 4 . 1 .
:

if ( ) _1; [ else ; ]

71
" . t r j t , , - .
, . e l s e .
1
, ,

7. - , , ,
. ,
( ).
, , , .
:
< 0 ) b = 1:
< b && ( > d 11 == 0 ) ) b++: e l s e { b *= : = 0:

,7 1
}

// 2

< b ) if ( < ) m = a: e l s e = :
if ( b < ) m = b: e l s e m = :
-

b > a ) max = b: e l s e max - a:

/.' 3
// 4

} 1 e l s e .
, , .
, . , 2
, < b
. , ,
. .
3 .
, e l s e iONiy

1 f .

\, 4 (
), )
, :
; . = b > a ? b : a ;


. , 0 < < 1,
,
. : i f ( 0 < X && < 1 ) . . .

3 ,
. 4.2.

72

4.

. 4 . 2 .
, , .
,
,
. 4.1.
4 . 1 .
using System;
namespace
{

ConsoleApplicationl

class Classl
{

static void MainO


Console.WriteLineC " x" );
string buf = Console.ReadLineO;
double x = Convert.ToDoubleC buf );
Console.WriteLineC " " );
buf = Console.ReadLineO:
double = double.ParseC buf );
int kol = 0:
if

( x * x + y * y < l ) k o l = 2 ;

else if ( x * x + * < 4 ) kol = 1;


Console.WriteLineC " = { 0 } ", kol );


,
, if:

73
-f ( temp < 1 ) k o l = 2;
T o n s o l e . W r i t e L i n e C - " = { 0 } " ,

kol

);

, i f

,
, .

.
:
-""oat . ; ...
* == b ) ...
-f
( Math.AbsCa - b) < i e - 6 ) ...

// !
// !

, ,

. S i n g l e Double
E p s i l o n ( ,
1.0 + E p s i l o n

!= 1 . 0 ) .

switch
s w i t c h ( )
.
. 4.3.
switch

case 1
case 2

case
default

1
2

>

. 4 . 3 . switch
:

switch ){
case __1: [ __1 ]
case __2: [ __2 ]
case : [ ]
[ default: ]

74

4. |

.
1

( c h a r ) .
,
, .
]
. , ,
d e f a u l t ( ?
s w i t c h

).

, b r e a k , g o t o r e t u r n :

break
s w i t c h , f o r , w h i l e do (. break, . 8 4 ) ;

g o t o ,
case s w i t c h ( .
g o t o , . 8 3 ) ;

r e t u r n , (
return, . 8 7 ) .

g o t o
,
, .
4.2 ,
.
4 . 2 .
u s i n g System;
namespace C o n s o l e A p p l i c a t i o n !
:
class Classl
{

s t a t i c void Main()
i
s t r i n g buf;
d o u b l e . b.

res,

Corisole. Writpi j n e '

" : "

):

buf" = C o n s o l e R e a d L i n e C ) :
a = double. Parse

Console.WriieLineC
c h a r op -

b u f i;
"

"

);

(char)Console.ReadO:

Console.ReaciLmec);
C o n s o l e . W r i t e L i n e C " :"
buf = Console.ReadLineO;
b = d o u b l e . P a r s e C b u f );

).

,
, .

75

bool ok = t r u e :
s w i t c h (op)
I
i
1
case ' +
: r e s = a + b; b r e a k :
1
case ' : r e s = a - b: b r e a k ;
case ' * ' : r e s - a * b: b r e a k :
case ' / ' : r e s = a / b: b r e a k :
default
: r e s = d o u b l e . N a N : ok = f a l s e : b r e a k :
i
if (ok) Console.WriteLineC ". " + res ):
else
Console W r i t e L i n e C " "

d e f a u l t ,
, .
.

switch if ,

, ,
. ,
.


, .
# : whi 1,
repeat, for foreach.
.
, , .

: ,
(, 4.4). .
,
, .

^ ( ,
: . 4.4, ), ( , . 4.4, 6).
.
, , .
,
, .

76

4.

. 4 . 4 .
,
,
, , .
, .
. .
,
1

.
, .
, .
break, continue, return goto ( .
, . 8 3 ) .
( ) .

while
:

while ( )
. ,
.
true, ( ) .
, fa 1 se.
.

,
.

77

.
false, .

,
.

, ,
,
:
't,
=

tx, 0
2,

<0 "
< 10
> 10

, ,
dX t. .
, :
.
:
1. .
2. , ,
.
3. .
4. .
5. , 2-4, .
2-4 ,
. 4.3.
.
.
. , ,
, .
4 . 3 . , while
_5"ng System;
-;~iespace Consol eApplicationl
class Classl
{

static void MainO


{
double Xn = -2, Xk = 12, dX = 2, t = 2. y;
Console.WriteLineC "|
x
|

|" ); //
double x = Xn;
while ( x <= Xk )
{

= t;

// 1
l i b
1/2

78

4. /

4,3

()
f

i ( X >= X < 10 ) = t * ;
i f ( >= 10 )
= 2 * t;
C o n s o l e . W r i t e L i n e C "| { 0 . 6 } j { 1 . 6
{1.6}
+= dX:

I",

x. ):

//
11
//'
//

2
2
3
4

, (
( 6
) . .
.

, ,
. ,
4. t c k j

,
. ,
Ctrl+Break,
:

w h i l e ( t r u e ) ;
- '
. 4.4 ]
.
4 . 4 .
u s i n g System;
namespace C o n s o l 1 i c a t i o n l
{
class Class!
{
s t a t i c void MainO
string

buf:

while ( true )

{
C o n s o l e . W r i t e L i n e C "1 - _ 1 . 2 - __2. 3 - " ):
buf = Console.ReadLineO:
switch buf )
// 1
case " 1 " :
C o n s o l e . W r i t e L i n e C " - _ 1 " ):

79

break:
case "2

//
C o n s o l e . W r i t e L i n ene(
C
"oi;idaxa
break;

return;

case " 3 "


default

n y i K i a
- _ 2 " ),

: Console.WriteLineC
break;

" !"

do
, . 4.4, ,

do while ;
, ,
( b o o l ) .
, .
, f a l s e
- .
,
, ,
. ,
.
, , 4.5.
4 . 5 .
. s i n g System:
-amespace C o n s o l I i c a t i o n i
class Classl
{
s t a t i c void MainO
char
do

answer:

C o n s o l e . W r i t e L i n e C " ,
answer = ( c h a r ) Console.Read ;
Console.ReadLineO;
w h l i e ( answer != ' );

a?"

):

80

4.

,
c o s ( x ) =
0,0001.
, ,
1

. .
. ,
(,
).
. , ,
, , .
,
.
( 4.6)
, . ,
, ,
( )
2

, .
4 . 6 .
using System;
namespace
{

ConsoleApplicationi

class Classl
{

static void MainO


double x, l e f t = 0, right = 1;
do
x = ( l e f t + right ) / 2;
if ( ( Math.Cos(x) - x ) * ( Math.Cos(left) - l e f t ) < 0 )
right = x;
else l e f t = x;
} while ( Math.Abs( right - l e f t ) < le-4 );
Console.WriteLineC " " + x );

81

for
:

for ( ; ; ) ;
, ,
.
, , :
- ( i n t i - 0. j 20: . . .
nt k. m;
"" ( k = 1 , m = 0 : . . .
, ,
. .
b o o l :
t r u e , .
.

.
, :
-~: ( i n t i = 0 , j = 2 0 ; i < 5 && j > 1 0 ; i + + , j - - ) ...
.
f o r (
!).
1 100:
" t 5 - 0:
- V ( i n t i - 1 , i < - 100; i + + ) s += i :
4.7 ,
4.3.
4 . 7 . , for
. 5 1 n g System:
-amespace
class
{

ConsoleApplicationl
Classl

s t a t i c void MainO
d o u b l e Xn = - 2 ,

Xk = 1 2 . dX = 2. t = 2, y;

Console.WriteLineC

"|

|";

f o r ( d o u b l e x = Xn; x <= Xk: x += dX )


- t;
i f ( x >= 0 && x < 10 ) = t * x ;

//


// 1 , 4 , 5
// 2
// 2

&

82

4.

4.7

()
i f ( >= 10 )
C o n s o l e . W r i t e L i n e C "|

= 2 * t;
{0,6} | {1.6}

|". , ):

/ / 2
// 3

}
}
}
,
. . ,
, ,
4.3, ,

w h i l e f o r
. , :
f o r :
f o r ( b l ; 2: ) ;
whi 1:
:
w h i l e ( 2 )

{
;

foreach
f o r e a c h
, , .
, ,

foreach ( . . 136).


,
.
do w h i l e ,
, , .
w h i l e ,
, ,
.

83
f o r e a c h
.
f o r .
,
,
(, 1 ) .
.
, :

, ,
,
( );

, ,
;


( );

, , ,
, .


# ,
:

g o t o ;

b r e a k ;

c o n t i n u e ;

r e t u r n ;

t h r o w .

,
, .
.
, t h r o w
. 93.

goto
g o t o :

goto ;
goto case _;
goto default;

: ;

84

4.

goto .
, ,
. ,
.
:

(,
-
).


, goto
.
,
, , .
goto
switch. goto case _
, goto default
default. , #
,
, .

break
break
, ,
break.
ch (
6

) = 10~

2

= 1+

2!

4!

+...
6!

"
+ +...
2!

| \ < .
, .
.
| \ > .
: ,

. ,
.
,
, . ,
- , -

85
( ,
,
).

.
break.
,
, ,
, , .
.
,
,
1

, , .
: ,
,
.


:
Cn+i

C,i ' ^

. +1,
:

, _
~

2\ -

"

- (2( + 1 ) ) !

(2 + 1)(2 + 2 ) '

4.8 .
4 . 8 .
j s i n g System:
'aniespace Conso 1 eAppl i c a t i o n l
{
class Class!
{

s t a t i c void MainO
{
double e = le-6;
const i n t M a x l t e r = 500;
C o n s o l e . W r i t e L i n e C "

/ /
:" );

s t r i n g buf = Console.ReadLineO;
d o u b l e x = C o n v e r t . T o D o u b l e C b u f );
b o o l done = t r u e ;

//

d o u b l e ch = 1, = c h :
f o r ( i n t n = 0: M a t h . A b s ( c h ) > e; n++ )

{
, .

4.

4.8

()
c h * = x * x / ( 2 * n

l ) / ( 2 * n

+= c h ;

2 ) ;

//

if ( n > M a x l t e r )

{ done = f a l s e ;

break:

}
if ( done ) C o n s o l e . W r i t e L i n e C

" -

else

" "

Console.WriteLineC

" + );
);

}
}
,
,
.
, , d o u b l e ,

- , . .
"!:

( );

( ) ;

(
);

(
).

, ,
,
,
! , ,
.

continue
c o n t i n u e
, ,
.
4 . 8 c o n t i n u e :
f o r i n t n = 0; M a t h . A b s ( c h ) > e; n++ )
{
ch * = x * x / ( 2 * n + 1 ) / ( 2 * n + 2 ) ;
+= c h ;
if ( n <= M a x l t e r ) c o n t i n u e ;
done = f a l s e :

break;

87
return
return
. :

return [ ];
.
void,
.

, ,

. ,

.
11 , ,
, ,
.
, .

.
- .
#
. ,
,
, , .
,
( ) .
,
- . .
. 4.5.

. 4 . 5 .

88

4.

,
,
, ,
, (. 4.6).

!
1
j

J ;__
j

^nrm

J j
!

j
!
'_

. 4 . 6 .

. (
, ),
.

; # (
).
, .
, ,

. ,
, , ,
.

89


# ,
( ), .
( . . 46),
.
, ,
,
,
.
. #
.
# ,
, Ctrl+C.
,
.
, ,
.
- .

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

, .

,
, , ,
,
.

,
t h r o w . . 4.1
, .
System. E x c e p t i o n , ,
S y s t e m E x c e p t i o n .
t r y .

90

4.

4 . 1 .

Ari thmeti cExcepti on


(
DivideBeZeroException OverFlowException)

ArrayTypeMi smatchExcepti on

Di vi deByZeroExcepti on

FormatException

IndexOutOfRangeException

InvalidCastException

OutOfMemoryExcepti on

OverFlowException

StackOverR owExcepti on

try
try :

try.
. , ,
;

catch,
, ;

f i n a l l y ,
.

try:

try [ catch ] [ finally ]


catch, f i n a l l y ,
.
, .
1. .
, , . ,
try, ,
.
2. ,
, .
3. final 1, (
, ).

91
4.

,
. .
.

,
, ,
.

t r y .
c a t c h ,
.
. c a t c h
, ,
.

. :

catch( ) { ... /* */ }
catch( )
{.../* */ }
catch
{ ... /* */ }
,
- ,
.
,
.
.
, ,
( )
. :

-' {
...

//

catch ( OvertlowException ) {
// O v e r f l o w E x c e p t i o n
catch

...

DivideByZeroExcepton

()

.// D i v i d e B y Z e r o E x c e p t i o n

( 0)

catch {
/7

,
.

92
, ,
finally ( ), ,
try.
, ,
, , ,
, .
4.9 ,
.
, .

, ,
, .
.

4 . 9 .
using System;
namespace ConsoleApplicationi
{

class Program
{

static void MainO


{
string buf;
double u, i, r;
try

{
Console.WriteLineC

" :"

);

buf = Console.ReadLineO;
u = double.ParseC buf );
Console.WriteLineC

" :"

):

buf = Console.ReadLineO;
r- = double. ParseC buf );
i = u / r;
Console.WriteLineC " - " + i );

}
catch ( FormatException )
{
Console.WriteLineC

" !"

);

}
catch

//

{
Console.WriteLineC " " );

}
}

}
}

93

try . ,
try
catch, ,


. , ,
,
,
. throw.

throw
,
# , .
throw ,
. ,
System.Exception.
.
throw , :

throw [ ];
catch
. , throw,
, :
:hrow new DivideByZeroExceptionO;
throw ,
0 new.

.
, , throw,
, catch, .
, # :
jsing System;
class Test
static void F ( ) {
try {
GO;

// ,

}
catch ( Exception ) {
Console.WriteLineC "Exception in F: " + e.Message );
e = new Exception( "F" );
throw;

//

4.

}
s t a t i c void GO {
throw new Exception( "G" );

//

}
static void MainO

try {
FO;

}
catch ( Exception e ) {
Console.WriteLineC "Exception in Main: " + e.Message );

F ,
, Message
( Exception ).
.
Exception ,
, , ,
, :
Exception in F: G
Exception in Main: G
throw :
throw e;
,
, :
Exception in F: G
Exception in Main: F
,
.

Exception
Exception ,
. . 4.2.
4 . 2 . System.Exception

HelpLink

U R L

Message

. .

Source

95

STTrace

,
.

" r E x c e p t i o n

~r.rget.Site

checked unchecked
3, ,
, checked unchecked,
, ,
, , :
- checked (b + ) ;

//

,^checked {

//

- b + :

, .


#
, , .
,
. ,
,
.
, .

, ,
. ,
,
.


. , ,
.
.
(,
- ) , .

96

4.

-, ,
(, ,
, , 0 100 1 . . ) .
:
, ,
, .

:

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

. ,

switch, i f .
for foreach. goto
, , switch
,
, break return.
. do
,
, , .
,
.
. 82.
(
, ),

. .
if ,
, .
true false:
bool busy:
if ( busy == true ) { ... }
if ( busy == false ) { ... }
1

// ! if ( busy )
// ! if ( !busy )

,
,
.

97
. :
if ( < b ) - 1 ;
else if ( > b ) = 2;
else if ( == b ) = 3:
:
if
( < b ) = 1:
else if ( > b ) = 2:
else
= 3;
:
= 3:
if ( < b ) = 1 ;
if ( > b ) - 2;
i f ,
else :
if ( i > 0 ) break:
// i <=
3 :
if ( z == 0 ) i = j; else i = k;

// : i = z == 0 ? j : k;


,
.
. , , switch
default, ,
.
3 .
- (),
.
,
. ,
.
,
, .

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

4.

98

, ,

!.

, , <
.
,

- ( -
) ,
.

, , .
,

,
.
, , , -
,
, .
.
, . #
, 15,
, , .

,
, ). ,
f . !
, ,
, .

3-5

, , :
s t r i n g but

= "qwerty";

d o u b l e ex

= 3.1234;

int

number = 1 2 ;

byte

if ( done ) C o n s o l e . W r i t e L i n e !

" -

else

" "

if

=0;

Console.WriteLineC

" + );
):

( x >= 0 && x < 10 ) = t * x ;

e l s e if ( x >= 10 )

= 2 * t:

else

= x;

, .

99

,
, .

:
II
//
//
{

, ,

,
/*

*/

}


-

//

. ,
:
f=a+b;

// ! f - + ;

, :
while ( true )
{
while ( < )
{
f o r ( i = 0; i < 10; ++i )
{
f o r ( j = 0; j < 10; + + j )
{
}
}
}
}

//
//
//
II
//

end f o r (
end f o r (
end w h i l e
end w h i l e

j
i
(
(

= 0; j < 10; + + j )
= 0; i < 1 0 ; + + i )
x < )
true )

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

.
,
, [ 2 ] , [ 6 ] .

,
.

5
:

( . . 13)

( . . 17). ,
, - Main.
,
. , , ,
.
, .
, ,

.
c l a s s , ,
- , . ,
( )
, :

[ ] [ ] class [ : ]
-
, c l a s s ,
. # .
, .
, .
, :
c l a s s Demo

{}

.
, ,
12.

, .

101

,
.
. 5.1.
.
.
.
5 . 1 .

new

.
.
,
8 ( . . 175)

")

public

protected

-i

internal

( ) '

protected internal


( )

private

.
,

abstract

. ,
8 ( . . 181)

sealed

. ,
8 ( . . 182)

static

. 2.0.
( . . 114)

2-6

.
,
, .
,
( ) .
: public internal. ,
,
internal.
,
,
, , .

1 . 9.

110

5. :

5.3

()

short tl = 3.

t2 = 4:

i n t = Max( t l , tZ ) :

//

Console.WriteLineC

// : 4

):

i n t z = Max( a + t l . t l / 2
Console.WriteLineC

b ):

);

//

Max

Max

// : 5

,
. . Main
.
, , ,
-

,
, ,

.

.
:
, .
: .

.
. , .

( )


, .
#
:

-;

- ref;

o u t :

- params.

. ,
-. -
, :
p u b l i c i n t Calculate( i n t a.

r e f i n t b . o u t i n t c , params i n t f ] d ) ...

200

9.

9.1

()

{
Monster temp = (Monster) obj;
if ( this.health > temp.health ) return 1;
if ( this.health < temp.health ) return -1;
return 0;

}
string name;
int health, ammo;

}
class CI ass 1
{

static void MainO


{
const int n = 3;
Monster[] stado = new Monster[n];
stado[0] = new Monster( 50, 50, "" );
s t a d o [ l ] = new Monster( 80, 80, "" );
stado[2] = new Monster( 40, 10, "" ) ' ;
Array.Sort( stado );

//

foreach ( Monster el em in stado ) elem.Passport();


}

}
}
:
Monster
Monster
Monster

health = 40 ammo = 10
health = 50 ammo = 50
health = 80 ammo = 80

,
.

. # I Comparer,
.


( IComparer)
IComparer System. Col lections.
CompareTo,
, :
interface IComparer
{
int Compare ( object obi, object ob2 )

201

.NET

,
,
.
(
).

( Name, SortByName) ( Ammo,
EortByAmmo) 9.2.
, M o n s t e r .
9 . 2 .
. s i n g System;
. s i n g System.Col l e c t i o n s ;
'amespace C o n s o l e A p p l i c a t i o n i
class

Monster

{
p u b l i c Monster(

int health,

i n t ammo,

s t r i n g name )

{
this.health = health;
this.ammo
= ammo;
this.name
= name;

}
p u b l i c i n t Ammo
{
g e t { r e t u r n ammo;
set

{
if ( v a l u e > 0)
else

ammo = v a l u e ;
ammo = 0;

}
}
p u b l i c s t r i n g Name
{
g e t { r e t u r n name;

}
virtual

public void

PassportO

{
Console.WriteLineC

"Monster {0} \t h e a l t h = {1}


name, h e a l t h , ammo );

ammo = { 2 } " ,

}
p u b l i c c l a s s SortByName :

IComparer

//

{
i n t IComparer.Compare( o b j e c t o b i ,

o b j e c t ob2 )

202

9.

9.2

()

M o n s t e r ml = ( M o n s t e r ) o b i :
M o n s t e r m2 = ( M o n s t e r ) o b 2 ;
return

String.Compare(

p u b l i c c l a s s SortByAmmo
int.

IComparer.Compare(

m1.Name,

m2.Name

);

IComparer
object obi,

o b j e c t ob2

{
M o n s t e r ml - ( M o n s t e r ) o b i :
M o n s t e r ni2 - ( M o n s t e r ) o b 2 ;
if ( ml.Ammo > m2.Ammo )

return

1;

if ( ml.Ammo < m2.Ammo ) r e t u r n - 1 ;


r e t u r n 0.

string
mt

Class
{

name:

health,

ammo;

Class1

s t a t i c void M a m ( )
const

int

Monster3

n=3.
s t a d o new M o n s t e r ] ,

s t a d o [ 0 ] = new M o n s t e r ( 50. 5 0 .
s t a d o [ l ] = new M o n s t e r ( 8 0 . 8 0 .
s t a d o [ 2 ] = new M o n s t e r ( 4 0 . 10.
Console.WriteLine(
Array.Sort(

stado,

"

""
""
""

);
);
):

: "

);

new M o n s t e r SortByName

j;

f o r e a c h ( M o n s t e r el em in s t a d o ) el em. P a s s p o r t ' , ' ) .


Console.WriteLine(

" "

Array.Sort ( stado.

new M o n s t e r . S o r t B y A m m o O

f o r e a c h ( M o n s t e r elem in s t a d o

:
:
Monster

h e a l t h 50 ammo

M o n s t e r

h e a l t h = 40 ammo

10

M o n s t e r

h e a l t h = 80 ammo

30

50

%:

);

) elem.Passport();

203

.NET

:
M o n s t e r

hheeaalltthh = 40 ammo

10

M o n s t e r

hheeaalltthh = 50 ammo

50

Monster

hheeaalltthh = 80 ammo

80


I C o m p a r a b l e ,
.
, .
: < >, <= >=, == ! =.
, Is.

IComparable, fcqual s
GetHashCode.
o b j e c t . object* (. . 183).

9.3 M o n s t e r .

h e a l t h , ,

9 . 3 .
u s i n g System;
namespace C o n s o l e A p p l i c a t i o n i
c l a s s Monster

IComparable

{
p u b l i c Monster(

int health,

i n t ammo, s t r i n g name )

{
this.health = health;
this.ammo
= ammo;
this.name

= name;

p u b l i c o v e r r i d e bool

Equals( o b j e c t obj

{
if ( obj == n u l l

II

GetTypeO

!= obj.GetType()

return false;

M o n s t e r temp = ( M o n s t e r ) o b j ;
r e t u r n h e a l t h == t e m p . h e a l t h &&
ammo
name

public override

== temp.ammo
== temp.name;

i n t GetHashCode()

&&

iP

204

9.

9.3

()

{
return name.GetHashCode();

}
public s t a t i c bool operator == ( Monster a. Monster b )
{
return a.Equals( b );

}
//

//

public static bool operator == ( Monster a, Monster b )

//

//

return ( a.CompareTo( b ) == 0 j;

//

public s t a t i c bool operator != ( Monster a, Monster b )


{
return ! a.Equals( b );
}
//

//

public s t a t i c bool operator != ( Monster a, Monster b )

//

//
//

return ( a.CompareTo( b ) !- 0 );
}

public static bool operator < ( Monster a, Monster b )


return ( a.CompareTo( b ) < 0 );

public static bool operator > ( Monster a. Monster b )


return ( a.CompareTo( b ) > 0 );

public s t a t i c bool operator <= ( Monster a. Monster b )


return ( a.CompareTo( b ) <= 0 );

public s t a t i c bool operator >= ( Monster a, Monster b )


return ( a.CompareTo( b ) >= 0 );

public int CompareTo( object obj

205

.NET

M o n s t e r temp = ( M o n s t e r ) o b j ;
if ( t h i s . h e a l t h > temp.health ) r e t u r n
1;
if ( t h i s . h e a l t h < temp.health ) return - 1 :
r e t u r n 0;

}
s t r i n g name;
i n t h e a l t h , ammo;

}
class
{

Classl

s t a t i c void Main()

{
M o n s t e r = new M o n s t e r ( 7 0 , 8 0 ,
M o n s t e r = new M o n s t e r ( 8 0 , 8 0 ,

"" );
"" );

if

( > )

Console.WriteLine(

" "

e l s e if

Console.WriteLine(

" "

Console.WriteLine(

" "

else

);

);
);

}
}
:

( ICIoneable)
.
. ,
, ( . 9.1, ).
,
w

e m b e r w i s e C l o n e , o b j e c t .

, ,
, (. 9.1, ). .

. 9 . 1 .

9.

206


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

I C l o n e a b l e 1
9.4
M o n s t e r MemberwiseClone, I C l o n e a b ' r
.
, MemberwiseClone
. ,
o b j e c t ( p r o t e c t e d ) .
9 . 4 .
u s i n g System:
namespace

ConsoleApplIcationi

{
c l a s s Monster

ICloneable

{
p u b l i c Monster( i n t h e a l t h ,

i n t ammo,

s t r i n g name )

{
this.health = health:
this.ammo
= ammo:
this.name
= name;

}
p u b l i c Monster S h a l l o w C l o n e ( )

/ /

{
return (Monster)this,MemberwiseClone();

}
public object CloneO

//

{
r e t u r n new M o n s t e r ( t h i s . h e a l t h , t h i s . a m m o .

" " + t h i s . n a m e );

}
virtual

public void

PassportO

{
Console.WriteLineC

"Monster {0}

\t h e a l t h = { 1 }

name, h e a l t h , ammo );

}
s t r i n g name;
i n t h e a l t h , ammo;

}
class
{

Classl
s t a t i c void MainO

ammo = { 2 } " ,

.NET

207

{
Monster = new Monster( 70. 80. "" );
Monster X = ;
Monster Y = .ShallowClone();
Monster Z = (51).CIone();

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

( I Enumerable)

foreach .
. N E T
, IEnumerable
IEnumerator. foreach
. ,
.
IEnumerable () GetEnuraerator,
IEnumerator (),
.
IEnumerator :

Current, ;

MoveNext, ;

Reset, .

foreach ,
.
, ,
foreach, : GetEnumerator, Current,
MoveNext Reset. ,
, ,
, MoveNext 1
, Current
. .
, , 2.0
, .
,
. foreach

208

9.

, .
y i e l d .
( 9.5).
, Monster,
.
.
9 . 5 .
using System;
using System.Col lections;
namespace ConsoleApplicationi

{
class Monster { . . . }
class Daemon { . . . }
class Stado ; IEnumerable

// 1

{
private Monster[] mas;
private int n;
public

StadoO

{
mas = new Monster[10];
n = 0;
}
public IEnumerator GetEnumeratoK)
{
for ( int i = 0; i < n; ++i ) y i e l d return mas[i];
}
public void Add( Monster m )
{
if ( n >= 10 ) return;
mas[n] = m:
++n;

class Classl
{

static void MainO


{
Stado s = new StadoO;
s.Add( new Monster() );
s.Add( new Monster("") ):
s.Add( new Daemon );
foreach ( Monster m in s ) m.Passport();

// 2

.NET

209

, 2.0 , ,
IEnumerable ( 1), (
2 ) . MoveNext Current
IEnumerator.
, 9.5,
. foreach
,
.
, , , ,
, ,
.
9.6 , .
9 . 6 .
using System;
using System.Collections;
namespace ConsoleApplicationi

{
class Num : IEnumerable
{
public IEnumerator GetEnumerator()
{
y i e l d return "one";
y i e l d return "two";
y i e l d return "three";
y i e l d return "oops";

}
}
class Classl
{

static void MainO


{
foreach ( string s in new NumO ) Console.WriteLineC s );

}
:
one
two
three
oops

( 1 5 ) :
using System;
using System.Col lections;

111

- , 7 ( . , 154),
.

-
-


, - :
. o i d ( i n t )
. .
,
(. .
: ,
, ,
. ,
, , , .
. ,
-

,
, .
, ,
, , , -
.
*.
,
:
-nt
X = 1;
; ; y t e = 1:
. s h o r t = 1:
, ,
:
;

):

( ) :

( ) :

( 200 ) ;

( / 4 + 1 ) ;

-
,
, , ,
,
return. ,
, . ,

- ( . . 4 5 ) .

210

9.

namespace
{

ConsoleApplicationi

class Classl
{
public static IEnumerable Count(

int from,

int to )

{
from = 1;
while ( from <= to ) y i e l d return from++:

}
static void MainO
{
foreach ( int i in Count( 1, 5 ) ) Console.WriteLineC i );

}
}
,
. 9.7
Stado,
9.5,
, Monster (
GetType, object).
9 . 7 .
using System;
using System.Col lections;
using MonsterLib:
namespace

ConsoleAppl i c a t i o n i

{
class Monster { . . . }
class Daemon

{ ... }

class Stado ; IEnumerable


{
private Monster[] mas;
private int n;
public StadoO
mas = new Monster[10];
n = 0;
public

IEnumerator GetEnumeratorO

for ( int i = 0 ; i < n; ++i ) y i e l d return mas[i];


public IEnumerable BackwardsO

//

for ( int i = n - 1; i >= 0; --i ) y i e l d return m a s [ i ] :

public

IEnumerable MonstersOnlyO

//

112
- .

-.

- r e f
:

ref
, - :
v o i d (

ref int )

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

-
.
r e f .

,
.
- -
( 5.4).
5 . 4 . - -
u s i n g System;
namespace C o n s o l e A p p l i c a t i o n i

class Classl

{
s t a t i c void P( i n t a,

ref int b )

{
a = 44; b = 33;
Console.WriteLine(

" {0}

{1}",

a,

b );

"

{0}

{1}",

a,

b );

"

{0}

{!}",

a,

b ):

}
s t a t i c void Main()
{
i n t a = 2 . b = 4;
Console.WriteLineC
P( a , r e f b ) ;
Console.WriteLineC

113
:
:;.
2 4
44 33

2 33
, Main , , b , .
,
, , .
, - ,
,
, .
, , .

, .

,
,
.


,
, ,
. : .
o u t . ,
, ,
.
.
5.4 ,

( 5.5).
5 . 5 .
, s i n g System;
"smespace C o n s o l e A p p l i c a t i o n i
class

Classl

{
s t a t i c v o i d P(

{
}

a = 44; b = 33:
Console.WriteLineC

static
^

i n t a.

out

int b )

"

{0}

{1}".

a,

b );

void MainO

&

114

5. :

5.5

()

int = 2 , ;
( a, out b ) :
Console.WriteLineC "

{ 0 } { ! } " , a, b );


out.

,
. ,
.

this
.
,

, .
this,
.
this ,
, ,
, :
'<

class Demo
double ;
public Demo TO

//

return this;
1
public void Sety( double )

**

this. = ;

//

.
new.
. :

, void.

115

-
, ,
n u l l .

"J

, ,
.


| ., , 5.1). ,
.
, ;. 5.6 Demo ,
( ) .
.
5 . 6 .
..s-ng S y s t e m ;
"=mespace C o n s o l e A p p l i c a t i o n i
c l a s s Demo
{
p u b l i c Demo( i n t a, d o u b l e )

//

{
t h i s . = ;
t h i s . = :

}
p u b l i c double GetyO

//

{
return ;

}
i n t ;
double ;

}
class
{

Classl
s t a t i c void MainO

{
Demo a = new Demo( 3 0 0 , 0 . 0 0 2 )
Console.WriteLineC

}
}

a.GetyO

):

//
// ;

0,002

Demo b = new Demo( 1, 5 . 7 1 );

//

Console.WriteLineC

// :

b.GetyO

);

5,71

116

5. :

,
.
,
:
class Demo
{
public DemoC int a )

// 1

{
this.a = a:
this. = 0.002;

}
public Demo( double )

// 2

{
this.a = 1:
t h i s . = ;
}

Demo x = new Demo( 300 );


Demo = new DemoC 5.71 );

// 1
// 2

.
- ,
-,
. this
, :
class Demo
{
public DemoC int a )

// 1

{
this.a = a;

}
public DemoC int a, double ) : thisC a )

// 1

{
t h i s . = ;
}

}
,

,
.
, # object.
, ,
.

117

<

base, .
, :
p u b l i c DemoC i n t )

: baseC)

// 1

{
t h i s . = :

,
.

, .'. ,
.
, .
,
. , .
,
.
, , , . ,
# ( p r i v a t e ) .
. 5.7
, .
.
5 . 7 . ( 2.0)
. s i n g System;
'STiespace Consol eAppl i c a t i o n i
class D
{
p r i v a t e DC){}
s t a t i c DC)

/ /
II

a = 200;

}
static int a:
s t a t i c double b = 0.002;
public s t a t i c void PrintC)

{
Console.WriteLineC
Console.WriteLineC

"a = " + a );
"b = " + b );

118

5. :

5.7

()

class Class2
{

static void MainO


{
D.PrintO;

//

D d = new DO;

// :

, ( ),
,
.

2.0 ,
static. ,
, .
s t a t i c (
).
, , .
5.8 .
5 . 8 . ( 2.0)
using System;
namespace ConsoleAppli cati onl

{
'ft.

s t a t i c class D
{
s t a t i c int a = 200;
s t a t i c double b = 0.002;
public static void Print

{
Console.WriteLine( "a = " + a );
Console.WriteLineC "b = " + b );

class Classl
{

static void MainO


D.PrintO;

119

i ,
, ,
. (, , ) . ,
- ( 5.9) ,
.
5 . 9 . Monster
' : System;
respace

ConsoleApplicationi

;i ass Monster
public Monster()
this.name

= "Noname";

this.health = 1 0 0 ;
this.ammo

= 100;

public Monster( string name )

: thisO

this.name = name;

public Monster( int health,

int ammo, string name )

this.name
= name;
this.health = health;
this.ammo
= ammo;

public int GetName()


return name;

public int GetHealthO


return health;

public int GetAmmoO


return ammo;

public

void

PassportO

102

5. :

, ,
. new.
:

Demo b

nPW "'.'

11 Demo

new Demo\

//' Demo

, ,
(. - . 3 5 ) .
, , ,
.
, new OutOfMemoryExcepti on.
,
.

,
. ,

.
-

,
.

),


.
. , :
, , , , , ,
(. 5.1).
( . . 5.1):

1
1

, .

,
.


, .

120

5. :

5.9

{)

Console.WriteLineC "Monster { 0 } \t health = { 1 } ammo = { 2 } '


name, health, ammo ):

string name;

//

int health, ammo;

class Classl
{
s t a t i c void MainO
{
Monster X = new MonsterO;
X.PassportO;
Monster Vasia = new MonsterC "Vasia" );
Vasia.PassportO;
Monster Masha = new MonsterC 2 0 0 , 2 0 0 , "Masha" );
Masha.PassportO;

:
"3
J.

Monster Noname
Monster Vasia

health = 100 ammo = 100


health = 100 ammo = 100

Monster Masha

health = 200 ammo = 200

(name, health ammo), (GetName, GetHealth,


%

GetAmmo Passport) , -

, .

. ,

. :

[ ] [ ]
{
[ get ]
[ set _ ]
}
.
( public),
.

121

,
( g e t ) ( s e t ) .
get, set, .
set, ( r e a d - o n l y ) ,
get, ( w r i t e - o n l y ) .
# 2.0
get set. ,
.
,
, .
, publ i ,
, protected internal,
internal, protected private. 2.0

[ ] [ ] _
{
[ [ ] [ ] get _ ]
[ [ ] [ ] set ]
}
:
j j b l i c class Button:

Control

private string caption;


public string Caption {
get {
return caption;

// ,
//
//

}
set {
//
if (caption != value) {
caption = value;

Button Control Button ,


Button Control.


, , ,
.
, :
Button ok = new ButtonO;
:k.Caption = "OK";
string s = ok.Caption;

//
//

122

5. :


.
. get
return, ,
. set
value,
.
, . ,
,
, . ,

, , .
,
, :
class
private s t a t i c ComplexObject x;
public s t a t i c ComplexObject X

//
//

get
{
if ( == null) {
x = new ComplexObject(); // 1-
return X;

Monster, 5.9, ,
3

. Name

,
1

, Health Ammo
. ,
.
5 . 1 0 . Monster
using System;
namespace

ConsoleApplication1

class Monster
{
1

public Monster()

, ,
readonly. .

123

{
this.health = 1 0 0 ;
this.ammo = 1 0 0 :
this.name = "Noname";

public Monster( string name ) : t h i s O


{
this.name = name;

}
public Monster( int health, int ammo, string name )
{
this.health = health;
this.ammo
= ammo;
this.name
= name;

public int Health

// Health health

{
get

{
return

health;

}
set
{
if (value > 0) health = value;
else
health = 0:

}
public int Ammo

// Ammo ammo

{
get
{
return ammo;

}
set
{
if (value > 0) ammo = value;
else
ammo = 0;

}
}
public string Name

// Name name

{
get

{
return name;
}
^

124

5. :

5.10

()

public void

PassportO

Console.WriteLineC"Monster { 0 } \t health = { 1 } ammo = { 2 } '


name, health, ammo);

string name:
int health, ammo;

//

}
class Classl
{
static void MainO
Monster Masha = new MonsterC 200. 200, "Masha" );
Masha. PassportO;
--Masha.Health;
//
Masha.Ammo += 100;
//
Masha. PassportO;

}
}

;
Monster Masha

health = 200 ammo = 200

Monster Masha

health = 199 ammo = 300


, ,
-
^

, . , -

3|

. , .

(pri v a t e ) . -

,

. ,
, ,
.
,
,
1

. ,
,
1

, .

125


.
.
(
) . ,
, . ,
1-2 : ,
,
.
,
(
pri vate).
, .
, .
,
.
.
, ,
. ,
out.
,
ref. ,
, .

, .
, . ,
, ,
.
, ,
, . ,
( ), -
, .
,
4 ( . . 9 5 ) .

6

,
, .

'i

, , -

"5

. , ,

'*

, .

.
,

(). -

3|

,
,
. ,
( ).
, .
:
null .
. 6.1 ,
,

int double, . 6.2

.
, , 10
100 :
int[]
w = new i n t [ 1 0 ] ;
s t r i n g [ ] z = new s t r i n g [ 1 0 0 ] ;

127


[1]

[0]

[2]

[3]

[4]

. 6 . 1 .

[1]

[0]

[2]

[3]

[
]

[4]

. 6 . 2 .
w i n t [ ] . new
10 , .
z s t r i n g [ ] . new
100 , null.
, ,
.
() ,

. ,
.
, int, ui nt, long
/long.
, :
short = . . . ;
s t r i n g [ ] z = new string[n + 1 ] ;
,
(,
w 0 9 ) .
,
:
44]

z[i]

,
.
: ,
IndexOutOfRangeException.

128

6.

.
, ,
, :
i n t [ ] = new i n t [ 1 0 ] ;
i n t [ ] b = ;

// b

# Array,
System. ,
, , .
System.Array ( . . 133).

, , , ,
.

# : ,
().


.
:

[]
[]
[]
[]
[]

:
=
=
=
=

new [ ];
{ };
new [] { };
new [ ] { };

,
.

( ):
int[]
int[] b

new i n t [ 4 ] ;

// 1
// 2

i n t [ ] = { 61. 2, 5, -9 } :
i n t [ ] d = new i n t [ ] { 61. 2, 5. -9

II 3
// 4


new

i n t [ ] e = new i n t [ 4 ] { 61. 2, 5. -9

// 5

.
, , , ,
. ,
, , .
1

: ,
, , , .

129

.
: 3-5, .
( 3 ) ,
.
new, ( 2 ) .
, ,
( 4 ) .

,
.

,
, ,
6 ( 6.1).
6 . 1 .
, " System;
-;-espace C o n s o l e A p p l i c a t i o n i
class Classl

{
s t a t i c void MainO
{
c o n s t i n t n = 6;
i n t [ ] a = new i n t [ n ] { 3,

12.

5.

-9,

C o n s o l e . W r i t e L i n e C " : " ) ;
f o r ( i n t i = 0; i < n; ++i )
Console.WriteC " \ t " + a [ i ] ) ;
Console.WriteLineC);
l o n g sum = 0;
//
i n t num = 0 ;
/ /
f o r i n t i = 0; i < n; ++i )
i f ( a[i] < 0 )

{
sum += a [ i ] ;
++num;

}
Console.WriteLineC
Console.WriteLineC

"
= " + sum );
"- = " + num );

i n t max = a [ 0 ] ;
//
f o r ( i n t i = 1 ; i < n; ++i )
i f a [ i ] > max ) max = a [ i ] ;
C o n s o l e . W r i t e L i n e C " = " + max );

, .

103

. 5 . 1 .

7J

.
,

, .

,
. ,
.



(. . 5 7 ) .
, , , .
,
,
, (. 5.2).
, , a, b ,
b = . b
. ,

130

6.


.
. :

[,]
[,]
[,]
[,]
[,]

;
=
=
=
=

new [ _1, _2 ];
{ };
new [,] { };
new [ _1, _2 ] { };

( ):
i n t [ , ] a;

/ I I

i n t [ , ] b = new i n t [ 2 . 3 ] ;

111

i n t [ , ] = { { 1 , 2. 3 } . { 4 , 5. 6 } } ;

// 3

new

i n t [ , ] = new i n t [ . ] { { 1 , 2 , 3 } , { 4 , 5 . 6 } } ;

// 4

i n t [ J d = new i n t [ 2 . 3 ] { { 1 . 2 . 3 } , { 4 . 5 , 6 } } ; / / 5

,
, , .
, ,
, :
[ 1 , 4]

b[i. j ]

b[j,

i]

,
, .

,
3 x 4
(. 6.3).

0
0

/771

1f

01

02

12

13

20

22

23

21

. 6 . 3 .

, .
( ) .

. . 6.4, 6.2.

131

. 6 . 4 . 6.2
6 . 2 .
using System;
namespace ConsoleApplication1
class

Classl

{
static void MainO
const int m = 3, n = 4;
i n t [ , ] a = new int[m, n]
{ 2,-2. 8. 9 } .
{-4,-5.

6,-2 },

&

132

6.

{)

6.2

{ 7, 0, 1, 1 }

}:
Console.WriteLineC " :" );
for ( int i = 0; i < m; ++i )

{
for ( int j = 0; j < n; ++j )
Console.WriteC "\t" + a [ i , j ] ) ;
Console.WriteLineC);

}
double sum = 0;
int nPosEI;
for ( int i = 0; i < m; ++i )
{
nPosEI = 0;
for int j = 0; j < n: ++j )
{
sum += a [ i , j ] ;
if ( a [ i . j] > 0 ) ++nPosEl;

}
Console.WriteLineC " {0} { 1 } - ",
i , nPosEI

);

}
Console.WriteLineC " : "
+ sum / m / n ) ;

}
}

sum .
,
.

, sum
,
,
.



. , :
, .

133

,
.
. 6.5.

>[0]

1]

[1]

i [ l ] fo]

. 6 . 5 .
:
[ ] [ ] ;
, ,
, :
"

= new

int[3][];

//

[0]

= new

int[5];

//

0- (5 )

[1]

= new

int[3];

//

1- (3 )

a[2]

= new

int[4];

//

2- (4 )

[ 0 ] , [ 1 ] [ 2 ] ,
( ) .
:
\it[][]

= {

new i n t [ 5 ] ,

new i n t [ 3 ] ,

new i n t [ 4 ]

};

,
, :
=:i][2]

a[i][j]

a[j][i]


. , ,
.

System.Array
, #
A r r a y , ,
. 6.1.

134

6.

6 . 1 . Array

Length

( )

Rank

BinarySearch

Clear

Copy

CopyTo

GetValue

IndexOf

LastlndexOf

Reverse

SetValue

Sort

Length ,
, , .

. 6.3
Array .
6 . 3 . Array
using System;
namespace
{

ConsoleApplicationi

class Classl
{

s t a t i c void MainO
{
int[] a =

{ 2 4 , 50, 18, 3, 16, -7, 9, -1 } ;

PrintArrayC " :", a );


Console.WriteLineC Array.IndexOf( a, 18 ) );
Array.Sort(a);
PrintArray( " :", a );
Console.WriteLineC Array.BinarySearch( a,

18)

);

}
public s t a t i c void PrintArrayC string header, i n t [ ] a )

135

{
Console.WriteLineC header ) ;
for ( int i = 0; i < a.Length; ++i )
Console.WriteC "\t" + a [ i ] ) ;
Console.WriteLineC);
}

}
)

Sort, IndexOf BinarySearch ,


, , .
.
, ,
IndexOf. , 18,
.

(
) , ,
, .

Classl PrintArray,
. :
header .
Length. ,
.
:
/ :
24
"
-7

50

18

16

-7

-1

16

18

24

50

:
-1

PrintArray ,
, Array. ,
Get Value,
Array .
:
: . : i i c static void PrintArrayC string header,

Array a

Console.WriteLineC header ) ;
for ( int i = 0; i < a.Length; ++i )
Console.WriteC "\t" + a.GetValue(i) );
Console.WriteLineC);

136

6.

6.4 Array
.
6 . 4 . Array
using System;
namespace
{

ConsoleApplicationi

class Classl

/ ?
{
i n t [ ] [ ] a = new i n t [ 3 ] [ ] ;
a [ 0 ] = new int [ 5 ] { 24. 50. 18. 3. 16 } ;
a [ l ] = new int [ 3 ] { 7, 9. -1 } ;
a [ 2 ] = new int [ 4 ] { 6. 15, 3, 1 } ;
Console.WriteLineC " :" );
for ( int i = 0; i < a.Length; ++i )

{
for ( int j = 0; j < a [ i ] . L e n g t h ; ++j )
Console.WriteC

"\t" + a [ i ] [ j ]

);

Console.WriteLineC);

}
Console.WriteLineC Array.IndexOf( a [ 0 ] , 18 ) );

,
. :
:
24

50

18

-1

15

16

foreach

foreach
1

. .
,
:
.
:

foreach ( in )
1

, , 9.

137

foreach

,
(
) .
, ,
.
.
, :
int[] =

{ 2 4 , 50, 18, 3, 16, -7, 9, -1 } ;

foreach
:
foreach ( int in ) Console.WriteLineC );
:
,
.
6.4
foreach , , ,
for:
foreach ( i n t [ ] in )

{
foreach ( int in ) Console.WriteC "\t" + ):
Console.WriteLineC);

}
6.5 , 6.1,
foreach. , .
6 . 5 . foreach
using System;
namespace
{

ConsoleApplicationi

class Classl
{

s t a t i c void MainO
{
i n t [ ] a = { 3, 12, 5, -9, 8, -4 } ;
Console.WriteLineC " :" );
foreach ( int el em in a )
Console.WriteC "\t" + elem );
Console.WriteLineC);
long sum = 0;

//

int

//

num = 0 ;

foreach int elem in )


if ( elem < 0 )
^

&

138

6.

6.5

()
sum += elem;
++num;

}
Console.WriteLineC "sum = " + sum );
Console.WriteLineC "num = " + num );
int max = a [ 0 ] ;
//
foreach ( int elem in a )
if ( elem > max ) max = elem;
Console.WriteLineC "max = " + max );
}

}
}
6.3:
public static void PrintArrayC

string header,

Array a

{
Console.WriteLineC header ) ;
foreach ( object x in a ) Console.WriteC "\t" + x );
Console.WriteLineC);

}
,
, object, ,
. #
( . . 178),
, .

foreach ,
, .


, ,
,
.
Monster:
using System;
namespace

ConsoleApplicationi

{
class Monster { . . . }
class Classl

139

static void MainO


{
Random rnd = new Random();
const int n = 5;
Monster[] stado = new Monster[n]:

// 1

for ( int i = 0; i < n; ++i )

112

{
s t a d o [ i ] = new Monster( rnd.Next( 1, 100 ),
rnd.Next( 1 , 200 ) ,
"Crazy" + i .ToStringO ):

}
foreach ( Monster x in stado ) x.Passport();

// 3

}
}
:
'Onster CrazyO
'onster Crazyl

health = 18 ammo = 94
health = 85 ammo = 75

''onster Crazy2
Onster Crazy3
Onster Crazy4

health = 13 ammo = 6
health = 51 ammo = 104
health = 68 ammo = 114


Random, ( . . 148). 1
Monster?,
null. 2 :
-ew ,
(
). 3
foreach .


, , . , #
- : , ,
.

char Unicode.
(. . 26).
#
Char . N E T System.
, ,

104

5. :

, (
, , b ).

. 5 . 2 .
.
, . ,
( b ,
b nullI).

:
, ,
,
( . . 3 8 ) ( . . 4 1 ) . ,
, .

, .
:

[ ] [ ] [ const ] [ = _ ]
, 12,
. 5.2.
1-6.
5 . 2 .

new

public

protected

internal

140

6.

.
. 6.2.
6 . 2 . System.Char

GetNumericValue

,
, -1

Unicode-

IsControl

true,

IsDigit

true,

IsLetter

true,

IsLetterOrDigit

true,

IsLower

true,

IsNumber

I1
-sir
S

GetUnicodeCategory

true, (
)

IsPunctuation

true,

IsSeparator

true,

Isllpper

true,

IsWhiteSpace

true, ( ,
)

Parse

)
ToLower

ToUpper

MaxValue, MinValue


( )

6.6 .
6 . 6 . System.Char
using System;
namespace
{

ConsoleApplicationi

class Classl
{

static void MainO

{
try
char b =
1

= 0x63' , d = Ou0032';

// 1

Bee Unicode- , , (DecimalDigitNumber), (LetterNumber), (LineSeparator),


(LowercaseLetter) . .

141

Console.WriteLine( " { 0 } { 1 } { 2 } " . b . . d ) ;


Console.WriteLine( " { 0 } { 1 } { 2 } " ,
char.ToLower(b). char.Tol)pper(c), char.GetNumericValue(d) ) ;
char a;
do

// 2

{
Console.Write( " : " );
a = char.Parse( Console.ReadLine() );
Console.WriteLine( " { 0 } . - { 1 } " .
. int) ) ;
if (char.IsLetter(a))
Console.WriteLine("");
if (char. IsUpper(a))
Console.WriteLine("Bepx
pe.");
i f (char.IsLower(a))
Console.WriteLine("
pe.");
i f (char.IsControl ( a ) )
Console.WriteLine("pa");
i f (char.IsNumber(a))
Console.WriteLine("");
i f (char.IsPunctuation(a)) Console.WriteLine("Paee");
} while (a != ' q ' ) :
catch
Console.WriteLine( " " );
return;

1 .
.
.
2 .
, Ctrl .
Parse, ,
, char. ,
Enter.
, q.
1

.
.
# , ,
2

, , :
string s = '' + ' ' + ' ;

// - ""

,
, .
.
1

:
U n i c o d e !

- ( . . 59).

142

6.


, ,
Array, . 6.1.
.
6.7.
6 . 7 .
using System;
namespace
{

ConsoleApplicationi

class Classl
{

static void MainO


{
char[] a = {

'm',

'a',

's',

's',

'i',

};

char[] b = " " .ToCharArrayO;


PrintArray( " :", );
int pos = Array.IndexOf( a,

'm'

);

I
*T
"3
4

a[pos] = ' M ' ;


PrintArrayC " a:", a );

if

Array.Reverse! b ) ;

PrintArrayC

PrintArray( " b : " , b );


" b : " ,

b );

"?
#

public s t a t i c void PrintArrayC string header, Array a )

Console.WriteLineC header );
foreach ( object x in a ) Console.WriteC x );
Console.WriteLineC "An" );

:
:
massiv
:
Massi V
:

:

// 1
// 2

143

,
( 1), ToCharArray string,
( 2 ) .

string
string,
Unicode, # .
System.String . N E T .
:
string
string
string
char[]
string

s;
t =
u =
=
V =

"qqq";
new stringO '. 2 0 ) ;
{ ' 0 ' , ' 0 ' , ' 0 ' };
new string( );

//
//
//
//
//



20

(=);

( = = ) ;

( ! = ) ;

( [ ] ) ;

() ( + ) .

,
, . ,
.

, . ,
string ,
, .
.
System.String , ,
.
. 6.3.
6 . 3 . System.String

Compare


( ) .


. .

144

6.

6.3

()

CompareOrdi nal

CompareTo

Concat

Copy

Empty

( )

Format


( . )

IndexOf,

IndexOf Any,

LastlndexOf,

LastIndexOfAny
Insert

Intern,

Islnterned


,
. , Intern
,
Is Intern nul I

Join

.

( . )

Length

PadLeft,

PadRight

( )


Remove

Replace

Split

,
.

StartsWith,

EndsWith

true false
,

Substring

ToCharArray

ToLower,

Tollpper

145

Tim,


(
P a d L e f t P a d R i g h t )

rimStart,
"rimEnd

6.8.
6 . 8 . string
j s i n g System:
namespace C o n s o l e A p p l i c a t i o n i
class
{

Classl
s t a t i c void MainO

{
s t r i n g s = " ":
C o n s o l e . W r i t e L i n e C s );
s t r i n g sub = s . S u b s t r i n g C 3 ).Remove( 1 2 , 2 );
C o n s o l e . W r i t e L i n e C sub ) ;
s t r i n g [ ] mas = s . S p l i t O ' ) ;
s t r i n g j o i n e d = s t r i n g . J o m ( "!
Console.WriteLineC j o i n e d );

III

112
" , mas ) ;

C o n s o l e . W r i t e L i n e C " " ) ;
s t r i n g x = Console.ReadLineO;
C o n s o l e . W r i t e L i n e C " " + x );
double a = 12.234;
i n t b = 29;
Console.WriteLineC " a = {0,6;C}
b = { 1 , 2 : X } " , a, b );
Console.WriteLineC " a = { 0 , 6 : 0 . # } a = { 1 , 5 : 0 . # ' .
a, b );

// 3

II A
'}"
l i b

}
:


! !

!
!
; = 12,23.
b = 10
1 : S u b s t r i n g
s, ,
. Remove,
, 12-.
s u b .

146

6.

Split ( 2) ,
. ,
mas. Join ( )
mas ,
"!

". 3 , .


4 6.8 Format,

.
, ,
.
:
{ [,[:_]]}
. ,
,
. . m ,
.
,
. , .
, ,
. , ( C u r r e n c y ) ,

, X ( H e x a d e c i m a l )
.

,
. .

5
. , :
,
#, 0. #,
, . 0, ,
0. . 6.4 .
6 . 4 .

1,243

00.00

01,24

1,243

#.##

1,24

0,1

00.00

00,10

0,1

#.##

,1

147

,
.

StringBuilder
, string, ,
.
StringBuilder,
System.Text .
new , :
StringBuilder

= new StringBuilder( :

StringBuilder

b = new StringBuilder( "qwerty"

StringBuilder

= new StringBuilder! 100 ) ;

);

StringBuilder d = new StringBuilder! "qwerty", 100 ).;


StringBuilder

e = new StringBuilder! "qwerty", 1, 3, 100 );

//

//

//

//

//

:
, (
). ,
.
( 1),
, (16 ).
, , / .
, 5 3 ,
( "wer"). StringBuilder
. 6.5.
6 . 5 . System.Text.StringBuilder

Append

.

, , string

AppendFormat

Capacity

.

,
ArgumentOutOfRangeExcepti on

Insert

Length

( )

MaxCapacity

Remove

&

148

6.

6 . 5 ()

Replace

ToString

string

6.9.
6 . 9 . StringBuilder
using System;
using System.Text;
namespace ConsoleAppl icationi
{
class Classl
{

static void MainO


{
Console.WriteC " : " );
double salary = double.ParseC Console.ReadLineO

);

StringBuilder a = new StringBuiIder();


a.Appendt " " );
a.AppendFormatC " { 0 . 6:C} - { 1 , 6 : C } " .
salary, salary * 12 );
Console.WriteLineC a ) ;
a.ReplaceC " p . " , ".$" ) ;
Console.WriteLineC " : " + a );

}
}
}
:
: 3500
3 500.00. - 42 000,00.
: 3 500,.$ - 42 000..$


, ,
.
Capacity, .

Random
, ,
, . #
Random, System.

149

< Random


, :
Ondom = new Random!);
Ondom b = new Random!

//

111

1 );

: ( 1)
, .
.
i n t ( 2 )
,
.
,
. 6.6.
6 . 6 . System.Random

'iBxt!)


i nt

'.)

[ 0 , ]

8.(, )

[ , ]

'iextByt.es()

[ 0 , 2 5 5 ]

'.extDoubl()

[ 0 . 1)

6.10.
6 . 1 0 .
. s i n g System;
-amespace C o n s o l e A p p l i c a t i o n i
class
{

Classl

s t a t i c void MainO
{
Random a = new Random();
Random b = new Random( 1 );
c o n s t i n t n = 10;
Console.WriteLine!

"\n

[ 0 , 1 ] : " ) ;

f o r ( i n t i = 0; i < n; ++i )
Console.Write!
Console.WriteLine!

"{0 , 6 : 0 . # # } " , a.NextDouble() );


"An

[ 0 , 1 0 0 0 ] : " ) ;

f o r ( i n t l = 0; i < n; ++i )
Console.Write!
Console.WriteLine!

"

" + b . N e x t ( 1000 ) );

"\n

[ - 1 0 , 1 0 ] : " ) ;

105

protected

internal

private

static

readonly

volatile

( p r i v a t e ) .
,
, .
.

, s t a t i c ,
,
, .
, .
.

( ) .
,
. 5.1
Demo .
5 . 1 . Demo,
. s i n g System.
"amespace Consol eAppl i c a t i o n i
class

Demo

{
public
public
public
double

i n t a = 1;
const double
= 1.66;
s t a t i c s t r i n g s = "Demo"
y;

//
//
II
//

class Classl
{
s t a t i c void MainO
Demo x = new Demo();
Console.WriteLine! x.a );
C o n s o l e . W r i t e L i n e ! Demo.
C o n s o l e . W r i t e L i n e ! Demo.s

// Demo
):
);

//
//
//

x . a -
Demo. -

150

6.

6.10

{)

for ( int i = 0; i < n; ++i )


Console.WriteC "

" + a.NextC-10, 10) );

Console.WriteLineC "\n

[0, 255]:" );

b y t e [ ] mas = new b y t e [ n ] ;
a.NextBytesC mas ):
for ( i n t i = 0; i < n; + + i ) Console.WriteC " " + mas[i] );

0,02

[0,

0,4

1]:
0,24

0,55

0,92

0,84

0,9

0,78

0,78

0,74

657

432

354

943

101

642

[0, 1000]:
248

-8

467

[-10,

-6
[0,

771

10]:
-10

-5

-2

-1

255]:

181 105 60 50 70 77 9 28 133 150


,
, , ,
, .
,
, .

IndexOutOfRangeException,
,
for.

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

, ,
, , ,
, 13.

151

, # string
,
.
OnngBuilder. -
, ,
.

,
15.

:
, 5.
,
5, ,
.


, ,
, .
,
, .
,
.


, .

, ,
. (resolution)
1

. .
,
,
, . ,
, :
1

params,
, .

153

// :
i n t max( i n t a , i n t b )
// :
i n t max( i n t a , i n t b , i n t )
// :
i n t max ( i n t a.

string b )

// :
i n t max ( s t r i n g b, i n t a )
C o n s o l e . W r i t e L i n e C max( 1,

2 )

);

C o n s o l e . W r i t e L i n e C max( 1, 2. 3 )
C o n s o l e . W r i t e L i n e C max( 1,

"2"

);
):

C o n s o l e . W r i t e L i n e C max( " 1 " , 2 ) ) ;


max ,
(
).
,
, , b o o l c h a r i n t , f l o a t
d o u b l e . . ,
.
, , ,
,
-
( . . 4 5 ) . ,
, .
, .
( . . 106).
, , ,
( o u t r e f ) . , ,
, :
i n t maxC i n t a .

int b )

i n t max( i n t a , r e f i n t b )
,
.
, ,
, .
. N E T . , C o n s o l e
W r i t e L i n e 19 .


, .
. ,
. ,

154

7. :

, ,
.
.
,
,
.
,
, .

( , ).
,
( - 1 ) . , 0 != 1 1 != 1:
long facte long n ) {
if ( n == 0 11 n == 1 ) return 1;
return ( n * fact( n - 1 ) );

//
//

}
To :
long factt long n ) {
return ( n > 1 ) ? n * f a c t e n - 1 ) : 1 :

}

, ,
, , (
13).
,
.
,


, , .

,
. #
params. , ,

, :
public int Calculate( int a, out int c. params i n t [ ] d ) ...
. ,
, .

155

Length.
( 7.1).
7 . 1 .
.sing System;
'smespace ConsoleApplicationi
class Classl

{
public static double Average! params i n t [ ] a )

{
if ( a.Length == 0 )
throw new Exception! " " );
double av = 0;
foreach ( int elem in a ) av += elem;
return av / a.Length;

}
static void Main!)
{

try
{
i n t [ ] a = { 10, 20, 30 } ;
Console.WriteLine! Average! a ) );
intC] b = { -11. -4, 12, 14, 32, -1, 28 };
Console.WriteLine! Average! b ) );
short z = 1. e = 12;
byte v = 100;
Console.WriteLine! Average! z, e, v ) );
Console.WriteLine! Average!) ) ;

III
/12

//3
//4

}
catch! Exception e )
{
Console.WriteLine! e.Message ) ;
return;

}
}
}
:

-^

Average
. .
, (NaN)
.

156

7. :

-
. ,
.

Main
, ,
Main .
.
:
//

static MainO { ... }


static void MainO { ... }
// :

static Main( string[] args ) { / * . . . * / }


static void Main( string[] args ) { / * . . . * / }
, ,
.
args.

, args.

, ,
, void. Main
, ,
.
, :
static int Main( s t r i n g [ ] args

{
if (

. . . /* */ ) return 1;

if (

. . . /* */ ) return 100;

}
,
. , ,
. 7.2
Main, .
7 . 2 . Main
using System;
namespace ConsoleApplicationi
{

class Classl

157

static void Main( s t r i n g [ ] args )


{
foreach( string arg in args ) Console.WriteLineC arg );
Console.ReadO;

}
}
ConsoleApplication1.exe
:
;

cs\ConsoleApplicationl\bin\Debug\ConsoleApplicationl.exe one two three

"

, .

, ,

.

.
, ,
,
. , .
:
this [ _ ]
get
set

,
.

[ ,

12,

158

7. :

public, .
.
,
( g e t ) ( s e t ) .
get, set, .
set, (read-only),
get, ( w r i t e - o n l y ) .
,
.
.

, - .
7.3 -,
[, 100]. , ,
.
7 . 3 .
using System;
namespace ConsoleApplicationi
{
class SafeArray
{
public SafeArray( int size )

//

a = new i n t [ s i z e ] ;
length = size;
public int Length

// -

get { return length; }

public int t h i s [ i n t i]

//

get
{
if ( i >= 0 && i < length ) return a [ i ] ;
else { error = true; return 0; }

}
set
{
if ( i >= 0 && i < length &&
value >= 0 && value <= 100 ) a [ i ] = value;
1

C# 2.0
, ( , . . 120).

159
else error = true;
l
}
public bool error = false;

//

i n t [ ] a;

//

int

//

length;

}
class Classl
{

static void MainO


{
int n = 100;
SafeArray sa = new SafeArray( n );

//

for ( int i = 0; i < n; ++i )

{
s a [ i ] = i * 2;

III

Console.WriteC s a [ i ] );

112

}
if ( sa.error ) Console.WriteC " !" );

11 , .
, ,
, .
( 1),
get. ( 2 ) ,
set.
SafeArray :
,
;
, 0;
error, true.

error - ,
, ,
(
).
.
, .

, -
. 7.4 Pow2,
2.

106

5. :

:
, ( Classl).

, .


(, int 0,
nu 1 1 ) . ,
.
,

readonly .
, (
).

,
>

, .
.
, } '
. , ,
. , .

[ ] [ ] _ ( [ ] )


'

.
. , , ,

1-7 . 5.2,

, , virtual, sealed,
override, a b s t r a c t extern, .

public,

- , ,

( s t a t i c ) , , ,
. Main.

,
.

160

7. :

7 . 4 .
using System;
namespace

ConsoleApplicationi

{
class Pow2
{

public ulong t h i s [ i n t i]
{
get

{
i f ( i >= 0 )
{
ulong res = 1;
for ( int k = 0; k < i; k++ )
unchecked { res *= 2;

//
III

return res;

}
else return 0;

}
}
}
class Classl
{

static void MainO


{
int n = 13;
Pow2 pow2 = new Pow2();
for ( int i = 0; i < n; ++i )
Console.WriteLineC

"{0}\t{l}O

i,

pow2[i]

);

1 ,
, , . ,
, Pow2
, ulong, ?
.
:
0

16

32

64
( . . 4 6 ) .

161

128
256
512
24
2048

12

4096

# .

,
. :
- t [ , ] :
,

c o l i c int t h i s [ i n t i ,

int j ]


? ,

.
, , :
"..Object ., . ;
: = + ;

// MyObject


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

]
12,
public static. ,
(extern).
operator,
. ,
.
, .

162

7. :

.
,
( ), .

(
public s t a t i c ) ;

(
ref out);

;
, , ,
( ).

# : ,
.


:
+

++

--

true

false

operator ( )
:
public static int operator +( MyObject m )
public static MyObject operator - - ( MyObject m )
public s t a t i c bool operator true( MyObject m )
, , ,
. :

+, -, ! - ;

++ - - , ;

true false bool.

. ,
, ,
,
.

(
, ).

163

true fal se SQL,


, ,
.

7.3 SafeArray
.
:

,
;

Print ;

,
.

7.5.
7 . 5 . SafeArray
using System;
namespace ConsoleApplicationi
class SafeArray
{
public SafeArray( int size )

//

a = new i n t [ s i z e ] ;
length - s i z e ;

public SafeArray( params i n t [ ] arr )

//

length = arr.Length;
a = new i n t D e n g t h ] ;
for ( int i = 0; i < length; ++i ) a [ i ] = a r r [ i ] ;

public s t a t i c SafeArray operator ++( SafeArray x )

// ++

SafeArray temp = new SafeArray( x.length ):


for ( int i = 0; i < x.length; ++i )
temp[i] = + + x . a [ i ] ;
return temp;

public int this [ i n t i ]


get

//

164

7. :

7.5

()

{
if ( i >= 0 && i < length ) return a [ i ] :
else throw new

IndexOutOfRangeException();

//

if ( i >= 0 && i < length ) a [ i ] = value;


else throw new
IndexOutOfRangeException();

//

}
set
{

public void Print( string name )

//

{
Console.WriteLine( name + " : " );
for ( int i = 0; i < length; ++i )
Console.Write( "\t" + a [ i ] ) ;
Console.WriteLine();

}
i n t [ ] a;
int length;

//
//

class Classl
{

static void MainO


{
try

{
SafeArray al = new SafeArray( 5. 2,
al.Print(" 1 " ) ;
al++;
a l . P r i n t ( " 1" );

-1.

1,

-2 );

}
catch ( Exception e )

//

{
Console.WriteLine(

e.Message ) ;

}
}
}
}


:
+

&

= =

! = > < > = < =

165

operator _ ( 2)
:
C j b l i c static MyObject operator +
Cublic static bool

( MyObject ml, MyObject m2 )

operator == ( MyObject ml, MyObject m2 )

, , ,
.
.
== ! =, > <, >= <=
.
, ,
, .
,
, 9 ( . . 203).
SafeArray,
, 7.6.
,
.
7 . 6 . SafeArray
using System;
ramespace

ConsoleApplicationl

class SafeArray
{

public SafeArray( int size )


a = new i n t [ s i z e ] ;
length = s i z e ;

public SafeArray( params i n t [ ] arr )


length = arr.Length;
a = new i n t [ l e n g t h ] ;
for ( int i = 0; i < length: ++i ) a [ i ] = a r r [ i ] ;

public static SafeArray operator + ( SafeArray x. SafeArray ) // +


int I en = x.length < y.length ? x.length : y.length;
SafeArray temp = new SafeArray d e n ) ;
for ( int i = 0; i < Ten; ++i ) temp[i] = x [ i ] + y [ i ] :
return temp;
"

166

7. :

7.6

()

public s t a t i c SafeArray operator + ( SafeArray x, int )

// +

SafeArray temp = new SafeArrayCx.length);


for ( int i = 0; i < x.length: ++i ) temp[i] = x [ i ] + y,
return temp;
public s t a t i c SafeArray operator + ( int x, SafeArray )

// +

SafeArray temp = new SafeArray(y.length);


for ( int i = 0; i < y.length; ++i ) temp[i] = x + y [ i ] ;
return temp;
public static SafeArray operator ++ ( SafeArray x )

// ++

SafeArray temp = new SafeArrayU.length);


for ( int i = 0: i < x.length; ++i ) temp[i] = + + x . a [ i j ;
return temp;

public int t h i s [ i n t i]

II []

get
{
if ( i >= 0 && i < length ) return a [ i ] :
else throw new IndexOutOfRangeExceptionC);
set
{
if ( i >= 0 && i < length ) a [ i ] = value;
else throw new IndexOutOfRangeExceptionC);

public void PrintC string name )


{
Console.WriteLineC name + " : " );
for ( int i = 0; i < length; ++i ) Console.WriteC "\t" + a [ i ]
Console.WriteLineC);

i n t [ ] a;

//

int length;

//

class Classl
{

static void MainO

167

try
SafeArray al = new SafeArrayC 5, 2, -1, 1, -2 );
a l . P r i n t ( " 1" );
SafeArray a2 = new SafeArray( 1, 0, 3 );
a2.Print( " 2" );
SafeArray a3 = al + a2;
a3.Print( " 1 2" );
al = al + 100;

// 1

a l . P r i n t ( " 1 + 100" );
al = 100 + a l ;

// 2

al.PrintC "100 + 1" );


a2 += ++a2 + 1 ;

113

a2.Print( "++a2, a2 + a2 + 1" );

catch ( Exception e )
{
Console.WriteLine( e.Message ) ;

:
1:
5
i 2:

-1

1
0
1 2:

7
3
J 1 + 100:
106
103
100 + 1:

3
100

102

99

206
203
++2. 2 + 2 + 1:

200

202

199

: ,
,
( 2 1 ) .
+= ( 3) ,
.

168

7. :

, . 3
, .

.
: temp.a[i] = + y . a [ i ] .



.
:

implicit operator ( )
explicit operator ( )

//
//

,
. ,
. ,
, .
1

.
Monster, 5:
public static implicit operator i n t ( Monster m )
return m.health:

}
public static e x p l i c i t operator Monster( int h )
{
return new Monster( h. 100, "Fromlnt" );
.
, :
Monster Masha = new Monster( 200, 200, "Masha" );
int i = Masha;

//

Masha = (Monster) 500;

//

, ;

, ;

, object ,
.

169

.
.
,
,
. implicit e x p l i c i t
, ,
.
,
.
, .

# , .
.
,
, , , ,
, ( , . . ) .
:

[ ] [ extern ] -

, ,
.
( ~ ) ,
.
, (extern).
, .

. ,
,
. ,
,
.
.


, .
,
.

107

:
o u b l i c double GetyO

//

{
return ;

}
, .
,

, ,
'Stum (. 5.3). ,
void, return .


return ..

. 5 . 3 .
.
,
. -

.
, ,
Sin Math,
, WnteLioe Console
;cuble = 0 . 1 :
:; - Math . S i n ( x ) :
2i'isole.Wri t e L i n e ( x ) ;
Sin
, , Wntel ine .

, , , ,
,
.

, ,
, .
,

170

7. :

.
(
) . ,
1

.
, Monster Gun.
,
:
using System;
namespace
{

ConsoleApplicationi

class Monster
{
class Gun
{

}
}
: ,
. 9.


, , ,
( p r i v a t e ) :

, , ;

, ;

,
.

, ,
, , ,
,
.

(, + -,
). - ,
, + ++ (
). ,
.
1

: (. . 104).

171


, ,
.
, ,
,
.
,
, , , ,
, ,
params,
.


.
,

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

,
, ,
.

#
.

173

. ,
System.Object:

[ ] [ ] class [ : ]

,
, . ,

, .
.

. ( . . 120)
Monster, .
, ,
Monster, .
Monster ( 8.1).
8 . 1 . Daemon, Monster
using System;
namespace

ConsoleApplicationi

class Monster

class Daemon

Monster

public Daemon
brain = 1:

public Daemon( string name, int brain )

: base( name )

// 1

this.brain = brain;

public Daemon( int health,

int ammo,

: base( health, ammo, name )

string name,

int brain )
// 2

this.brain = brain;

new public void PassportO

// 3

&

174

8.

8.1

()

{
Console.WriteLineC
"Daemon { 0 } \t health = { 1 } ammo = { 2 } brain = { 3 } " .
Name, Health, Ammo, brain ):

}
public void ThinkO

// 4

{
Console.Write( Name + " i s " );
for ( int i = 0; i < brain; ++i ) Console.WriteC " thinking" );
Console.WriteLine(

);

}
int brain;

//

}
class Classl
{

static void MainO


{
Daemon Dima = new Daemon( "Dima", 3 )

// 5

Dima. Passport ();

// 6

Dima.ThinkO;

// 7

Dima.Health -= 10;

// 8

Dima.PassportO;

}
}
Daemon brain Think,
, Passport.
1

Monster Daemon .
:
Daemon Dima
Dima

is

health = 100 ammo = 100 brain = 3

thinking thinking thinking...

Daemon Dima

health = 90 ammo = 100 brain = 3

, Daemon
( 5 - 7 ) , ( 8)
. ,
8.1.
,
.
:
1

Monster , Daemon,
.

175


,
. Daemon.

, ,
, .
, ,
, .
, .

,

( ,
1 2 ) . base.
,
, base.

, ,

1

new . 8.1
Passport.

.
Passport Daemon ,

.
base, :
case.PassportO:


, .
, ,
, .
,
.

, , Passport, Daemon
, :
*ev public void PassportO
base.PassportO;
Console.WriteLineC " brain = { 1 } " , brain );

, .
(warning) , , ...

176

8.

, private,
. Passport name, health ammo
.
, protected,
, Monster.
.

,
, .

,
1

.
,
.
:

,
;

, ,
;

, ;
,
.

,
2


. 8.2 Monster
Monster Daemon.
8 . 2 .
using System;
namespace .ConsoleApplicationl
{
class Monster
{

}
class Daemon : Monster
1

,
. ,
. .NET 13.
, , ,
, .

177

{
. . . / / . 8.1

}
class Classl
{

static void MainO


{
const int n = 3;
Monster[] stado = new Monsterln];
stado[0] = new Monster( "Monia" );
s t a d o [ l ] = new Monster( "Monk" );
stado[2] = new Daemon ( "Dimon", 3 );
foreach ( Monster elem in stado ) elem.PassportO;

// 1

for ( int i = 0; i < n; ++i ) stado[i].Ammo = 0 ;


Console.WriteLineC);

111

foreach ( Monster elem in stado ) el em. PassportO;

// 3

}
}
}
:
Monster Monia
Monster Monk
Monster Dimon
Monster Monia
Monster Monk
Monster Dimon

health = 100 ammo = 100


health = 100 ammo = 100
health - 100 ammo = 100
health = 100 ammo = 0
health = 100 ammo = 0
health = 100 ammo = 0

: Daemon
, Monster,
, .
2, 1 3 , Passport,
.
, ,
,
. ,
, , .
: ,
, ,
( ).
,
(, stadoli ] .Ammo). ,

178

8.

,
.
, ,
,
, , ,
. #

, .


, ,
, .
, ,
, ',
,
.
, - ,
-. #
virtual. , :
virtual

public void Passport!)

...

virtual .
,
, ,
.

,
, ,
(Virtual Method Table.
V M T ) . (
) . .
V M T .
,
.
V M T ,
.
,
override, :
override public void PassportO

...


, .
, , ,
, .

179

8.2 virtual override


Passport, , ( 8.3).
8 . 3 .
using System;
namespace

ConsoleApplicationi

class Monster
{
virtual

public

void

PassportO

{
Console.WriteLineC "Monster { 0 } \t health = { 1 } ammo = { 2 } " ,
name, health, ammo );

}
}
class Daemon ; Monster
{
override public

void

PassportO

{
Console.WriteLineC
"Daemon {0} \t health = { 1 } ammo = { 2 } brain = { 3 } " ,
Name. Health, Ammo, brain );

}
}
class Classl
{

static void MainO


{
const int n = 3;
Monster[] stado = new Monster[n];
stado[0] = new MonsterC "Monia" );
s t a d o [ l ] = new MonsterC "Monk" ):
stado[2] = new Daemon ( "Dimon", 3 );
foreach ( Monster elem in stado ) elem.PassportO;
for ( int i = 0; i < n; ++i ) stado[i].Ammo = 0;
Console.WriteLineC):
foreach ( Monster elem in stado ) elem.PassportO;

}
}
}

108
.
.
. ,
S i n :
p u b l i c s t a t i c double Sin(

double a

):

,
,
. .
5.2 Demo
( , ,
) . , s ,
p r i v a t e ,
Gets, .
5 . 2 .
using

System;

namespace

ConsoleApplicationl

{
class

Demo

i
pub'ic

int

= 1 ;

p u b l i c c o n s t d o u b l e - 1.66;
s t a t i c s t r i n g s = "Demo";
double y;
p u b l i c double GetyO

//

{
return ;

}
p u b l i c v o i d SetyC d o u b l e y_ )

//

{
= _:

}
p u b l i c s t a t i c s t r i n g Gets

// s

{
return s;

class Classl
{
s t a t i c void MainO
{
Demo x = new Demo();
x.Sety(0.12):

//

180

8.

:
Monster Monia

health = 100 ammo = 100


health = 100 ammo = 100
health = 100 ammo = 100 brain = 3

Monster Monk
Daemon Dimon
Monster Monia

health = 100 ammo = 0

Monster Monk

health = 100 ammo = 0

Daemon Dimon

health = 100 ammo = 0 brain = 3

, 1 3 Passport,
, .


.
: ,
.
:
V M T , V M T ,
. ,
,
.

, ,
V M T ,
.


- .
,
.
.

. ,
.
, , ,
.

, -.
,
.

181



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

.
abstract.

,
,
. .

,
, :
abstract class S p i r i t

public

abstract

void

PassportO:

}
class Monster

Spirit

{
override public void PassportO
{
Console.Writel_ine( "Monster { 0 } \t health = { 1 } ammo = { 2 } " ,
name, health, ammo );

}
}
class Daemon : Monster

{
override public void PassportO
{
Console. Writel_ine(
"Daemon { 0 } \t health = { 1 } ammo = { 2 } brain = { 3 } " ,
Name, Health, Ammo, brain );

}
. . . // 12

8.

182

,
,
. , ,
, .
, .

.

.
.

foreach (. . 136)
, P r i n t A r r a y .
10.


# s e a l e d , , ,
, :
sealed

class

Spirit

}
//

c l a s s Monster :

Spirit {

...

s e a l e d .
, ,
, : .
, ,
, . ,
, ,
, .
, ,
. ,
,
. . 8.4.
8 . 4 . -
u s i n g System;
namespace

ConsoleApplicationi

{
class
{

p u b l i c v o i d )

183

object

{
Console.WriteLine( "!!" );

}
class
{

public ()
{

= new ;

= new ;

}
public void _()
{
.();
.);

}
, ;

}
class Classl
{

static void MainO


{
AH24_1 = new );
24_1.3_();
}

}
:
!!
!!
_ , ,
, .
, (is )
, -
(has ). ,
, ,
.

object
System.Object . N E T , #
object, .

.

184

8.

object
,
.
System.Object .

Equals true,
.
:
public virtual

bool

Equals( object obj

);

Equals true,
. :
public static bool

Equals( object obi,

object ob2 );

GetHashCode - ,
.
. Equals,
GetHashCode. -
( . . 291). :
public virtual

int GetHashCodeO;

GetType ,
, , .
. ,
1

.
:
public Type Get ;

ReferenceEquals

true,

. :
public static bool( object obi,

object ob2 );

ToString
, ,
. ,
. :
public

virtual

string ToStringO

. ,

Equals , -

( ) , ( ).
object Monster
8.5.
1

12 (. . 279).

185

object

8 . 5 . object
using System;
namespace ConsoleApplicationl
class Monster
{
public Monster( int health, int ammo, string name )

{
this.health = health;
this.ammo = ammo;
this.name

= name;

}
public override bool Equals( object obj )
{
if ( obj == null || GetTypeO != obj.GetTypeC) ) return false;
Monster temp = (Monster) obj;
return health == temp.health &&
ammo

== temp.ammo

name

== temp.name;

&&

}
public override int GetHashCode()
{
return name.GetHashCode();

}
public override string ToStringO
{
return string.Format( "Monster { 0 } \t health = { 1 } ammo = { 2 } " ,
name, health, ammo );

}
string name;
int health, ammo;

}
class Classl
{

static void Main()


{
Monster X = new Monster( 80, 80, "" );
Monster Y = new Monster( 80, 80, "" );
Monster Z = X;
if ( X == Y ) Console.WriteLine( X == Y " ) ;
else

Console.WriteLine(

X != Y " ) ;

&

186

8.

8.5

()

if ( X == Z ) Console.WriteLineC X == Z " ) ;
else

Console.WriteLineC X ! = Z " ) ;

if ( X.Equals(Y) ) Console.WriteLineC " X Equals Y " );


else

Console.WriteLineC " X not Equals Y " );

Console.Wri teLi ne(X.GetTypeC));

}
}
}
:
X != Y
X * Z
X Equals Y
ConsoleAppli cati onl.Monster
Equals .
nul 1 , ,
false. true
.
GetHashCode
. ToString ,
.
, ,
, Equals
.
,
( . . 203).



.

.
Y X , Y
X, , .

, Y . , X,
Y, (,
) . ,
1

, , .

187


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

.

, .
Y X Y X.
,
Y X ( Y X ) . ,
, X Y
, Y X, Y
X ( Y X ) .


,
.


.
, ,
1

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

[ ] [ ] interface _ [ : ]
[ ; ]
, new, publ i , protected, internal
private. new
, .
.
.
, ( i n t e r n a l ) .
1

, , 10.

189

,
.
, , .

,
, , , , ,
.

I Act ion,
, .
, ,
:
interface IAction

{
void Draw();
int Attack(int a ) ;
void D i e ( ) ;
int Power { get; }

}
IAction Power,
. ,
,
set, :
int Power { get; set; }
,
- .

-
, ,
. ,
,
( Health, Ammo Name
).
.

publ i
, ;

109
Console.WriteLineC x . G e t y O

);

C o n s o l e . W r i t e L i n e t Demo.Gets )
//

Console.WriteLinet GetsO

//
);

);

// s
//

}
}
}
,
. , s t a t i c ,
. ,
,

/
.


,
. :
1. , .
2. .
3. (
) .
4. .
5. , ;
v o i d , , .

.
. 5.3 .
5 . 3 .
. s i n g System;
-amespace C o n s o l e A p p l i c a t i o n i
class

Classl

{
s t a t i c i n t MaxCint a ,

i n t b)

/ /

{
if ( > b ) r e t u r n ;
else
r e t u r n b;

}
s t a t i c void MainO
{
i n t a = 2, b = 4 :
i n t x = Max( a, b );
C o n s o l e . W r i t e L i n e C x );

// Max
// : 4

190

9.

, ,
,
(
);

,
.

.NET ,
. ,
,
IComparable.
.


, ,
, .
, #
.
, .
, IAction Monster
:
using System:
namespace ConsoleAppl i c a t i o n l
{
interface IAction

{
void DrawO;
int Attack( int a ) :
void D i e O :
int Power { g e t ; }

}
class Monster :

IAction

{
public void DrawO
{
Console.WriteLineC " " + name ):

}
public int Attack( int ammo_ )
{
ammo -= ammo_;
if ( ammo > 0 ) Console.WriteLineC "-!" );
else

ammo = 0:

191
r e t u r n ;

}
public

void

OieO

{
Console.WriteLineC

" M o n s t e r " + name + " RIP"

);

h e a l t h = 0:

}
public

int

Power

I
ciet
{
r e t u r n ammo * h e a l t h ;

}
,
.
publ i c .
, 1:
Monster V a s i a = new MonsterC 5 0 . 5 0 ,

""

);

Vasia.DrawC):

// M o n s t e r
// :

I A r t i o n A c t o r = new MonsterC 10,

10,

"" );

Actor.DrawC);

//
// :

I Action
, .

, .
, :
s t a t i c void Act

IAction A )

{
A.DrawC);

)
s t a t i c v o i d Maine)
{
M o n s t e r V a s i a = new MonsterC 5 0 , 5 0 ,
ActC V a s i a

""

);

);

, .
, , ,
.

192

9.

:
.
.
, :
class Monster IAction
{
int IAction.Power
{
get
{
return ammo * health;

}
}
void IAction.DrawO
{
Console.WriteLineC " " + name );

}
}
IAction Actor = new Monster( 10, 10, "Mama" );
Actor.Draw();

//

// Monster Vasia = new MonsterC 50, 50, "" );


// Vasia.DrawO;

,
.
, -
.
,
,

1

. , , Monster :
, :
interface

ITest

{
void DrawO;

}
interface IAction
1

,
, .

193

void Draw();
int Attack( int a ) ;
void D i e ( ) ;
int Power { get;

lass Monster :

IAction,

ITest

void ITest.DrawO

{
Console.Writel_ine( "Testing " + name );

void IAction.DrawO
{
Console.WriteLineC " " + name );

Draw .
. ,
, :
Monster Vasia = new MonsterC 50, 50, "" );
:(ITest)Vasia).DrawO;

// :

;(IAction)Vasia).DrawO;

//:

Testing

, ,
( public),
:
slass Monster : IAction. ITest
public void DrawO
{
Console.WriteLineC " " + name ):

Draw, , :
Monster, IAction ITest.
,
,
.
.

9.

1 94


. is as

, .
is. ,
, is, , .
true,
, false .
:
1f ( is )

{
//

""

/'/

""

, -
object.
, ,
.
, , :
static vo'd Act( object A )

{
if ( A is IAction )

{
IAction Actor = ( I A c t i o n ) A;
Actor.Draw();
1

}
Act ,
, IAction.
is ,
: .
as.
, , null,
:
static void Act( object A )
I

IAction Actor = A as IAction;


if ( Actor != null ) Actor.Draw();

, .

195


-,
,
.
, . ,
, private internal,
(publ i c ) '.
,
, . -
,
.
new, .
.
# :
interface 1 Base
i
void F( int i ):

}
interface I l e f t

IBase

{
new void F( int i
interface I n g h t

);

// F

IBase

void G ( ) :
interface Iderived : I Left, I Right {}
class A
void Test( IDerived d ) {
d.F( 1 );

// I Left F

( ( I B a s e ) d ) . F ( 1 ):

// IBase.F

( ( I L e f t ) d ) . F( 1 );

//' I Left. F

v ( I R i g h t ) d ) . F ( 1 );

/7 IBase.F

F IBase ILeft,
IDerived IRight IBase .
, , ,
. ,

, .

196

9.

,
, :
class : IRight
{
IRight.GO { . . . }
IBase.F( int i ) { . . . }

// IRight.F( int i ) -

}
,
, , :
class :
{
//

IRight.GO { . . . }

}
class : A. IRight
{
IRight.GO { ... }
IBase.F( int i ) {

... }

II
//

}
, ,
.
new, .
, :
interface

IBase

{
void :

}
class Base : IBase
{
public void AO { . . . }

}
class Derived: Base
{
new public void AO { . . . }

}
Derived d = new Derived ( ) ;
d.AO;
IBase id = d;

// Derived.AO;

id.AO;

// Base.AO;

,
(
) :

197

interface

IBase

{
void ;

}
class Base : IBase
{
public virtual void AO {

...

}
class Derived: Base
{
public override void AO { . . . }

}
Derived d = new Derived ( ) :
d.AO;
IBase id = d;
id.AO;

// Derived.AO;
// Derived.AO;

, ,
.
: ,
, .
IBase
_, Base:
interface

IBase

{
void A O ;

class Base : IBase

{
void IBase.AO { A _ ( ) ; }
protected virtual void A _ ( ) { . . . }

class Derived: Base


protected override void A _ ( ) { . . . }

,
-,
.
:
interface

IBase

{
void ;

9.

1 98

}
c l a s s Base :

IBase

I
void IBase.A()

...

c l a s s D e r i v e d : Base,

i
i

p u b l i c v o i d AO

/ / D e r i v e d

IBase
...

,
,
, :
interface

Interfacel

void

FO:

}
class

Classl

p u b l i c void F()

...

p u b 1 ic v o i d GO

...

c l a s s Class-2

Classl.

Interfacel

new p u b l i c v o i d GO

...

j
Class2 C l a s s l F. I n t e r f a c e l
F. , Class2
, .

. ,
,
.

.NET
."NET ,
. , I C o m p a r a b l e
,
. l E n u m e r a b l e e n u m e r a t e

f o r e a c h , I C l o n e a b l e .

199

. , f o r e a c h
, A r r a y I E n u m e r a b l e I E n u m e r a t o r .
, ,
.

( IComparable)
I C o m p a r a b l e System.
CompareTo,
:
interface

IComparable

i n t CompareTo( o b j e c t o b j

0, ;

, ;

, .

IComparable M o n s t e r .
h e a l t h . 9.1
, ,
( , ,
).
9 . 1 . IComparable
j s i n g System;
'iamespace Consol eAppl i c a t i o n i
c l a s s Monster

IComparable

{
p u b l i c MonsterC i n t . h e a l t h ,

i n t ammo,

s t r i n g name )

{
this.health = health;
this.ammo
= ammo:
this.name
= name;

}
virtual

public

void

PassportO

{
Console.WriteLineC

" M o n s t e r { 0 } \t h e a l t h = { 1 }
name, h e a l t h , ammo );

ammo = { 2 } " ,

}
p u b l i c i n t CompareToC o b j e c t o b j

//

211

.NET

{
for ( int i = 0; i < n; ++i )
if ( mas[i] .GetTypeO .Name == "Monster" )
y i e l d return m a s [ i ] ;

}
public void Add( Monster m )
{
if ( n >= 10 ) return:
mas[n] = m;
++n:

class Classl
{

static void MainO

{
Stado s = new StadoO;
s.Add( new Monster() );
s.Add( new Monster("Bac") ):
s.Add( new Daemon() );
foreach ( Monster i in s )
foreach ( Monster i

in s.Backwards

i.PassportO
)

foreach ( Monster i in s.MonstersOnly()

i.Passport
) i.PassportO

}
, ,
.

, get ,
1

IEnumerable IEnumerator .
:

y i e l d return , ;

y i e l d break .

y i e l d
.
, .
-, MoveNext
,
y i e l d . MoveNext -
,
.

IEnumerable<T> IEnumerator<T>
System. C o l l e c t i o n s .Generic, 13.

9.

212

-\ . , ^ ^ ^ ,
:

^ ^\

crnvi

Q , a -fte , .\
\ , ,
, ;

,
;
,

( );

, # .

:
,
, . !
!
, , !

,
, .

[ ] [ ] struct [ : ]
_ [ ; ]
, ,
public, internal private ( ;
).
, , .
, , , , ,
, , .
, :
, :

,
' protected protected internal;

(abstract),
( s e a l e d ) ;

213

;
( o v e r r i d e )
, o b j e c t ;

t h i s ,
, ;

,
( ,
n u l 1 ) .
9.8 ,
.
. T o S t r i n g :
,
C o n s o l e . W r i t e L i n e .
.
9 . 8 .
u s i n g System;
namespace

ConsoleApplicati onl

i
s t r u c t Complex
{
p u b l i c double r e ,

im;

p u b l i c Complex( d o u b l e r e _ , d o u b l e i m _ )
re = r e _ ; im = i m _ ;

/ / t h i s . r e ,

p u b l i c s t a t i c Complex o p e r a t o r + ( Complex a.
r e t u r n new Complex(

a.re + b.re,

this.im

Complex b )

a . i m + b . i m );

public override string ToStringO


r e t u r n ( s t r i n g . F o r m a t ( " ( { 0 . 2 : 0 . # # } ; { 1 , 2 : 0 . # # } ) " . r e , im ) );

}
class
{

Classl
s t a t i c void MainO

{
Complex a = new Complex!

1.2345,

5 . 6 );

214

9.

9.8

()

Console.WriteLineC " = " + );


Complex b:
b.re = 10; b.im = 1;
Console.WriteLineC "b = " + b );
Complex = new ComplexO;
Console.WriteLineC "c = " + );
= a + b;
Console.WriteLineC "c = " + );
}

}
}
:

=
=
=
=

(1.23:5,6)
(10; 1)
( 0; 0)
(11,23:6.6)

,
. (
, . . 3 6 ) ,
, , ,
.

, , ,
.
.

ref out.
,
. , 100
101 ,
. , :
Complex [] mas = new Complex[4];
for ( int i = 0; i < 4; ++i )

{
mas[i].re = i;
mas[i].im = 2 * i;

}
foreach ( Complex elem in mas ) Console.WriteLine( elem );

215

Main 9.5,
:
(
(
(
(

0
1
2
3

0)
2)
4)
6)


,
.
,
, :
enum Menu { Read, W r i t e , Append, E x i t }
enum { , , ,

.
i n t , 0,
, :
enum Nums { two = 2, t h r e e ,

f o u r , t e n = 10, e l e v e n ,

f i f t y = t e n + 40 }:

t h r e e f o u r 3 4, e l e v e n 11.

, .

, ; ,
,
, .
:

[ ] [ ] enum _ [ : ]
_ [ ; ]
, ,
new, p u b l i c , p r o t e c t e d , i n t e r n a l p r i v a t e .
, .
i n t , ,
( c h a r ) , : b y t e , s b y t e , s h o r t , u s h o r t , i n t ,
u i n t , l o n g u l o n g . ,
i n t .
,
. ,
.
p u b l i c .

216

9.

,
- .
9.9.
9 . 9 .
using System;
namespace

ConsoleApplicationl

{
struct
{
public enum _
{
, , ,

}
public string ;
public _3 ;

}
class Classl
{

static void MainO


{
x;
x. = "";
x. = ._.;
Console.WriteLineC . + " " + . );

}
}
}
:

, :
enum Flags ; byte
{
, . 2, = 0x04, 4 = 0x08, 5 = 0x10, 6 = 0x20, 7 = 0x40



( + , -, ++, - - ) , ( * , &, |, ~ ) ,
( < , <=, >, >=, ==, ! = ) ( s i z e o f ) .

.
,

217

, , .
.
:
Flags = Flags.2 | Flags.4;
Console.WriteLineC " = {0}
{ 0 , 2 : } " , );
++;
Console.WriteLineC " = {0}
int = ( i n t ) ;

{ 0 . 2 : X } " . ):

Console.WriteLine( "x = {0}

{ 0 . 2 : X } " , x );

Flags b = (Flags) 65;


Console.WriteLine( "b = {0}

{ 0 . 2 : X } " , b );

( { 0 , 2 : } ):
= 10
= 11
= 11
D = 65

0
0

41

9.10.
9 . 1 0 .
using System;
namespace ConsoleApplicationi

{
struct
{
public enum _
{
, , , , ,

}
public string ;
public _ ;

}
class Classl
{

static void MainO


{
x;
x. = "
x. = ._.;
for ( int i = 1976; i < 2006; i += 5 )
{
if ( x. < ._. ) ++.;

218

9.

9.10

()
Console.WriteLineC

" : {0} {1} { 2 } " .


1 . . . .

1976
1981
1986
1991
1996
2001

System.Enum
# System.Enum,
.
GetName
, :
Console.WriteLineC Enum.GetName(typeof( Flags ) . 8 ) ) ;
// fc>4
Console.WriteLineC Enum.GetNameCtypeof( ._ ) , ! ) ) :
//

typeof (. 12).

GetNames GetValues , ,
, , :
Array names = Enum.GetNames( typeof(Flags) );
Console.WriteLineC " : " + names.Length );
foreach ( string elem in names ) Console.WriteC "
" + elem );
Array values = Enum.GetValuesC typeof(Flags) );
foreach ( Flags elem in values ) Console.WriteC "

" + (byte) elem );

IsDefined true,
, false
, :
if ( Enum.IsDefinedC typeof( Flags ), "5" ) )
Console.WriteLine( " 5 " );
else Console.WriteLine( " 5 " );
GetUnderlyingType ,
. , Flags System.Byte:
Console.WriteLine( Enum.GetUnderlyingType( typeof(Flags) ) );

219



. ,
, , ,
-.
-
, ,
.
#
.
.
, .
. N E T .

.
,
IComparable
IComparer. IEnumerable IEnumerator
foreach,
ICloneable .

.
,
, , .

,
.

, ,
. , ,
,
.

10
,


# ,
.
.

, .
, , , \
.
, .
.

, <
:

[ ] [ ] delegate ( [ ] )
, ,
new, public, protected, internal private.
,
, !

, .

10
,


# ,
.
.

, .
, , , \
.
, .
.

, <
:

[ ] [ ] delegate ( [ ] )
, ,
new, public, protected, internal private.
,
, !

, .

221

:
public delegate void D ( int i

);

, ,
void .

, , .
System.Delegate,
, . ,
.


,
, .
.
:

,
;

, ;

.
. 10.1 ,

(001 Hack).
1 0 . 1 .
using System:
namespace

ConsoleApplicationi

delegate void Del

( ref string s );

//

class Classl
public s t a t i c void C001 ( ref string s )

// 1

string temp = " " ;


for ( int i = 0: i < s.Length: ++i )

222

10. ,

10.1

()
if

( s [ i ] == ' ' 11 s [ i ] ' 0 ' ) temp += ' O ' ;

else i f ( s [ i ] = = ' I ' )

temp + = ' ;

else

temp += s [ i j ;

}
s = temp;

}
public static void Hack ( ref string s )

// 2

{
string temp = " " ;
for ( int i = 0; i < s.Length; ++i )
if ( i / 2 * 2 == i ) temp += char.ToUpper( s [ i ] );
else

temp += s [ i ] ;

s = temp;

}
static void MainO
{
string s = "cool
Del d;

hackers";
//

for ( int i = 0; i < 2; ++i )


{
d = new Del( COOl );

// 1

if ( i == 1 ) d = new Del(Hack);

// 2

d( ref s );

//

Console.WriteLine( s ) ;

}
:
cOOl hackers
COOl hAcKeRs
, .
,

, .

Combine,
System.Delegate, , ,
. Main
,
:

223

static void MainO


string s = "cool hackers";
Del d = new Del ( C001 ) ;
d += new Del( Hack );

//

d( ref s ) ;
Console.WriteLineC s );

// : C001 hAcKeRs

, ;

,
;

out
, ,
(
, voi d);

,
, ,
;

, ,

System. Null Ref erenceExcepti on.



.

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

, ,
.

224

10. ,


.
, .

(observer)
,
.
( 10.2),
.
O O P S ! .
.
1 0 . 2 .
using System;
namespace

ConsoleApplicationi

{
public delegate void Del( object );
class Subj
Del dels:

//
// -
//

public void Register( Del d )

//

dels += d:

public void OOPS

// -

Console.WriteLineC "OOPS!" ) :
if ( dels != null ) dels( this );

//

class ObsA
public void Do( object )

// -
//

Console.WriteLineC ", OOPS!" ):

class ObsB
public static void SeeC object )

// -
//

Console.WriteLineC " , OOPS!" ):

225

\
:lass Classl
{
static void MainO
{
= new SubjO;

//

ObsA ol = new ObsA();


ObsA o2 - new ObsAO;

//
//

s.Register( new Del( ol.Do ) );

//

s.Register( new Del( o2.Do ) );

//

s.Register( new Del( ObsB.See ) );

//

( )

s.OOPSO;

//

Subj s

}
}
, ,
.

( . :
, .
X .
: :
PS!
. OOPS!
, OOPS!
, OOPS!
object,
; . ,
( ).
.
,
Remove , :
jublic void UnRegister( Del d )
dels -= d;
}

//

226

10. ,

. ,

.
,
.

, , .


, :
Del dl = new Del( ol.Do ):

// o l . D o

Del d2 = new Del( o2.Do );

// o2.Do

Del d3 = dl + d2;

// o l . D o o2.Do

d3 += d l :

// o l . D o . o2.Do o l . D o

d3 -= d2;

// Ol.Do o l . D o

, , ,

.
, string, ,
,
.


,
. :
,
.
.

,
, .
.
(callback) ,
. . 10.1. ,
,
()
. ,
.

227

()

()-

/'

. 1 0 . 1 .

.
, W i n d o w s .
10.3.
,
.
1 0 . 3 .

using System:
namespace ConsoleApplicationl
{

public delegate double Fun( double x );

//

class Classl
{

public static void Tablet Fun F, double x, double b )


{
Console.WriteLineC "
X
Y
" );
while (x <= b)
{
Console.WriteLineC "| {0,8:0.000} | {1,8:0.000} |". x, F(x));
x += 1:
}
Console.WriteLineC "
" );
public static double SimpleC double x )
{
return 1:
}
static void MainO
{
Console.WriteLineC " Sin " );

10. ,

2 2 8

1 0 . 3 ()

Tablet new Fun( Math.Sin ), -2, 2 );


Console.WriteLineC " Simple " );
TableC new Fun( Simple ), 0. 3 );
}
}
}
:

Sin
X
Y
I
-2.000 I
-0,909
I
-1.000 I
-0,841
I
0.000 I
0,000
I
1,000 I
0.841
I
2,000 j
0,909

I
j
j
I

Simple
X
Y
I
0,000 I
1,000 I
I
1.000 I
1,000 I
I
2,000 I
1,000 j
I
3,000 I
1.000 I
Visual Studio 2005, 2.0 C # ,
.
,
, .

, ,
. 10.4
, 10.3.
1 0 . 4 . ( 2.0)

using System;
namespace ConsoleAppl icationi
{
public delegate double FunC double x );

//

class Classl
{
public static void TableC Fun F. double x, double b )
{
Console.WriteLine( "
while ( x <= b )

" ):

229

{
Console.WriteLineC "| {0.8:0.000} | {1,8:0.000} |", x. FCx)):
x += 1;
}

Console.WriteLineC "

" ):

static void MainO


{
Console.WriteLineC " Sin " ):
TableC Math.Sin. -2. 2 );

// 1

Console.WriteLineC " Simple " );


TableC delegate (double x ){ return 1: }, 0. 3 ); // 2
}
}

}
, Sin,
1

. ,
.

Simple, ,
.

.
, :
, .
,
.
10.5.
1 0 . 5 . -

using System;
namespace ConsoleAppli cationl
{
abstract class TableFun
{
public abstract double F( double x );
public void TableC double x, double b )
{
Console.WriteLineC "
while ( x <= b )
1

2005 #
,
1992 , .

&

230

10.5

()

Console.WriteLineC "| {0.8:0.000} | {1.8:0.000} |". , FCx));


+= 1;
}
Console.WriteLineC "
'

" );

}
class SimpleFun : TableFun
{
public override double F( double x )
{
return 1;
}
}

class SinFun : TableFun


{
public override double F( double x )
{
return Math.Sin(x);
}
}

class Classl
{
static void MainO
{
\ TableFun a = new SinFunO:
Console.WriteLineC " Sin " );
a.TableC -2. 2 ):
a = new SimpleFunO;
Console.WriteLineC " Simple " ):
a.TableC 0, 3 );
}

}
}
, , , ,
.


,
, . ,

. , ,

231

GetlnvocationList.
10.6, 10.1.
1 0 . .

using System;
namespace ConsoleApplicationi
{
delegate void Del ( ref string s );
class Classl
{
public static void C001 ( ref string s )
{
Console.WriteLineC " C001" );
string temp = "";
for ( int i = 0; i < s.Length; ++i )
{
if
( s[i] == 'o' || s[i] == 'O') temp += ' 0 ' ;
else if ( s[i] == ' 1 ' )
temp += ' ;
else
temp += s[i];
}

s = temp;
}

public static void Hack ( ref string s )


{
Console.WriteLineC " Hack" );
string temp = "";
for ( int i = 0; i < s.Length; ++i )
if ( i / 2 * 2 == i ) temp += char.ToUpperC s[i] );
else
temp += s[i];
s = temp;

public static void BadHack ( ref string s )


{
Console.WriteLineC " BadHack" );
throw new Exception();

static void MainO


{
string s = "cool hackers";
Del d = new Del C001 ) ;
d += new Del( BadHack );
d += new DelC Hack );

//

//
//
//

232

10. ,

10.6

()

foreach ( Del fun in d.GetlnvocationListO )


{
try
{
fun( ref s );

//

}
catch ( Exception )
{
Console.WriteLineC .Message );
Console.WriteLineC "Exception in method " +
fun.Method.Name);
}
}
Console.WriteLineC " - " + s );
}
}
}
:

COOl
BadHack
Exception of type System.Exception was thrown.
Exception in method BadHack
Hack
- COOl hAcKeRs
GetlnvocationList
Method. Method Info.
Method Info ,
, ,
.
12.

,
. ,
, -
. - .
,
, .

: , (sender) ,
, , ,
(receivers) , .

233

:
- .
:

, ;

(), .
1

[ ] [ ] event
new, public, protected, internal, private,
static, virtual, sealed, override, abstract extern,
. , ,
(static), ,
.
, .
:

public delegate void Del( object );


class

//

{
public event Del Oops;

//

}
- .
- ,
. ( !), ,
- .
, ,
. ,
, ,
+= - = ,
.

.
, ,
,
.

10.7 10.2,
.
1

.
, .

234

10. ,

1 0 . 7 .

using System;
namespace ConsoleApplicationl
{
public delegate void D e l O ;

//

class Subj
{

// -
//

public event Del Oops;


// ,
public
void CryOopsO
{
Console.WriteLineC "OOPS!" );
if ( Oops != null ) OopsO:
}

class ObsA
{
public void D o O ;

// -
//

Console.WriteLineC ", OOPS!" );


}

class ObsB
// -
{
//
public static void S e e O
{
Console.WriteLineC " , OOPS!" )
}
class Classl
{
static void MainO
{
Subj s = new Subj();

// '

ObsA ol = new ObsAC):


ObsA o2 = new ObsAC);

//

s.Oops += new DelC ol.Do );


s.Oops += new DelC o2.Do );
s.Oops += new DelC ObsB.See );

//

//
//

s. CryOopsO;

//

//

235

:
,
+= -=. void,
.
.
, , ,
, : ,
. . null. , CryOops
nul 1 ,

System.Nul1ReferenceException.
. N E T ,
.
:

EventHandler;

:
object;
EventArgs
.

,
, EventArgs,
.
, ,
System.EventHandler.
On .
10.8 10.7,
.NET. !
1 0 . 8 . EventHandler

using System;
namespace ConsoleApplicationi
{

class Subj
{
public event EventHandler Oops;
public void CryOops()
{
Console.WriteLineC "OOPS!" );
if ( Oops != null ) OopsC this, null );
}
}

class ObsA
^

&

236

10. ,

10.8

()

public void OnOopsC object sender. EventArgs e )


{
Console.WriteLineC ", OOPS!" );
}

}
class ObsB
{
public static void OnOopsC object sender, EventArgs e )
{
Console.WriteLineC " , OOPS!" ):

}
}

class Classl
{
static void MainO
{
Subj s = new SubjC);
ObsA ol = new ObsAC);
ObsA o2 = new ObsAC);
s.Oops += new EventHandlerC ol.OnOops ):
s.Oops += new EventHandlerC o2.0nOops ):
s.Oops +- new EventHandlerC ObsB.OnOops );
s. CryOopsO:
}
}
}
, # 2.0, ,

. 10.9.

, .
1 0 . 9 . ( 2.0)

using System;
namespace ConsoleApplicationl
{
class Subj
{
public event EventHandler Oops;
public void CryOopsO
{
Console.WriteLineC "OOPS!" )

237

if ( Oops != null ) Oops( this, null );


}

class ObsA
{
public void OnOopsC object sender, EventArgs e )
{
Console.WriteLineC ", OOPS!" );
}
}
class ObsB
{
public static void OnOopsC object sender, EventArgs e )
{
Console.WriteLineC " , OOPS!" ):

class Classl
{
static void MainO
{
Subj s = new SubjC);
ObsA ol ' new ObsAC);
ObsA o2 = new ObsAC);
s.Oops += ol.OnOops;
s.Oops += o2.0n0ops;

s.Oops += ObsB.OnOops;
s.Oops += delegate ( object sender, EventArgs e )
{ Console.WriteLineC " !" ); };
s.CryOopsO;
}
}
}
. N E T , ,
Windows.Forms, Windows-
. 14.


.NET .
.
() ,

238

10. ,

.
1

. (thread )
. ,
. Main.
,
.
.
.
,
.
, ,
.
:

,
, ;

,

(,
, ,
, , ,
).

Thread
. N E T
System.Threading.
. 10.1.
1 0 . 1 . System.Threading

Interlocked

,
,

Monitor

Mutex

- ,

ReaderWriterLock

, ,

Thread

, , ,

,
-, .
.

239

ThreadPool

Timer

WaitHandle

, ,

IOCompletionCallback

,
-
, ,

ThreadStart

TimerCallback

, ,
Timer

WaitCaback

, ThreadPool

ThreadPriority

ThreadState

.
Thread. - ,
, :

Thread t = new Thread ( new ThreadStartC _ ) );


,
. 10.10
.
1 0 . 1 0 .

using System:
using System.Threading:
namespace ConsoleAppl icationi
{
class Program
{
static public void HedgehogO

//

for ( int i = 0; i < 6: ++i )


{
Console.WriteLineC i ); Thread.SleepC 1000 );
}

}
static void MainO
{
Console.WriteLineC " " +
Thread.CurrentThread.GetHashCodeO );

240

10. ,

10.10

()

Thread ta = new ThreadC new ThreadStart(Hedgehog) );


Console.WriteLineC " " + ta.GetHashCodeC) );
ta.StartC);
for ( int i = 0; i > -6: --i )
{

.
Console.WriteC " " + i ); Thread.SleepC 400 );

}
}
}
:
1
2
0 0 -1 -2 1 -3 -4 2 -5 3 4 5
Sleep,
. ,
. ,
, ,
.
. 10.2 Thread.
1 0 . 2 . Thread

CurrentThread

(
)

IsAlive

true false ,

IsBackground

,
,

Name

Priority

/ (
ThreadPrority)

ThreadState

(
ThreadState)

Abort

ThreadAbortException.

GetData,
SetData

()

GetDomain,
GetDomainID

(
),

241

GetHashCode

Sleep

Interrupt

Join

Resume

Start

ThreadStart
Suspend

.
,

,
. 10.11.
1 0 . 1 1 . ,

using System;
using System.Threading;
namespace ConsoleApplicationi
{

class Classl
{
public void Do()
{
for ( int i - 0: i < 4; ++i )
{ Console.Write( " " + i ); Thread.SIeep( 3 ); }
}

}
class Program
{
static void MainO
{
Classl a = new ClassK);
Thread tl = new Thread( new ThreadStartC a.Do ) );
tl.Name = "Second
Console.WriteLineC " " + tl.Name );
tl.StartO;
Thread t2 = new ThreadC new ThreadStartC a.Do ) );
t2.Name = "Third":
Console.WriteLineC " " + t2.Name );
t2.Start();
}
}
}

242

10. ,

:
Second
Third
0 0 1 1 2 2 3 3
,
.

, lock. :

lock ( ) _
, .
this,
typeof (). ,
.
, Do
:
public void Do()
{
lockC this )
{
for ( int i = 0; i < 4; ++i )
{ Console.Write( " " + i ): Thread.SIeep( 30 ): }
}
}
:
Second
Third
0 12 3 0 12 3

,
, Beginlnvoke Endlnvoke.
Beginlnvoke
,
.
.

Beginlnvoke ,
.
, AsyncCal 1 back. ,

Endlnvoke.
'j

243

Beginlnvoke,
End Invoke , .
10.11 ,
.
Visual Studio .
Factorizer Factorize,
. : Numl
Beginlnvoke, Num2
End Invoke.
1 0 . 1 1 .

using System:
using System.Threading;
using System.Runtime.Remoting.Messaging;
//
public delegate bool AsyncDelegate ( int Num. out int ml, out int m2 );
// ,
public class Factorizer
{
public bool FactorizeC int Num. out int ml, out int m2 )
{
ml = 1; m2 = Num;
for ( int i = 2; i < Num; i++ )
if ( 0 == (Num % i) ) { ml = i; m2 = Num / i; break; }
if (1 == ml ) return false;
else
return true;
}

}
// ,
public class PNum
{
private int Number;
public PNum( int number ) { Number = number; }
[OneWayAttributeO]
// ,
public void Res( IAsyncResult ar )
{
int ml, m2;
// AsyncResult
AsyncDelegate ad = (AsyncDelegate)((AsyncResult)ar).AsyncDelegate;
// Factorize
ad.EndInvoke( out ml, out m2, ar );

&

244

10. ,

10.11

()

II
Console.WriteLineC " : {0} : {1} {2}",
Number, ml. m2 );
}
}

//
public class Simple
{
// 1:
public void NumlO
{
Factorizer
f = new FactorizerO;
AsyncDelegate ad = new AsyncDelegate ( f.Factorize );
int Num = 1000589023, tmp;
// ,
// Factorize
PNum n = new PNum( Num );
//
AsyncCallback callback = new AsyncCallbackC n.Res ):
// Factorize
IAsyncResult ar = ad.BeginlnvokeC
Num, out tmp, out tmp, callback, null );
//

// -
// ...
}
// 2:
public void Num2()
{
Factorizer
f = new FactorizerO;
AsyncDelegate ad = new AsyncDelegate ( f.Factorize ):
int Num = 1000589023. tmp;
// ,
// Factorize
PNum n = new PNum( Num );
//
AsyncCallback callback = new AsyncCallbackC n.Res );
// Factorize
IAsyncResult ar = ad.BeginlnvokeC
Num, out tmp, out tmp, null, null );
//
ar.AsyncWaitHandle.WaitOne( 100.00. false );

245

if ( ar.IsCompleted )
{
int ml. m2;
// Factorize
ad.Endlnvoket out ml, out m2. ar );
//
Console.WriteLine( " : {0} : {1} {2}",
Num, ml. m2 );
}
}

public static void MainO


{
Simple s = new SimpleO;
s.NumlO;
S.Num2();

}
}
:

: 1000589023 : 7 142941289
: 1000589023 : 7 142941289

[OneWayAttributeO]
.


. N E T ,
,
Windows.
,
, , , .
+ + ,
.
,
.
2.0, # ,

.

.
,
,
.

11


, .
,
, , ,
.
, , , .
- # -
.NET.
. .
(stream)

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

.
1

, . ,
!

247

. N E T ,
. 11.1.
System. 10.
.

. 1 1 . 1 . .NET
,
.
. 11.1.
1 1 . 1 . System.

BinaryReader,
BinaryWriter


(, , . .)

BufferedStream

(,
)

Di rectory,
Directorylnfo, File,
Filelnfo

: , ,
. Fi 1 Di rectory
.
Directorylnfo Filelnfo

FileStream

() ,

MemoryStream

&

1 1 .

248

11.1

()

StreamWriter,
StreamReader


( )

StringWriter,
StringReader

,
:

(BinaryReader, BinaryWriter);

(FileStream);

, (StreamWriter, StreamReader).

. N E T Unicode,
. , , ,
,
.
,
,
.
, .
,
()
, , , ()
.
,
.

.

,
, #
.
Convert Parse, - (. . 59).

, !
, , ]
ToString,
.
. N E T XmlTextReadf
XmlTextWriter, '
X M L . X M L 15.

249

.
:
1. .
2. (-).
3. .
,

.
, ,
.
FileAccess, System. 10.
. 11.2.
1 1 . 2 . FileAccess

Read

ReadWrite

Write

FileMode
(. 11.3).
1 1 . 3 . FileMode

Append

, ,
. ,

Create

.
,

CreateNew

.
, IOException

Open "

OpenOrCreate

, . ,

Truncate

FileMode.Append
FileAccess.Write, , .

FileShare (. 11.4).

250

1 1 .

1 1 . 4 . FileShare

None

Read


. ,

ReadWrite

Wri te


- Fi I eStream,
Stream,
. Stream . 11.5.
1 1 . 5 . Stream

BeginRead,

BeginWrite
CanRead,
CanSeek,

, , : ,
/

CanWrite
ose

(,
. .)

EndRead,

EndWrite
Flush


. ,

Length

Position

Read,
ReadByte

( )

Seek

SetLength

Wri te,
WriteByte

( )

251

FileStream .

FileMode, FileAccess FileShare.
. 11.211.4. 11.1 .
,
.
1 1 . 1 .

using System;
using System.10;
namespace ConsoleApplication1
{
class Classl
{
static void MainO .
{
FileStream f = new FileStream( "test.txt",
FileMode.Create, FileAccess.ReadWrite );
f.WriteByte( 100 );

II 100

byte[] = new byte[10];


for ( byte i = 0; i < 10; ++i )
{

x[1] = (byteM 10 - i );
f.WriteByte(i);

// 10 0 9

f.WriteC x, 0, 5 );

// 5

byte[] = new byte[20];


f.Seek( 0, SeekOrigin.Begin );
f.Read( ,. 0. 20 );

// -
//

foreach byte elem in ) Consol.WriteC " " + elem );


Console.WriteLineC);
f.Seek(5, SeekOrigin.Begin); // - 5-
int = f.ReadByteO;
// 5-
Console.WriteLineC );
= f.ReadByteO;
// 6-
Console.WriteLineC );
Console.WriteLineC " " + f.Position );
f.CloseO;
}

}
}

252

1 1 .

100 0 1 2 3 4 5 6 7 8 9 1 0 9 8 7 6 0 0 0 0
4
5
7

( , Append)
.
Seek,
: ,
. SeekOrigin:
Begin, Current End.
.
, ,
(. . 30), :

FileStream f = new FileStreamC @"D:\CJ\test.txt",


FileMode.Create, FileAccess.ReadWrite ):
.
, ,

, .

, , :

FileNotFoundException,
;

DirectoryNotFoundException, ;

Argument Except ion, ;

IOException, - -.

.
,

.
, :

try

{
FileStream f = new FileStreamC @"d:\C#\test.tx",
Fi1eMode.Open. FileAccess.Read );
//
f.CloseO;
}
catchC FileNotFoundException e )

253

{
Console.WriteLineC .Message );
Console.WriteLineC " !" );
return;

}
catch( Exception e )
{
Console.WriteLineC "Error: " + e.Message );
return;
}
, ,
, , .
,
, . ,
, Flush.

Stream (, , FileStream)
-: .
,
-.
- ,
1

. .
-
,
. BeginRead.
, ,
, , .
,
, ,
.
EndRead, .
. 11.2

.

,
, .

254

1 1 .

1 1 . 2 .

using System;
using System.10;
using System.Threading;
namespace ConsoleApplicationl
{
class Demo
{
public void UserlnputO

//

{
string s;
do
{ Console.WriteLineC " . Enter " );
s = Console.ReadLineO;
} while (s.Length != 0 );
}
public void

OnCompletedReadC IAsyncResult ar )

.// 1

{
int bytes = f.EndRead( ar );
Console.WriteLineC " " + bytes );
}
public void AsyncReadO
{
f = new FileStreamC "D:\\verybigfile". FileMode.Open.
FileAccess.Read, FileShare.Read. buf.Length, true );
callback = new AsyncCallbackC OnCompletedRead ):
f.BeginReadC buf, 0. buf.Length, callback, null );
}
FileStream f;
byte[] buf = new byte[66666666];
AsyncCallback callback;
}
class Program
{
static void MainO
{
Demo d = new DemoO;
d. AsyncReadO;
d.UserlnputO;
}

}
}

111
1/3
// 4

255


Demo.
OnCompletedRead ( 1)
IAsyncResult, ,
EndRead.
, true
( 2 ) . 3
AsyncCallback,
OnCompletedRead.
OnCompletedRead 'BeginRead (
4 ) , ,
. OnCompletedRead
.
verybigfile , Userlnput
, OnCompletedRead.

, 11.2,
BeginRead EndRead,
, . .

StreamWriter StreamReader Unicode- ,


, ,
.
TextWriter TextReader ,
. . 11.6 11.7
. ,
.
1 1 . 6 . TextWriter

Close

.
,

Fl ush

System.
Text.Encoding.

&

256

1 1 .

11.6

{)

NewLine

,
.
(\\)

Write

Wri teLi

1 1 . 7 . TextReader

Peek

Read

ReadBlock

ReadLine


stri ng. (nul 1) (EOF)

ReadToEnd

, ,
stri ng

, :

, , ,
.
11.3 , .

.
. ,
. ...\ConsoleApplication1\bin\Debug.
1 1 . 3 .
using System;
using System.10;
namespace ConsoleApplicationl
{

class Classl
{

static void MainO


{
try
{
StreamWriter f = new StreamWriterC "text.txt" );
f.WriteLineC " :" );
double = 12.234;

257

int

b = 29:

f.WriteLineC " a = {0.6:C}

b = { 1 . 2 : X } \ a. b );

f.CloseO;

catch( Exception e )
{
Console.WriteLineC "Error: " + e.Message );
return;
}

}
11.4 , , .
1 1 . 4 .
using System;
using System.10;
namespace ConsoleApplicationi
{
class Classl
{
static void MainO
{
try
{
StreamReader f = new StreamReaderC "text.txt" );
string s = f.ReadToEndO;
Console.WriteLine(s);
f.CloseO:

catchC FileNotFoundException e )
Console.WriteLineC e.Message );
Console.WriteLineC " !" ):
return;

catch( Exception e )
Console.WriteLineC "Error:
return;

+ e.Message );

258

1 1 .


ReadToEnd. ,
11.5. .
1 1 . 5 .
using System;
using System.10;
namespace ConsoleApplicationl
{

class Classl
{

static void MainO


{
try
{
StreamReader f = new StreamReaderC "text.txt" );
string s;
long i = 0:
while ( ( s = f.ReadLineO ) != null )
Console.WriteLineC " { 0 } : { 1 } " . ++i, s ) ;
f.CloseO;

}
catch( FileNotFoundException e )
{
Console.WriteLineC e.Message );
Console.WriteLineC " !" );
return;
}
catch Exception e )
{
Console.WriteLineC "Error: " + e.Message );
return;
}
}

}
}
, , ]
11.6. *
.
: :
,
, *

.

259

1 1 . 6 .
using System;
using System.10;
namespace ConsoleApplicationi
{
class Classl
{
static void MainO
{
try
{
StreamReader f = new StreamReaderC "numbers.txt" );
string s:
const int n = 20;
i n t [ ] a = new int[n];
string[] buf;
while ( s = f.ReadLineO ) != null )
{
buf = s.SplitO ' ) ;
long sum = 0;
for ( int i = 0; i < buf.Length; ++i )
{
a [ i ] = Convert.ToInt32( b u f [ i ] ) ;
sum += a [ i ] ;
}
Console.WriteLineC "{0}

; { 1 } 0 s. sum );

}
f.CloseO;

}
catch( FileNotFoundException e )
{
Console.WriteLineC e.Message );
Console.WriteLineC " !" ):
return;

}
catch Exception e )
{
Console.WriteLineC "Error; " + e.Message ):
return;
}
}

}
}
:
12 4 : 7
3 44 -3 : 50
8 11 : 10

260

1 1 .


,
, .
, .
BinaryWriter ,
.
,
FileStream.
.
. 11.8 11.9.
1 1 . 8 . BinaryWriter

BaseStream

, BinaryWriter

Close

Flush

Seek

Write

1 1 . 9 . BinaryReader

BaseStream

, BinaryReader

Close

PeekChar



'

Read

ReadXXXX

(,
ReadBoolean, ReadByte, Readlnt32 . .)

11.7 .
,
8888.
1 1 . 7 .
using System;
using System.10;
namespace ConsoleApplicationl
{
class Classl
{

static void MainO

I
j
I

try
{
BinaryWriter tout = new BinaryWriter(
new FileStreamC @"D:\C#\binary", FileMode.Create) );
double d = 0;
while ( d < 4 )
{
fout.WriteC d );
d += 0.33;

}:
fout.Seek( 16. SeekOrigin.Begin );

//

fout.Write( 8888d );
fout.CloseC);
}
catchC Exception e )
{
Console.WriteLineC "Error: " + e.Message );
return:
}
}

}
.

doubl 8 .

, , 11.8
, BinaryReader
, .
, ReadDoubl
EndOfStreamException.
, .
1 1 . 8 .
using System;
using System.10;
namespace ConsoleAppl icationi
{

class Classl
{

static void MainO


{

try

262

1 1 .

1 1 . 8 {)

'{
FileStream f =
new FileStreamC @"D:\C#\binary", Fi1eMode.Open );
BinaryReader fin = new BinaryReaderC f );
long n = f.Length / 8;
doublet] = new double[n]:

//

long i = 0:
try

{
whileC true ) x[i++] = fin.ReadDoubleO;

//

}
catch ( EndOfStreamException e ) {}
foreachC double d in x ) Console.WriteC "

" + d ); //

fin.CloseO;
f.CloseO;

catch ( FileNotFoundException e )
Console.WriteLineC e.Message );
Console.WriteLineC " !" );
return;

catch ( Exception e )
Console.WriteLineC "Error: " + e.Message );
return;
}
}
}
:
0

0,33

8888

0,99

1.32

1,65

1,98

2,31

2,64

2,97

3,3

3,63

3,96

-
,
.
Console,
System.

263

: Console. In TextReader Consol . Out


Console.Error TextWriter.
, ,

Set In SetOut (
<, > ) .
,
Console Read, ReadLine, Write WriteLine,
In, Out Error.
,
. ,
,
.


System. 10 ,
: Directory, File,
Directorylnfo Filelnfo. , ,
, .
Directory File .
Directorylnfo Filelnfo ,
. Directorylnfo
Filelnfo FileSystemlnfo,
, . 11.10.
1 1 . 1 0 . FileSystemlnfo

Attributes


.
FileAttributes

CreationTime

Exists

Extension

Full Name

LastAccessTime

LastWriteTime

Name

. .
,
. ,

264

1 1 .

Directorylnfo ,
. . 11.11
1 1 . 1 1 . Directorylnfo

Create,
CreateSubDirectory

Delete

GetDirectories

GetFiles


Filelnfo

MoveTo

Parent

11.9 , ,
.
1 1 . 9 . Directorylnfo
using System;
using System.10;
namespace ConsoleApplicationl
{

class Classl
{

static void DirlnfoC Directorylnfo di )


{

//
===== Directory Info
):
Console.WriteLineC
+ di.FullName );
Full Name: "
Console.WriteLineC
+ di.Name );
Name: "
Console.WriteLineC
+ di.Parent ) ;
Parent: "
Console.WriteLineC
+ di.CreationTime );
Creation: "
Console.WriteLineC
+ di.Attributes ) ;
Attributes:
Console.WriteLineC ,
Root: "
+
di.Root );
Console.WriteLineC
);
=====
Console.WriteLineC

}
static void MainO
{
Directorylnfo di1
Directorylnfo di2

new DirectoryInfo( @"c:\MyDir" );


new DirectorylnfoC @"c:\MyDir\temp" );

try
{
//
dil.CreateO:

265

di2.Create();
//
Dirlnfo(dil):
DirInfo(di2);
//
Console.WriteLineC " { 0 } . " , dil.Name ) :
dil.DeleteO;
}
catch Exception )
{
Console.WriteLineC " " );
}
}
}

}
:
===== Directory Info =====
Full Name: c:\MyDir
Name: MyDir
Parent:
Creation: 30.04.2006 17:14:44
Attributes: Directory
Root: c:\
===== Directory Info =====
Full Name: c:\MyDir\temp
Name: temp
Parent: MyDir
Creation: 30.04.2006 17:14:44
Attributes: Directory
Root: c:\
MyDir.

, . ,
Delete ,
, :
dil.DeleteC true );

//

Attributes. ,
FileAttributes, . 11.12.

266

1 1 .

1 1 , 1 2 . FileAttributes

Archive


Compressed

Directory

Encrypted

Hidden

Normal

,
.

Offline

, ,
. ,

Readonly

System

11.10 Filelnfo
jpg d:\foto d:\temp. Exists
, .
1 1 . 1 0 .
using System;
using System.10;
namespace Consol eApplicationi

{
class Classl
{
static void MainO
{
try
{
string DestName = @"d:\tempV;
Directorylnfo dest = new DirectoryInf( DestName ):
dest.CreateO;
//
Directorylnfo dir = new DlrectorylnfoC @"d:\foto" );
if ( ! dir.Exists )

//

{
Console.WriteLineC " " +
dir.Name + " " );
return;
}
FilelnfoD files = dir.GetFiles( "*.jpg" ); //

267

()

foreachC Filelnfo f in files )


f.CopyToC dest + f.Name );

//

Console.WriteLineC " " +


fi1es.Length + " jpg-" );

}
catch ( Exception e )
{
Console.WriteLineC "Error: " + e.Message );

}
}

}
}
Fi 1 e Di rectory , ,
, , .

()
#
, . ,
.
.
,
, , ,
.
, ,
[Serial izable].
12, ,
, . ,
, [NonSerialized], :
[Serializable]
class Demo
{
public int a = 1:
[NonSerialized]
public double y:
public Monster X. Y;

}
: S O A P (
X M L - ) .
System.Runtime.Serialization.Formatters.Binary,
System.Runtime.Seri alizati on.Formatters.Soap.

268

1 1 .

.
BinaryFormatter, :
Serialize( , );
DeserializeC ):
Serialize , Deserialize
.
11.11 Demo
Demo.bin. , , ,
Visual Studic.NET.
1 1 . 1 1 .
using System:
using System.10;
us i ng System.Runti me.Seri a1i zat i on.Formatters.Bi na ry;
namespace ConsoleApplicationi

{
[Serializable]
abstract class Spirit
{
public abstract void PassportO;

}
[Serializable]
class Monster : Spirit

{
public Monster( int health, int ammo, string name )
{
this.health = health;
this.ammo

- ammo;

this.name

= name:

}
override public void PassportO
{
Console.WriteLineC "Monster { 0 } \t health = { 1 } ammo = { 2 } " ,
name, health, ammo );

}
string name;
int health, ammo;

}
[Serial izable]
class Demo
{
public int a = 1;

269

()

[NonSerialized]
public double b;
public Monster X, Y;
}
class Classl
{

static void MainO


{
Demo d = new DemoO;
d.X = new Monster( 100. 80. "" );
d.Y = new MonsterC 120, 50, "" );
d.a - 2;
d.b = 2;
d.X. PassportO;
Console.WriteLineC d.a );

d. Y. PassportO;
Console.WriteLineC d.b );

FileStream f = new FileStreamC "Demo.bin", FileMode.Create );


BinaryFormatter bf = new BinaryFormatterO;
bf.Serialize( f, d );

// d f

f.CloseO:
}

}
.
,
. :
Monster
Monster
2
2

health = 100 ammo = 80


health = 120 ammo = 50

, :
1. System.Runtime.Serialization.
Formatters.Binary.
2. [Serializable].
3.
.
4. BinaryFormatter.
5. .
6. .
11.12 .

270

1 1 .

1 1 . 1 2 .
using System;
using System.10;
using System.Runtime.Serialization.Formatters.Binary;
namespace Consol eAppl icationi

{
class Classl
{
static void MainO
{
FileStream f = new FileStream( "Demo.bin", FileMode.Open );
BinaryFormatter bf = new BinaryFormatterO;
Demo d = (Demo) bf.DeserializeC f );

//

d.X. PassportO;

d.Y. Passport ;

Console.WriteLineC d.a );

Console.WriteLineC d.b );

f.CloseO;
}
}
}
:
Monster

health = 100 ammo = 80

Monster

health = 120 ammo = 50

2
0
, .
, ,
.

SOAP
SoapFormatter. ,
ISeri al i zabl e
.



, , , ,
.
, .NET.

271


, .
:
, .
, Unicode.
,
.
. ,
.
.
#
.
Convert Parse.
ToString,
.
,
,
, , .

.
() [Serializable].
: S O A P (
XML-).

12
, ,
,
.
,

.
,
, .
.

.NET
dll, , ,
(. 12.1). 1 (. . 9 ) ,
.

IL
( )
. 1 2 . 1 . ,

273

(Intermediate Language, I L ) ,

,
:
1 ,
C L R ;
2 , .NET-
.
[L- ILDasm.exe,
...\SDK\bin\ Visual Studio.NET. ILDasm . N E T dll
; File Open.
, .
,
.

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

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

,
.

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

274

12. , , ,

, ;

, (,
);

(strong name) ,
;

, ;
,
( . N E T . [27]).

.

.
:
, :

(major version);

(minor version);

(build number);

(revision number).

,
. ,
3.1.0.0, 3.4.0.0,
, ,
.
, .
, ,
(patch).
,
[AssemblyVersion],
. 285.
,
.
.
, , ,
, , . .
.
Visual Studio.NET
.

, .
, , .

275

,
(Global Assembly Cache, G A C ) .
(strong name),
.

"'


Visual Studio.NET
Class Library ( ). 8
.
, dll.
MonsterLib (. 12.2).

. 1 2 . 2 .
12.1. 8
public ,
.
1 2 . 1 .

namespace MonsterLib
{
using System;
public abstract class Spirit
{
public abstract void PassportO;

&

276

12 . , , ,

12.1

()

public class Monster : Spirit


{
public Monster'O
this.health = 100;
this.ammo = 100:
this.name = "Noname";

public MonsterC string name ) : thisO


{
this.name = name;

}
public Monster( int health, int ammo, string name )
{
this.health = health;
this.ammo = ammo;
this.name = name;

}
public int Health
{
get
{
return health;

}
set
{
if ( value > 0 ) health = value;
else
health -= 0;

}
}
public int Ammo
{
get
{
return ammo;

}
set
{
if ( value > 0 ) ammo = value;
else
ammo = 0;

}
}
public string Name
{

277

pet

return inSTfe ,
-

}
}
override public void PassportO
{
Console.WriteLineC "Monster {0} \t health = {1} ammo = { 2 } " ,
name, health, ammo );

}
string name;
int health, ammo;

}
public class Daemon : Monster
{
public Daemon()
brain = 1 ;
public DaemonC string name, int brain ) : baseC name )
this.brain = brain;

public DaemonC int health, int ammo, string name, int brain ) :
baseC health, ammo, name )
this.brain = brain;

override public void PassportO


Console.WriteLineC
"Daemon {0} \t health = {1} ammo = {2} brain = { 3 } " .
Name, Health, Ammo, brain );

public void ThinkO


Console.WriteC Name + " is" );
for ( int i = 0; i < brain; ++i ) Console.WriteC " thinking" );
Console.WriteLineC " . . . " ) ;

int brain;

278

12. , , ,

, MonsterLib.dll ...\bin\
Debug ...\obj\Debug. MonsterLib.dll ILDasm.exe,
(. 12.3).

. 1 2 . 3 . ILDasm.exe

, .
, ,
(. . 178),
MonsterLib.dll. ,
( , )
Project Add Reference ( ).
, , Browse.

, .
12.2.

279

1 2 . 2 .
using System; namespace ConsoleApplicationi

{
using MonsterLib:
class Classl
{

static void MainO


{
const int n = 3;
MonsterC] stado = new Monster[n]:
stado[0] = new MonsterC "Monia" );
stadoll] = new MonsterC "Monk" );
stado[2] = new Daemon ( "Dimon", 3 );
foreach ( Monster elem in stado ) elem. PassportO;
for ( int i = 0; i < n; ++i ) stado[i].Ammo = 0;
Console.writeLineO;
foreach ( Monster elem in stado ) elem.PassportO;

}
}
8.3.
...\bin\Debug ,
MonsterLib.dll, ,
. ,
, .
,
.

.NET ,
, .
, , ,
VB.NET.


. , ,
, . .
.
System. System.Reflection.

280

12. , , ,

,
. System.Reflection , ,
,
.
. 12.1.
1 2 . 1 .

IsAbstract, IsArray,
IsNestedPublic, IsClass,
IsNestedPrivate, IsCOMObject,
IsEnum, Islnterface, IsPrimitive,
IsSealed, IsValueType

,

(,
, ,
. .).

GetConstructors, GetEvents,
GetFields, Get Interfaces,
GetMethods, GetMembers,
GetNestedTypes, GetProperties

,
(,
, . .).
, , GetFields
Fieldlnfo, GetMethods
Method Info.
( S ),

(,
GetMethod GetMethods)

FindMembers

Member Info

GetType

InvokeMember

.
,
new , :
1. object GetType,
, .
, :
Monster X = new MonsterC);
Type t = X.GetTypeO;
2. Type GetType
, (), :
Type t = Type.GetTypeC "Monster" );
3. typeof ,
, :
Type t = typeof Monster );

281


.
. 12.1,
(, Memberlnfo).
System. Ref lection. . 12.2.
1 2 . 2 . System.Reflection

Assembly

AssemblyName

(, ,
, . .)

EventInfo

Fieldlnfo

,
Eventlnfo, Fieldlnfo, Methodlnfo Propertylnfo

Memberlnfo
Methodlnfo

Module

Parameterlnfo

Propertylnfo

12.3
12.1.
1 2 . 3 .
using System;
using System.Reflection;
namespace ConsoleApplicationl

{
using MonsterLib;
class Classl
{

static void InfoC Type t )


{
Console.WriteLineC " = = = = " + t.FullName ) ;
if ( t.IsAbstract ) Console.WriteLineC "" );
if ( t.IsClass
) Console.WriteLineC "" );
if ( t.IsEnum
) Console.WriteLineC "" );
Console.WriteLineC " " + t.BaseType );
MethodInfo[] met = t.GetMethodsO;
foreach ( Methodlnfo m in met ) Console.WriteLineC m );

Console.WriteLineC):

282

12. , , ,

12.3

()

PropertyInfo[] prs = t.GetPropertiesO;


, foreach ( PropertyInfo p in prs ) Console.WriteLineC p );
Console.WriteLineC);

}
static void MainO
{
Type t = typeof( Spirit );
InfoC t );
t = typeof( Monster ):
InfoC t );
t = typeof( Daemon );
InfoC t );

}
}
:
======= MonsterLib.Spirit

System.Object
Void PassportO
Int32 GetHashCodeO
Boolean Equals(System.Object)
System.String ToStringO
System.Type Get Type
======= MonsterLib.Monster

MonsterLib.Spirit
Void PassportO
Int32 GetHashCodeO
Boolean Equals(System.Object)
System.String ToStringO
Int32 get_Health()
Void set_Health(Int32)
Int32 get_Ammo()
Void set_Ammo(Int32)
System.String get_Name()
System.Type GetType()
Int32 Health
Int32 Ammo
System.String Name

283

======= MonsterLib.Daemon

MonsterLib.Monster
Void PassportO
Int32 GetHashCodeO
Boolean Equals(System.Object)
System.String ToStringO
Void Think
Int32 getJealthO
Void set_Health(Int32)
Int32 get_Ammo()
Void set_Ammo(Int32)
System.String get_Name()
System.Type GetTypeO
Int32 Health
Int32 Ammo
System.String Name
, ,
. , .
, .

(,
, . .).
.
System. Attribute.
. .NET
,
. ,
,
, , .
() ,
,
.
, .
.
,
, . :
[Serializable]
class Monster

284

12. , , ,

[NonSerialized]
string name;
int health, ammo;

}
[Serializable], ,
, Monster. name
[NonSerialized], ,
. 10.
, ,
.

, .
, ,
. 12.3.
1 2 . 3 . ,

assembly

field

event

method

pa ram

property

return

type

, , ABC:
[ABC]
public void Do()

{ ... }

. ,
, , :
[return:ABC]
public void Do()

{ ... }

.
.
= ,
. ,
CLSCompliant true. ,

285

,
using, :
using System;
[assembly:CLSCompliant(true)]
namespace ConsoleApplicationl
{ ...
[CLSCompliant] ,
C L S (Common Language Specification) .
, , ,
, () .
,
, , ,
.
[STAThread],
, , .
, ,
. ,
["Conditional!, .
Assemblylnfo.cs,
.
[AssemblyVersion], :
[assembly: AssemblyVersion("1.0.0.0")]
.
[27].


,
.
:

,
;

, .

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

286

12. , , ,

, :
namespace State
{
namespace City
{

}
}
:
namespace State.City
{

}
, ,
.NET.
, -
:
1. . ,
System.Runtime.Serialization.Formatters.Binary BinaryFormatter.

:
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bf =
new System.Runtime.Seri ali zati on.Formatters.Bi nary.Bi naryFormatter();
2. using, ;
. ,
using System.Runtime.Serialization.Formatters.Binary;
BinaryFormatter bf = new BinaryFormatterO;

using , ;
.

3. .
using:
using BinF =
System.Runtime.Seri ali zati on.Formatters.Bi nary.Bi naryFormatter;
BinF bf .= new BinFO;

,
, ,
.

287

# 2.0 : : , :
SI0 = System.10:
ig MIO = MyLibrary.IO;
ss Program

//
//

static void MainO {


SI0::Stream s = new MIO: .-Empty St ream ; //

, ( . ::
)bal. , ,
, .
: ,
,
; .
, , . , ) ,
, .


+ + ,
. # ,
- + + , , .
,
, .

, .
/ //. . 12.4.
' .
^ , . , , .

288

12. , , ,

1 2 . 4 .

fdefine, fundef

(, #def i ne DEBUG)
(fundef DEBUG) ,
.
.

#if,#elif,
felse, fendif

. ,
, ,
(. )

#line

, ,
(, #1 i 200 "ku_ku.txt").

,
1
,

terror,
fwarning

, .
f error
(, ferror ).
fwarning

#region,
fendregion

,
.

fpragma

# 2.0. (#pragma warning


disable) (#pragma warning restore )

#if
[ #e1if
...]
[ #elif

...]
[ #else

...]
#endif
# e l i f .
, .
,
==, ! =, !, && 11. .
true,
#def i ne.
1

... , . . . .

289

12.4.
1 2 . 4 .
// #define VAR1
// #define VAR2
using System;
namespace ConsoleApplicationi
{
class Classl
{
#if VAR1
static void F ( ) { Console.WriteLine( " 1" ): }
#elif VAR2
static void F(){ Console.WriteLineC " 2" ); }
#el se
static void F ( ) { Console.WriteLineC " " ); }
#endif
static void MainO
{
FO:

}
}
,
, F.
#def i
. Conditional
. ,
. 12.5.
,
System.Diagnostics.
1 2 . 5 . Conditional
// #define VAR1
fdefine VAR2
using System;
using System.Diagnostics:
namespace ConsoleApplicationi
{
class Classl
{
[Conditional ("VAR1")]
static void A ( ) { Console.WriteLineC " A" ); }
[Conditional

("VAR2")]

&

290

12. , , ,

12,5

()

static void ( ) { Console.WriteLineC " " ); }


static void MainO
{
:

BO;

}
Main ,
, VAR1 .


,
: , , ,
.

, [20], [21], [26], [27], [30].

13
,
-
3 , , . N E T .
, , # 2.0, -
1

[generics), (partial types) (nullable types) .


,
.
,

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

, 2.0, . ,
9, 10.

292

13. , -

, .
.
, .
, ,
.
. ,
, , , ,
:
, , ,
. .
:

,
,
, .
,

.
,
,
. .
. , L I F O
(Last In First Out, ).
,
. ,
.
, , .
,
, .
. .
, F I F O (First In First
Out, ). ,
-. , ,
, ,
-.
, ,

. .
.

293

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

. 1 3 . 1 .
,
,
, . .
,
.
,
,
. ,
:
,, .
,
, .
.
-, , ,
, .

294

13. , -

, , (. 13.1). .
(-),
-.
1 3 . 1 . -

boy

girl

dog

'

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

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

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

295

System.Collections

. ,
.
, ,
,
.
.

,
.
,
. ,
,
,
.

.

. N E T ,
.
, , System.Collections, System.
Col 1 ecti ons. Speci al i zed System. Col 1 ecti ons. Generi ( 2.0).
.

System. Array, ,
System.Array (. 6. 133).

System.Collections
System.Collections
, . . 13.2
,
. N E T (. . 198).
1 3 . 2 . System.Collections

ICol 1 ecti on
I

Compare

(, )

IDictionary

IDictionaryEnumerator

,
IDictionary

296

13. , -

13.2

()

IEnumerable

IEnumerator

IEnumerator

foreach

IHashCodeProvider

I Li st

. 13.3 ,
1

System. Collections .
1 3 . 3 . System.Collections

ArrayList

IList, ICollection, IEnumerable, ICloneabl

BitArray

I Col lection, IEnumerable, ICloneable

Hashtable

Queue

IDictionary, ICollection, IEnumerable,


ICloneable
ICollection, ICloneable, IEnumerable

SortedList ,
.

IDictionary, ICollection, IEnumerable,


ICloneable

Stack

ICollection, IEnumerable

System.Collections.Specialized
, StringCollection -
StringDictionary.
ArrayLi st.

ArrayList
, ,
,
. ArrayList
.
1
2

13.2 13.3 [27] .


, Hashtabl
System.Object.GetHashCode.

297

System.Collections

ArrayLi st 16
object. ,
Capacity, :
ArrayList arrl = new ArrayList();
// 16
ArrayLi st arr2 = new ArrayList(1000); // 1000
ArrayList = new ArrayList();
arr3.Capacity = 1000;

//

ArrayLi st . 13.4.
1 3 . 4 . ArrayList

Capacity

( ,
)

Count

Item

Add

AddRange

BinarySearch

Clear

Clone

CopyTo

GetRange


ArrayLi st

IndexOf

(
-1, )

Insert

( )

InsertRange

LastIndexOf

Remove

RemoveAt

RemoveRange

Reverse

SetRange

Sort

TrimToSize

(. . 205).

298

13. , -

ArrayLi st Array,
. # object,
.
, ,
object,
-. ,
, . ,
, ArrayLi st.
,
, ,

.
ArrayLi st:
arrl.AddC 123 ) ;
arrl.Add( - 2 ) :
arrl.AddC "" );
,
, :
int

= (int)

arrl[0]

int

b = (int)

arrl[l]

string s = (string) a r r l [ 2 ]
, ,
InvalidCastException.
:
ArrayLi st , "
,
, ArrayLi st. ~
13.1,
Monster .
(. . 178), ~
, Stado
.
1 3 . 1 .
using System;
using System.Col lections;
namespace ConsoleApplicationl

{
class Monster { . . . }
class Daemon : Monster { . . . }
class Stado : IEnumerable
{

299

private ArrayList l i s t ;
public StadoO
{ l i s t = new ArrayListO; }
public void Add( Monster m ) { list.Add( m ) ; }
public void RemoveAtC int i ) { list.RemoveAtt i ); }
public void ClearO
{ list.ClearO; }
public IEnumerator GetEnumeratorO
{ return list.GetEnumeratorO; }

}
class Classl
{

static void MainO


{
Stado stado = new StadoO;
stado.Add( new Monster( "Monia" ) , ) ;
stado.Add( new MonsterC "Monk" ) );
stado.Add( new Daemon ( "Dimon", 3 ) );
stado. RemoveAU 1 );
foreach ( Monster x in stado ) x. PassportO;

}
}
}
:
Monster Monia
Daemon Dimon

health = 100 ammo = 100


health = 100 ammo = 100 brain = 3

,
-,
. , . # , 2.0,
- (generics), .
.

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

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

, .

300

13. , -

. N E T
,
, , , , . . ,
System.Collections.Generic,
System.Collections,
System.CoIlections (. . 295). . 13.5

. N E T (, , ,
).
1 3 . 5 . .NET 2.0
- ( 2.0)

Comparer<T>

Comparer

Dictionary<K,T>

HashTable

LinkedList<T>

List<T>

ArrayList

Queue<T>

Queue

SortedDi cti ona ry<K,T>

SortedList

Stack<T>

Stack

, . N E T 1.0 1.1,
, , object:

,
,
,
;


,
.

- , .
.
ArrayLi st List<T>
Monster Daemon,
1

5 8, .
1 3 . 2 . List<T>
using System;
using System.Collections.Generic;
using System.Text:
(. . 275)

301

namespace ConsoleAppl icationi


{
using MonsterLib;
class Program
{
static void MainO
{
List<Monster> stado = new List<Monster>();
stado.Add( new MonsterC "Monia" ) );
stado.Add( new MonsterC "Monk" ) );
stado.AddC new Daemon ( "Dimon", 3 ) );
foreach ( Monster x in stado ) x.PassportO;
List<int> lint = new List<int>();
lint.AddC 5 ); lint.AddC 1 ); lint.AddC 3 );
lint.SortO;
int a = l i n t [ 2 ] ;
Console.WriteLineC a );
foreach ( int x in lint ) Console.WriteC x + " " );

}
}

}
:
Monster Monia
Monster Monk
Daemon Dimon
5
1 3 5

health = 100 ammo = 100


health = 100 ammo = 100
health = 100 ammo = 100 brain = 3

13.2 . (stado)
, MonsterL.ib.clll,
. , Monster,
,
.
, ,
, ,
, ,
. ,
,
, .
1 i nt ,
,
, (. 13.1).
- ,
,

302

13. , -

. ,
-, .


,
.

.

13.3
. ,
.
Dictionary<T,K>.
: , .
, ,
, ,
.
1 3 . 3 .
using System:
using System.Collections.Generic;
using System.Text;
using System.10;
namespace ConsoleApplicationl
class Program
{

static void MainO


StreamReader f = new StreamReader( @"d:\C#\text.txt" );

//

string s = f.ReadToEndO;

//

// 3
char[] separators = {
' ',
' ! ' };
List<string> words = new List<string>( s.Split(separators) ); / / 4
Dictionary<string, int> map = new Dictionary<string. i n t > ( ) ; / / 5

foreach ( string w in words )

//

if ( map.ContainsKeyC w ) ) map[w]++;
else
map[w] = 1;

foreach ( string w in map.Keys )


Console.WriteLineC " { 0 } \ t { l } " , w. map[w] ) ;

// 7

303

text. txt

. , .
, !
:

1
3
1
2
4

1
2

1
2

1
2
1
1
1

. 1 ,
32 767 , 2
.

, . ,
, ,
FileNotFoundException (. 11).

3 ,
Spl it, ,
.
words List<string>.
.
5 , 6
words. , ,
, . ,
.
7 (
Keys, )
.
Split : ,
, .
, 15.

304

13. , -

,
. ,
, .

,
System.Collections.Generic
, . 13.6.
1 3 . 6 . .NET 2.0
( 2.0)

ICollection<T>

ICollection

IComparable<T>

IComparable

IDi ct i ona ry<K.

IDictionary

IEnumerable<T>

I Enumerable

IEnumerator<T>

I Enumerator

IList<T>

IList

C# -
, , , (generic)
.
- ,
# . , ,
, ,
:
public class Stack<T>
{
[ ] items:
int count;
public void PushC T item ) { . . . }
public T
PopO
{ ... }

//
//

}

, int:
Stack<int> stack - new Stack<int>();
stack.PushC 3 );
int x = stack.PopO;
Stack<int> (constructed type).

305

. Stack
, doubl ,
. ,
,
. -
.
- .
(constraints), ,
,
, , ,
, .
where, :
public class Stack<T>
where T : struct
{ ...
struct ,
.
class. , ,
, ,
, .
,
,
. , - -
, , , . .
,
- ,
-.
new(), :
public class EntityTabl<,>
where : IComparable<K>, IPersistable
where E: Entity. new()

{
public void Add( key. E entity )
{

if ( key.CompareToC x ) < 0 ) { ... }

}
}
EntityTable
, ,
Entity .

306

13. , -


, , ,
.
-
default.
null, 0.


, -
. .
, .
, ,
.
,
, ,
.
13.4 .
,
, ,
, , . .,
- 1 .
.
1 3 . 4 .
using System;
using System.Collecti ons.Generi ;
using System.Text;
namespace ConsoleApplicationl
{

class Program
static void Sort<T> ( ref T [ ] a )
where T : IComparable<T>

// 1
// 2

{
T buf;
int n = a.Length;
for ( int i = 0; i < n - 1: ++i )

{
int im = i;
for ( int j = i + 1; j < n; ++j )
if ( a[j].CompareTo(a[im]) < 0 ) im = j;
buf = a [ i ] ; a [ i ] = a[im]; a[im] = buf;

}
}
static void MainO

II

307

i n t : ] = { 1, 6. 4, 2, 7. 5, 3 } ;
Sort<int>( ref );

// 4

foreach ( int elem in aa )) Console.


Console.WriteLineC elem );
double[] b = { 1.1, 5 2, 5.21, 2, 7, 6, 3 } :
SortC ref b );
foreach ( double elem in b ) Console.WriteLineC elem );

// 5

string[] s = { "qwe", qwer", "df", "asd" };


SortC ref s );
foreach ( string elem in s ) Console.WriteLineC elem );

// 6

, H a i (
2 ) , -
CompareTo, 3.
( Main) Sort :
- ( 4) (
5 6).
, .

- ,
.

, :

, ;

, .

, -
, .

, ,
. -,
, , ,
.
,
.

308

13. , -



,
(partial types). ,
, , ,
. ,
, .
partial.
, , :
public partial class
{

}
public partial class A
{

}
,
.
, ,
.
partial
class, struct interface
.
.

parti al ,
, .

.
abstract sealed,
.
- ,

.
,
:
,
. . ,
, ,
.

309


, ,
.
. ,
- , . ,
,
- 1 . , .
, ,
. , ,
, .
# 2.0
, (nullable).
, ( Value)
, ,
( HasValue).
, HasValue true.
null, HasValue false,
Value .
, ?,
:
int? = 123;
int? = null;
if ( .HasValue ) Console.WriteLineC x ); // x x.Value
if ( y.HasValue ) Console.WriteLineC );

, ,
:
int
i = 123:
int?
= i:
double? = x;
int?
z = (int?) y;
int
j = ( i n t ) z;

//int
- - > int?
// int?
- - > double?
// double? - - > int?
// int?
- - > int

. ==
!= true, null.
, nul 1 .
<, >, <= >= false,
nul 1.

nul 1, nul 1, :
int? = null;
int? = + 1;

// = null

310

13. , -

??
(null coalescing operator). ,
, null, .
, null, :
int? = null:
int = ?? 0;
= 1:
= ?? 0:

// =
// = 1

X M L .



,
,
.
,

,
.

,
.
.NET

. , 2.0
, ,
2.0,
.
, ,
- .
,
C L R .
-
, .

.
, ,
.

14


Windows
#
,
.NET, .
Windows-
, .
Windows
.


. :
,
.

, . Windows
.

.
,
:
.
.

.
.

, . .
,

. .

312

14. Windows

,
, A P I (Application Program
Interface ). API-
(Dynamic Link Library, D L L ) , j
. ,

, .
Windows
,

, .
,
. , .
.

|
\

4 .

Windows , ,
Windows 95/98 , Windows NT/2000.

. ,
O L E (Object Linking and Embedding
) .

. 32- W i n d o w s
16- Windows-, ,
M S - D O S .
DOS-, ,
M S - D O S .

( ).

Windows . ,
,
.
( , , )
. , ,
. 14.1.
1

32- , ,
32 .

313

. 1 4 . 1 . ,
W i n d o w s ,
(,
, ).
, ,
.
, , ,
.
,
. ,

, (. 14.2). ,
Windows- ,
,
.

. 1 4 . 2 . Windows-
Visual Studio.NET Windows-
,
, ,
. . .

314

14. Windows

Windows-

(File New Project), Windows Application


(. 14.3). ,
, Windows-. ,
, Form1.cs[Design],
.
( ) , , ,
, . .

. 1 4 . 3 .
, .
, Solution Explorer (View Solution Explorer)
Forml .cs
View Code. , ,
, 14.1. ,
, ,
.
1 4 . 1 . Windows-

using
using
using
using
using
using

System;
System.Drawing:
System.Col lections;
System.ComponentModel;
System.Windows.Forms;
System.Data;

315

1 Windows-

nespace WindowsApplicationl
public class Forml : System.Windows.Forms.Form

{
private System.ComponentModel.Container components = null;
public Forml()
{
InitializeComponent

();

}
protected override void Dispose( bool disposing )
{
i f ( disposing )
{
if (components != null)
{
components.Di spose();

}
}
base.Dispose( disposing );

}
#region Windows Form Designer generated code
private void InitializeComponent

{
this.components = new System.ComponentModel.ContainerO;
this.Size = new System.Drawing.SizeOOO.300):
this.Text = "Forml";

}
#endregion
static void MainO
{
Application. Run (new FormlO);

}
}

.NET. , ,
:
jsing System;
jsing System.Windows.Forms;
.
System . System.Windows.Forms
, Windows-.

316

14. Windows


. 14.1, . 14.4.
1 4 . 1 . Windows.Forms

Application

Windows-.

Windows-,
. .

ButtonBase, Button, CheckBox, ComboBox,


OataGrid, GroupBox, ListBox, LinkLabel,
PictureBox

,
(): ,
, , ,
, , , ,

Form

Windows-

ColorDialog, FileDialog, FontDialog,


PrintPreviewDialog


, , ,

Menu, MainMenu, Menultem, ContextMenu

Clipboard, Help, Timer, Screen, ToolTip,


Cursors


: , ,
, , ,

StatusBar, Splitter, ToolBar, ScrollBar

,
: ,
, . .

Wi ndows. Forms .
14.1. Forml,
Form. ,
. Forml
components ,
.
InitializeComponent,
(
fregion fendregion).
,
. , ;
(Properties), :
this.BackColor = System.Drawing.SystemColors.AppWorkspace:
Di spose ]
. , Mai , ^
Run Application. Run ,
,

Windows-

317

. 1 4 . 4 . Windows.Forms

, ,
D e b u g S t a r t F5.
,
Windows-: ,
.

Windows- :
1. , .
2.
.

( )
. ,

318

14. Windows

View Properties Window. ,


.
, (. 14.5).

. 1 4 . 5 .

Toolbox ( ,
View Toolbox),
.
.
, .
(
).
,
.
+, , .
. ,
Windows Forms, . 14.6.
(. . 325).
,
, ,
. ., , ,
.

319

Windows-

. 1 4 . 6 .


.
.


,
Events ,
( Events
, . 14.5).
,
. :

Acti vated ;

Click, Doubleclick ;

Closed ;

Load ;

KeyDown, KeyUp ;

Keypress , ASCII-;

MouseDown, MouseUp ;

MouseMove ;

Paint .

14.2 ,
, . 14.5,
Click, KeyPress.
.
1 4 . 2 .

using System;
using System.Drawing:
using System.Collections:

&

320

14. Windows

14.2

{)

using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace WindowsApplicationi
{
public class Forml : System.Windows.Forms.Form

//

//
//

1
2

//
//
//

3
4
5

{
private System.Windows.Forms.TextBox textBoxl;
private System.Windows.Forms.Button buttonl;
private System.ComponentModel.Container components = null;
public Forml
{
1 i t i a1izeComponent():

protected override void DisposeC bool disposing ) { . . . }


#region Windows Form Designer generated code
private void InitializeComponentO

{
this.textBoxl = new System.Windows.Forms.TextBoxO:
this.buttonl = new System.Windows.Forms.ButtonO;
this.SuspendLayoutO;
//

// textBoxl
//
this.textBoxl.Location = new System.Drawing.Point(24, 16);
this.textBoxl.Name = "textBoxl":
this.textBoxl.Size = new System.Drawing.Size(240. 20);
this.textBoxl.Tablndex = 0;
this.textBoxl.Text = "textBoxl";
this.textBoxl.KeyPress += new
// 6
System.Wi ndows.Forms.KeyPressEventHandler(thi s.textBoxl_KeyPress);
//
// buttonl
//
this.buttonl.Location = new System.Drawing.Point(192. 80);
this.buttonl.Name = "buttonl";
this.buttonl.Tablndex = 1 ;
this.buttonl.Text = "buttonl";
this.buttonl.Click += new
111
System.EventHandler(this.buttonl_Cl ick);
//

// Forml
//
this.AutoScaleBaseSize = new System.Drawing.Size(5. 13);
this.ClientSize = new System.Drawing.Size(292, 126):
this.Controls.Add(this.buttonl);

//

321

Windows-

thi s.Controls.Add(thi s.textBoxl)


this.Name = "Forml";
this.Text = "Forml";
this.ResumeLayout(false);

// 9

// 10

}
lendregion
static void MainO
Application.Run(new Forml 0 ) ;
private void buttonl_Click(object sender. System.EventArgs e)

// 11

private void textBoxl_KeyPress(object sender.


System.Windows.Forms.KeyPressEventArgs e)
// 12

}
. 1 2
: TextBox Button.
System.Windows .Forms,
using, :
public class Forml : Form

//

{
private TextBox

textBoxl;

// 1

private Button

buttonl;

111

private Container components = null;


, ,
. , Name .

Class View (View


Class View),
Name . ,
buttonl , .
, .

InitializeComponent.
3 4 ,
, , , . .

322

14. Windows

6 7.
. ,
10 (. . 232) .
, buttonl, ! ick,
buttonl_Click, Forml.
, (
) .

. ,
: - , .

,
Events .


, Controls
( 8 9). ,
.
(Add Remove).
,
:
1. .
2. ,
.
3. .
5 10 ,
. 5
, 10 .
( 11 12) ,
. ,
:

MessageBox.Show(" buttonl");
MessageBox.Show(" " + e.KeyChar);

// 11
// 12

Show MessageBox,
System.Windows.Forms.
,
,
.
, *
, Control.

323

Control

Control
Control ,
, ,
, , . Control
.
, , ,
.
, Parent, ,
1

, , .
Control . 14.2,
. 14.3.
1 4 . 2 . Control

Anchor

,
.
,

BackColor,
Backgroundlmage, Font,
ForeColor, Cursor

:
, , , ,

Bottom, Right

.
Size

Top, Left

.
Location

Bounds

Rectangle (),

CIientRectangle

Rectangle,

ContextMenu

Dock

Location


, ,
Pol nt. X Y

Height, Width

Size

Size.
Height Width

,
. , ,
.

&

324

14. Windows

14.2

{)

Created, Disposed,
Enabled, Focused,
Visible

bool,
: , ,
, ,

Handle

(
, )

ModifierKeys

,
(Shift, Control, Alt).
Keys

MouseButtons

, .
MouseButtons

Opacity

.
0 () 1 ()

Parent

,
( , -)

Region

Region,

Tablndex, TabStop


Tab
,

1 4 . 3 . Control

Focus

GetStyle, SetStyle

.
Control Styles (. )

Hide, Show

Visible (Hide ,
Show )

Invalidate

QnXXXX


.
,
, ,
- (OnMouseMove, OnKeyDown,
OnResize, OnPaint . .),

Refresh

SetBounds,
SetLocation,
SetClientArea

, , .

325

Control Styles
, .
,
ResizeRedraw. ,
.
, - ,
.
. 14.4 , Control.
1 4 . 4 . Control

Click, Doubleclick, MouseEnter, MouseLeave,


MouseDown, MouseUp, MouseMove, MouseWheel

KeyPress, KeyUp, KeyDown

BackColorChanged, ContextMenuChanged,
FontChanged, Move, Paint, Resize

GotFocus, Leave, LostFocus

, ,
.
:

.
,
Events .
,
.
, OnXXXX (OnMouseMove,
OnKeyDown, OnResize, OnPaint . .), ,
- .
[27].


, ,
ToolBox (View ToolBox).
.

.

326

14. Windows

Label
.
Text. ( Font), (BackColor),
(ForeColor) (TextAlign) .
(AutoSize = True).
(Image) (
BackColor Col or. Transparent).
, ,
. (. . 327).

Button
, , (Click). ,

, . . Enter ,
, .
,
Text, , .
AcceptButton ,
, Enter Click,
.
.
, Cancel Button ,
, Esc Click
.
. ,
(, ).
. ,
,
. ,
( Yes),
, , Cancel.
1 4 . 5 . DialogResult

None

Ignore

Ignore

OK

Yes

Yes

Cancel

Cancel

No

No

Retry

Retry

Abort

Abort

327

, ,
DialogResult.
DialogResult, System. Wi ndows. Forms.
. 14.5.
.

TextBox
TextBox ,
Text.
( 32 ), ,
() ,
( PasswordChar).
Multiline, Scrol 1 Bars Wordwrap.
Readonly.
(Clear), (Select),
(), (Paste) .,
, KeyPress KeyOown.
,
Label, Button TextBox. . 14.7.

. 1 4 . 7 .
Enter,
, .
, !.

. ,
(. 14.8).
, .
.
, . Text
. 14.6 (
; ).

328

14. Windows

. 1 4 . 8 .
1 4 . 6 . Text

Text

Forml

label 1

label2, label3, textBoxl

buttonl

.
, ,
. ,
( Load) ( CI i ck).
Enter ( KeyPress
textBoxl).

Events (. . 14.5).
i .
max . rnd
2

.
14.3.
1 4 . 3 .
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
1
2

, .
Random (. . 148).

329

namespace WindowsApplicationl
{
public class Forml : Form
{
private Label
labell:
private TextBox textBoxl;
private Button buttonl;
private Label
label2;
private Label
label3;
private Container components = null;
public Forml { ... }
protected override void Dispose( bool disposing ) { . . . }
Windows Form Designer generated code { ... }
const int max - 10;
Random rnd;
int i, k;

//
//
II

static void MainO { ... }


private void Forml_Load(object sender, EventArgs e)
{
rnd = new RandomO;
i = rnd.Next(max);
// 0 max
}
private void textBoxl_KeyPress(object sender, KeyPressEventArgs e)
{
int n;
if ( e.KeyChar != (char)13 )
return;
// Enter,
try
{

//
n = Convert.Int32(textBoxl.Text);

}
catch
{
n = -1; // ,
}
if ( n != i )
// = = = = = = = = = =
{
label 2.Left += 5;
label2.Text = "He !";
textBoxl. ClearO;
k++;
//
buttonl.Visible = false;
}

330

14. Windows

14.3

()

else
{


1abel2.Left = 32: //
1abel2.Text = " ";
double koef = 1.0 * k / max:
label3.Text = koef.ToStringO;
buttonl.Visible = true;

}
private void buttonl_Click(object sender. System.EventArgs e)
{

i = rnd.Next(max)
k = 0;
textBoxl.CI ear()
textBoxl.Focus
label 2.Text = ""
1abel3.Text = ""

// 0 max
//
//
//
//

MainMenu ContextMenu
MainMenu ,
: Toolbox.
,
.
Menultem, Text.
,
Enter . ,
, Name ,
.
( Enabled),
(Visible), (Checked).
.
Exit,
.
Close , Exit
Appl i cati on, :
private void Exit_Click(object sender, EventArgs e)
{
// - Exit
CloseO:
// :
// Application.ExitO;

331

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

CheckBox
- -
. , , Checked,
true false.
, . , ,
,
, .
CheckState, Checked, Unchecked Intermediate.
, ThreeState,
.
, .
Appearance :
(Normal), (Button), .
, ,
.
.

RadioButton

, .
( Checked),
. ,
, .
, .
,
.
, Group Panel.
Appearance :
(Normal), (Button),
1

.
.
1

AutoCheck false .

332

14. Windows

GroupBox
GroupBox ,
,
, .
14.4 ,
,
.
.' [-10; 10], [-100; 100].
/ .
. 14.9. ( Name)
, .
GroupBox. Checked radioButtonl,
, maxtextBox numPosittextBox
Readonly ( ).

" i e

!!!|

{ < tffltlOl 00; j

|
J

. 1 4 . 9 .


. Format,
,
Shift Ctrl. ,
, Format Align Lefts.

,
CI ick.
1 4 . 4 .
using System;
using System.Drawing; .
using System.Collections;
using System.ComponentModel;

333

using System.Windows.Forms;
using System.Data;
namespace WindowsApplicationl
{
public class Forml : Form
{
private Label
labell;
private GroupBox
groupBoxl;
private RadioButton radioButtonl;
private RadioButton radioButton2;
private TextBox
numtextBox;
private CheckBox
numPositcheckBox;
private Button
createbutton;
private Button
calcbutton;
private TextBox
maxtextBox;
private TextBox
numPosittextBox;
private TextBox
arraytextBox;
private CheckBox
maxcheckBox;
private Container
components = nul1;
public FormlO { . . . }
protected override void Dispose( bool disposing ) {
Windows Form Designer generated code { . . . }
//

i n t [ ] arr;
static void MainO { . . . }

private void createbutton_Click(object sender, EventArgs e)


{
Random rnd = new RandomO;
int a = -10. b = 10;
if ( radioButton2.Checked )

a =

//

-100; b = 100;

//

int n = 0;
try
{
n = int.Parse(numtextBox.Text);

//

}
catch
{
MessageBox.Show(" !");
numtextBox.CI e a r ( ) ;
numtextBox.Focus();

&

334

14. Windows

14.4

()

arraytextBox.ClearO:
maxtextBox.ClearO;
numPosittextBox.ClearO;

//

if ( n < 0 ) n = -n;
//
= new i n t [ n ] ;
//
for ( int i = 0; i < n; ++i )
{
a r r [ i ] = rnd.Next(a. b ) ;
//
arraytextBox.Text += " " + arr[i.];
//

}
}
private void calcbutton_Click(object sender, EventArgs e)
{
int max = arr[0];
int numPosit = 0;
for ( int i = 0; i < arr.Length; ++i )
{
if ( a r r [ i ] > max ) max = a r r [ i ] ;
//
if ( a r r [ i ] > 0 )
++numPosit; //

}
if ( maxcheckBox.Checked )
maxtextBox. Text = max.ToStringO;
else maxtextBox.Text = " " ;
if ( numPositcheckBox.Checked )
numPosittextBox.Text = numPosit.ToStringO;
else numPosittextBox.Text = " " ;

ListBox
,
( Sel ecti onMode One)
( Sel ecti onMode Multi Simple Multi Extended).
SelectionMode MultiSimple,
. Multi Extended
Shift,
Ctrl, Windows. ,
SelectionMode, None.
,
. ( MultiColumn)
(Sorted = True).
. Items,
. Items

335

wjd, AddRange Insert. Remove RemoveAt,


/ .
Selectedltems SelectedIndices,
.
14.5 ,
Li stBox , ,
.
. 14.10.

" Forml

| _|[

SystemObject
SystemMashalByRefObject
System. ComporrertModeLComponent
System. Windows.Forms.Conttol
System. Windows.Forms.ScrollableControl
System.Windows.Foims.ContainerContiol
System. Windows.Forms.Form

. 1 4 . 1 0 .
1 4 . 5 .
using System;
using System.10;
using System.Drawing;
using System.Col lections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Collections.Specialized;
namespace WindowsApplicationi
{
public class Forml : Form
{
private ListBox
listBoxl;
private Button
buttonl;
private Container components = null;
public FormlO { . . . }
protected override void DisposeC bool disposing ) { . . . }
Windows Form Designer
generated code 1 { . . . }
3
'

&

336

14. Windows

14.5

()

static void MainO { ... }


private void Forml_Load(object sender. EventArgs e)
{
try
{
StreamReader f = new StreamReaderC "input.txt" );
string buf;
while ( ( buf = f.ReadLineO ) != null ) //
listBoxl.Items.Add(buf);
//
}
catch ( FileNotFoundException exc )
{
MessageBox.Show( exc.Message );
return;
}
}

private void buttonl_Click(object sender. EventArgs e)


{
StreamWriter f = new StreamWriter( "output.txt" );
foreach ( string item in listBoxl.Selectedltems )
f.WriteLine(item);
//
f.CloseO;

}
}
}
,
. . N E T .
, ,
1

, .

.
, , F1
...overview (). Remarks Example,
...Members ( ),
,
.
,


Form Application, .
1

, , , <

337


; Form .
Form : Object-^MarshalByRefObject-Component->Control-
HableControl>ContainerControl.
ia .
. , 1

. ,
, - . ,
,
Cancel, .
MessageBox, Windows- (. . 322).
,
, , . , i - .
. ( Main).
.
i (Multiple Document
nterface, M D I ) , .
. , , .
.
, ,
.
MarshalByRefObject ,
,
.
Component , . , Di spose,
,
. , ,
.
1

- -
.

338

14. Windows

Control, ,
. ScrollableControl ,
.
AutoScrol 1 AutoScrol IMinSize
, .
ContainerControl
. ,
, . .,
.
, ,
TabStop Tablndex.

Form
Form ,
. ,
,
. 14.7-14.9.
1 4 . 7 . Form

AcceptButton

,

Enter

ActiveMDIChild,
IsMDIChild,
IsMDIContainer


( M D I )

AutoScale

, ,
,
, ,

FormBorderStyle


( FormBorderStyle)

Cancel Button

,

Esc

Control Box

, ,

Menu, MergedMenu

MaximizeBox,
MinimizedBox

339

nTaskbar

, Windows

Position

,

( FormStartPosition).
,
CenterScreen

wState


( FormWi ndowState)

1 4 . 8 . Form

vate

:erToScreen

xitMDI


LayoutMDI

esize

Resize

( Control)

wDialog

(
)

> 1 4 . 9 . , Form

ti vate

(
)

osed, Closing

HChildActive

, , .


5 . N E T
.
) . 1, . :
2 (FormBorderStyle = FixedDialog);

(MaximizeBox = False, MinimizedBox = False);

340

14. Windows

, ,
Cancel, ,
(AcceptButton = __, Cancel Button = __1);

DialogResult ,
.

ShowModal,
DialogResult,
. 14.5.
,
. Cancel
.
.
14.6 , .
(. 14.11) Dialog Exit
Dialog ,
, , Cancel (. 14.12)

. 1 4 . 1 1 .

. 1 4 . 1 2 .

, .
Cancel,
.
*
Project > Add Windows Form.
, , <
. 14.10.

341

1 4 . 1 0 .

Text

TextAli gn

MiddleCenter

Text

Name

btnOK

DialogResult

OK

Text

OK

Name

btnCancel

DialogResult

Cancel

Text

Cancel

AcceptButton

btnOK

Cancel Button

btnCancel

FormBorderStyle

FixedDialog

MaximizeBox

False

MinimizeBox

False

StartPosition

CenterParent

textBoxl,
. Text ,
Info. ,
:
public class Form2 : Form
{
private Label
private TextBox

label 1:
textBoxl:

private Button

btnOK:

private Button

btnCancel;

private Container components = null;


public string Info
{
get
{
return textBoxl.Text:
}

}
}
, ,
.

342

14. Windows

(BorderStyle = FixedSingle).
:
private void menuIteml_Click( object sender, EventArgs e )
{
Form2 f = new Form2();
//
if ( f.ShowDialogO == DialogResult.OK )
//
1abel1.Text = f.Info;
}
private void menuItem2_Click( object sender, EventArgs e )
{
CloseO;

//

}
,
, ShowDialog.

Info, .
, ,
set.

Form
. , ,
new .
Show ShowDi al Form, .
Hide. , Show Hide
Visible . Hi de
CI ose , ,
, CI ose .

Application
Application, System.Windows.Forms,
, ,
.
Application . 14.11.
1 4 . 1 1 . Application

AddMessageFiIter,
RemoveMessageFi Iter



.
, ,
1
IMessageFiIter

. [27].

343

Application


DoEvents

Exit

ExitThread

Run

CommonAppOataRegi stry

CompanyName

CurrentCulture

CurrentInputlanguage

ProductName

ProductVersion

StartupPath

ApplicationExit

Idle

ThreadExit


.
,
Appl i cati onExi t

Appl i cati on (
, ),
System.Reflection.
Application,
.

344

14. Windows


, ,
Graphics, System.Drawing.
.
, Graphics
PaintEventArgs, Paint,
:
private void Forml_Paint( object sender, PaintEventArgs e )
{ Graphics g = e.Graphics;
//
}
CreateGraphics,
:
Graphics g;
g = this.CreateGraphicsO;
- Image.
:
Bitmap bm = new Bitmap( "d:\\picture.bmp" );
Graphics g = Graphics.FromImage( bm );
Graphics ,
, . ,
, :

Brush ;

Font ;

Col or .

14.6 ,
, . . 14.13.
1 4 . 6 .
using System;
using System.Drawing;
using System.Windows.Forms;
namespace WindowsApplicationl
{
public partial class Forml : Form
{
public Forml { InitializeComponent(); }
private void Forml_Paint( object sender, PaintEventArgs e )
{
using ( Graphics g = e.Graphics )
{

// 1

345

using ( Pen pen = new Pen( Col or. Red ) )

112

{
g.DrawLine( pen. 0, 0, 200. 100 );
g.DrawEllipse( pen. new Rectangle(50, 50, 100, 150) ):
}
string s = "Sample Text";
Font font = new Font( "Arial", 18 );
SolidBrush brush = new SolidBrush( Col or. Black );
float x = 100.OF;
float = 20.OF;
g.DrawString( s, font, brush, x. );
font.DisposeO;
brush.DisposeO:

/7 3
//4

// 5
//

. 1 4 . 1 3 .
,
Dispose.
1

# using :

using ( )
, System.
IDisposable, Dispose. , ,
, . Dispose
, .
using Dispose ,
. 1 2.
1

using # :
. using 12.

346

14. Windows

3 4 ,
Di spose, 5 6.
,
,
, -, , -, ,
-, .


Windows- ,
:
.

Windows-: Microsoft,
Apple,
, ,
,

:
,
, ,
, . .

.
.
, , *
. , ,
(, - '
? ) ,
-
. .
Windows-. ,
. N E T :
,
, .
, , <
, (
, ]

[17], [18], [20], [31].
: , i
!

1 5

#
# Visual Studio:
, X M L .

# : A S P . N E T (- -) A D O . N E T ( ).
, ,
+ + , # ,
.
X M L ,
, .
, -, -
# , -
, ,

, , . .


# :
.

,
,
.

, , ,
, .
(unsafe) .

348

15. #

, C L R
.
unsafe,
.
unsafe ,
.
, , , , . . ,
.
, :
public unsafe struct Node
{
public int
Value; .
public Node* Left;
public Node* Right;

}
Node ,
Left Right.
,
:
public struct Node
{
public int Value;
public, unsafe Node* Left;
public unsafe Node* Right;
}
unsafe :
unsafe
, , .

, , !
/unsafe. Visu
Studio (Project Properties Configuration Properties Build Allow Unsafe Cod*


.
:

* ;
, , .
, . 6i
, , , , *

349

: sbyte, byte, short, ushort, int, uint, long, uiong, char, float,
double, decimal, bool void. ,
.
voi d , ,
, (,

).
voi d ,
, -
, ,
.
:
int*

// int

Node*

pNode;

// Node

void*

//

i n t * [ ] m;

// int

int**

// int

d;

,
:
int* . , :

// int

.
object, object
. , .
,
.

,
, , .

, ,
.
.



void*. null.
, :

( ).

350

15. #

.
, ,

1


:
1. :
:
int

= 5:

int* = &;

//
//

, ,
, .

:
int* = ;
, :
i n t [ ] b = new i n t [ ] {10. 20. 30, 50};
//
fixed ( int* t = b ) { . . . }; //
fixed ( int* t = &b[0] ) { . . . } ;
//

f i xed .

2. :
char* V = (char *)0xl2F69E;
0xl2F69E , (char *)
: char.

,
, .

3. :
i nt* = nul 1 :
1

,
(undefined behavior), .

351

4. :
int* s = stackalloc int [ 1 0 ] ;
stackalloc 10
int ( 10 )
s, .

() #
. , ,
( . ).


.
. 15.1.
1 5 . 1 .

, ,

->

[]

&

++, --

+, -

==, ! = , < > , < = , > =

, .

stackalloc

.
, .
, ,
, .
, , :
int

= 5;

int* = &;
Console.WriteLineC * ):
Console.WriteLineC ++(*) ) ;
i n t [ ] b = new i n t [ ] {10, 20, 30, 50};

//
//
//
//
//


, : 5
:

352

15. #

//

fixed ( int* t = b )

//

int* z = t;
for (int i = 0; i < b.Length; ++i )
t[i] += 5;
*z += 5;
++z;

// ( 5)
// ( 5)
//

Console.WriteLineC &t[5] - t );

//

fixed , ,
, ,
. ,
.

: t z,

.
*
, .
, .
,
.
( , ,
) ,
.
, $ , , .
,
. sizeof
(), sizeof (
). ,
,
.
- .

, ,
, :

short* ; ...
++;
long* q;
q++;

// 2
// q 4

353

,
. ,
5. .

. ,

*++

=10;

,
. , ,
, 10,
, . :
* = 10; ++;
(*)++, , ,
.

t:
uint = 0xAB10234F;
byte* t = (byte*)&x;
for ( int i = 0; i < 4: ++i )
Console.Write("{0:X} ", *t++ ) ;

// : 4F 23 10 AB

, t
. 15.1 :
1 5 . 1 .
using System;
namespace ConsoleApplicationl
{
class A
{

public int value -. 20;

struct
{

public int a;

class Program
{
unsafe static void MainO
{
A n = new A O ;
fixed ( int* pn = &n.value ) ++(*pn);
Console.WriteLineC "n = " + n.value );
b;
* pb = &b;

// : 21

354

15. #

15.1

{)

pb->a - 100;
Console.WriteLineC b.a );
}

// : 100

'

}
}
stackalloc
:

stackalloc [ ]
. ,
System.StackOverflowException.

, .
int 0 4:

int* = stackalloc int [5];


for int i = 0; i < 5: ++i )
{
pC1]-1:
Console.WriteC p[i] + " " ):
}

// : 0 1 2 3 4

15.2 ,
# . IntToString
, .
1 5 . 2 . :

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 = (charX n % 10 + '0' );
n /- 10;
} while ( n !- 0 );
if ( value < 0 ) *--p - '-':
return new stringC p. 0, (int)C buffer + 16 - p ) );

}
}

static void MainO {


Console.WriteLineC IntToStringC 12345 ) );
Console.WriteLineC IntToStringC -999 ) );

}
}

355



:
;
, ;
.
, , H T M L , .
. N E T ,
System.Text.RegularExpressions.

' (),
.
: .
, ,
.
,
,
, , \d\d.

. , IP-,
, , . .

.NET Perl 5.

,
. ,
, .
, , ,
!

. 15.2 ,
.
, . 15.3, ,
, ,
. ,
.

356

15. #

1 5 . 2 .

, \ri

. t
cat, cut, clt, c{t . .

[]


,
.

c[aul]t
cat, cut clt,
c [ a - z ] t
cat, cbt, cct,
cdt,czt

,
,
.

c[*aul]t
cbt,
c2t, cXt . .,
c[*a-zA-Z]t cnt,
clt, c4t, c3t . .

\w

-
,

c\wt
cat, cut, clt, ClOt
. .,
c{t, c;t . .

\W

-
, ,


c\Wt
c{t, c;t, t . .,

cat, cut, clt, clOt . .

\s

,
,
(\t, \v),
(\, \),
( \ f )

\s\w\w\w\s

,

\S

,
,

\s\S\S\s

,

\d

c\dt
clt, c 2 t , c 9 t

\D

C\Dt
clt, c2t
c9t

1 5 . 3 .

, ,

, ,

, ,

357

.
\Z

, ,

\b

, ,
( ,
\w \W)

, ,

, ~cat cat,
, cat$ cat, (
), *$
, , .
.
,
.
,
, \d\d\d\d\d \d{5}.
11111, 12345, 53332 . .
. 15.4.
1 5 . 4 .

ca*t
ct, cat, caat, caaaaaaaaaaat . .

ca+t
cat, caat, caaaaaaaaaaat . .

ca?t
ct cat

{}

ca{3}t
caaat, ( c a t ) { 2 }
1
catcat

{".}

{ 3 , } t
caaat, caaaat,
caaaaaaaaaaaat . .

{.}

ca{2,4}t
caat, caaat caaaat


.
1

358

15. #

. , ,
cat>, dog horse,

cat|dog|horse

,
,
.
:

(, ):
t-+]?\d+

( , ):
C-+]?\d+\.?\d*

():
[A-Z]\d{3}[A-Z]{2}\d\dRUS

, -
, . ,
\ . , \\.

, cat.doc
cat\.doc.
, ,
( !), 1 .
.
, ,
, (
). ,
, ,
. :
(?<_>_)
, ,
. , ,
, nnn-rm-nn.
:
(?<num>\d\d\d-\d\d-\d\d)
,
, , *.doc.

359

num
.

. , ,
, 1. , ,
. ,
(\w)M (wa//,
mass, cooperate)1.
,
. :
()\s+()\s+(?<sum>\d+)\spy6\.\s+Hy . \1
, .
. ,
,
5 . ,

53459 .

, IP-:
((\d{1.3}\.){3}\d{1.3})
, .
. IP-: 212.46.197.69, 212.194.5.106,
209.122.173.160. , , .
1. ,
{3}.
, ,
.
,
(?<s>\w)\k<s>, s , ,
\ .
.
, , .
(?# ), ,
2
# .

.NET

. N E T
System.Text.RegularExpressions.
1
2

\w\w, - .
RegexOptions.
IgnorePatternWhitespace.

360

15. #

Regex, .
,
.
:

Regex () ;

Regex(String) ;

RegexCString, RegexOptions)
RegexOptions (
, ).

,
,
, :
Regex = new Regex( @"\b(?<word>\w+)\s+(\k<word>)\b".
RegexOpti ons.IgnoreCase );
, ,
IsMatch, Match Matches.
IsMatch true, , ,
, false . 15.3
.
, , ,
.
1 5 . 3 . ( IsMatch)
using System;
usi ng System.Text.RegularExpressions;
public class Test
{

public static void MainO


{
Regex r = new Regex( @"\b(?<word>\w+)[.,:;!? ]\s*(\k<word>)\b",
RegexOptions.IgnoreCase ) ;
string t s t l = "Oh. oh! Give me more!";
if ( r.IsMatch( t s t l ) ) Console.WriteLine( " t s t l yes" );
else
Console.WriteLine( " t s t l no" );
string tst2 = "Oh give me. give me more!";
if ( r.IsMatch( tst2 ) ) Console.WriteLineC " tst2 yes" );
else
Console.WriteLine( " tst2 no" );

}
}
:
t s t l yes
tst2 no

361

tst2 ,
. ,
,
- ( \ s ) ( . ) :
Regex = new Regex( <a"\b(?<word>\w+)[..:;!? ].*(\k<word>)\b".
RegexOptions.IgnoreCase ) ;
Match Regex, IsMatch, ,
, Match
, . 15.4,
.
1 5 . 4 . ( M a t c h )
using System;
using System. Text.RegularExpressions;
public class Test

{
public static void MainO
{
string text

= " - $4, - $3, - $10.";

string pattern = @"(\w+) - \ $ ( \ d + ) [ . . ] " ;


Regex

Match

int

total

= new Regex( pattern );


= r.Match( text );
= 0;

while ( m.Success )
{
Console.WriteLine( m );
total += int.Parse( m.Groups[2].ToStringO );
m = m.NextMatchO;

}
Console.WriteLine( ": $" + total );
}
}
:
- $4,
- $3,
- $10.
: $17
Match ,
pattern. Match
Groups, ,
. ,
, ,
, ,
. . ,

362

15. #

, ,
;
string pattern - @"(?'name'\w+) - \$(?'price'\d+).,]";
total += int.ParseC m.Groups["price"].ToString() );

NextMatch Match ,
.

Matches Regex MatchCollection


, .
Split Regex.
,
,
. 15.5 15.4 .
1 5 . 5 . ( Split)
using System;
usi ng System.Col 1ecti ons.Generi ;
using System.Text.RegularExpressions;
public class Test
{

public static void MainO


{
string text
= " - $4, -$3. - $10.";
string pattern
= " [ - ..]+";
Regex r
' = new Regex( pattern );
List<string> words = new List<string>( r.SplitC text ) );
foreach ( string word in words ) Console.WriteLineC word );

'

'

'
i

$4

$3

$10
Repl Regex .
.
,
$ . .:
string text = " -

$4, -$3, - $10.";

string textl = Regex.Repl( text, @"\$", "y.e." );

363


MatchEvaluator, ,
.

Regex Match System.Text.RegularExpressions


, , Capture ,
; CaptureCol 1 ecti on
, ; Group
Capture . .

8
-. ,
.
:
ppp-48.pool-113.spbnit.ru - - [31//2002:02:08:32 +0400] "GET / /1.1" .200
2434 "http://www.price.ru/bin/price/firmlnfo_f?fid=10922&where=01&base=2"
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)"
81.24.130.7 - - [31/May/2002:08:13:17 +0400] "GET /swf/menu.swf HTTP/1.~200
4682 "-" "Mozilla/4.0 (compatible; MSIE 5.01; Windows 98)"
81.24.130.7 - - [31/May/2002:08:13:17 +0400] "GET /swf/header.swf HTTP/1.1" 200
21244 "-" "Mozilla/4.0 (compatible; MSIE 5.01; Windows 98)"
gate.solvo.ru - - [31/May/2002:10:43:03 +0400] "GET / HTTP/1.0" 200 2422
"http://www.price.ru/bin/price/firminfo_f?fid=10922&where0I&base=l"
"Mozilla/4.0 (compatible; MSIE 5.0: Windows NT; DigExt)"
,
.
,
, 0 ,
, 5 (GET ), 8
(200 ) , ,
9 .
15.6 H T M L
, ,
, .
1 5 . 6 . -
using System;
using System.10;
usi ng System.Col 1ecti ons.Generi ;
using System.Text.RegularExpressions:
public class Test
{
public static void MainO
^

&

364

15. #

15.6

()

StreamReader f

= new StreamReaderC "accessjog" );

StreamWriter w

= new StreamWriterC "report.htm" ):

Regex

get

Regex

new RegexC "GET" );


new RegexC " " );

. 15.1.

j?>block209-209-002. octetgroup.net j 162781 j


J81.24.130.7
jgate.

174756
sohro.

[212.113.108.164

ru
"

{113692}
~ ~ 12611991

. 1 5 . 1 . -
a c c e s s j o g , ,
items. , ,
GET 2 0 0 5 8,

365

XML

( 9) - ,
, 0.
H T M L - report.htm
. , , Internet Explorer.
,
1

.NET .

XML
X M L (extensible Markup Language) .
, , , , :
, , . .
, . X M L
: ,
. ,
, , :
<summary> </summary>
= ,
.
X M L
. X M L
. , X M L ,
.

.NET XML,
System. Xml .NET ,
X M L .
.

.
, . N E T

XML-.
, ,
/ / /
. ,
, ,- . .
. 15.5.
, Perl
.

366

15. #

1 5 . 5 .

<>

<code>

( <1>)

<example>

<exception>

<include file-''

path='nyTb[@name="Hfl"]" />
<Tist>

<param>

<paramref>

<permission>

<remarks>

(, . .)

<returns>

<see cref="">

<seealso ^="">

<summary>

(, . .)

<value>

:
///<summary> </summary>
/7/<param name="i"> </>
///<seealso cref="System.Double"> double </seealso>
///<returns> </returns>
///<remarks>
/// </remarks>
public double Sin( double i ) { ... }

1
1

X M L

/doc: _. xml. ^
Visual Studio (Project Properties) Build (
XML documentation file).
.

Visual Studio 2003 H T M L


Tools Build Comment Web Pages.

,
,
# : - -, ,
A D O . N E T . .

,
, .

ADO.NET
,
. . N E T
A D O . N E T (ActiveX D a t a Objects),
.
A D O . N E T :

(content

components)

(managed-

provider components). DataSet,



. , DataTable,
DataRow, DataColumn DataRelation.
. DataSet DataTable
DataRelation. DataSet , X M L ,
.

( ).
Connection, Command DataReader. DataAdapter

.
, X M L - ,
, Excel.
A D O . N E T :

DataSet;

- ;

, DataSet, .

A D O . N E T System.Data, System.Data.Common,
System.Data.01 eDb, System.Data.SqlClient System.Data.SqlTypes.

Visual Studio . N E T ,
. M S D E . Server
Explorer (View Server Explorer) S Q L Server
.
, .

ASP.NET
A S P . N E T (Active Server Pages for . N E T )
- .NET, - -.
,
,

368

15. #

. ,
,
.
: F T P - , - (Apache, IIS), ,
.
- , W o r l d W i d e W e b .

,
. - H T T P /
H T T P S . - :

Apache ( - ;
U n i x - );

IIS (Internet Information Services) Microsoft.

IIS Microsoft , Win


dows 2000 Professional . IIS

Windows.
aspnetregiis, .

- , IIS ,
. - aspx. ASP.NET Web Application.
, Location U R L - ,
. ,
W i n d o w s - , -
Web Form Controls, H T M L , Windows Forms.

- , W i n d o w s -
: ,
.
H T M L -
aspx ( HTML )
# aspx.cs.
, System.Web.UI.Page. (aspx-)
. ,
A S P . N E T .
Web
Form Controls A S P . N E T ,
-
. # ,
aspx-. ,
, .

369

ASP.NET , DataGrid DataList,


, ( ADO DataSet).

-
.

X M L ,
. -

, H T T P S M T P .
- . N E T .NET,
IIS-. - , . N E T :
, , ,
, .
,
.
, ,
, -
.
, - (
, ) .
- ,
XML-, H T T P .
-, A S P . N E T ,
IIS-. - asmx.
, aspx-,
# asm.cs, -.
, -, System.Web.
Services.WebService. - ASP.
NET Web Service.

# ,
,
.NET. ,
, [13], [20], [21], [23], [27], [ 3 1 ] - [ 3 3 ] .
,
, H T M L , X M L .

! , ,
.


1.

: 1-3.
.
(
). Math,
# , . 64. ,
. , ,
.
1.

zt = cos + sin + cos + sin ; z 2 = ^ - ^ s i n ^ - 8 j .

2.

z, = cos + sin + cos + sin ; 2 = 2>/2 cos

zx =

sin 2 + sin5a - sin


j

5.
6.

8.

2fll

a\
-cos

4J
2

z2 = 4 cos cos^a cos 4a.


- . a

+ ;
1 8

= 2 S I N A .

z2 = cos a + cos a.

Zj = cos a + cos 2a + cos 6a + cos 7a;


2(3

2 j -

._

;
cos a - cos 3a + cos 5a
2
, = l - - s i n 2 a + cos2a;
4

z<=cas,\-n

z2=tg3a.

zj=

.
z

cosa + l - 2 s i n 2a
sin2a + sin5a - sin3a
4.

sm

4/

z, = cos x + sin + ^ sin 2x -1;

=
2

sin.
2

z2 = sin(y + x) sin(y -

2.

371

2.

: 4, .

1.
,
, . R .

372

374

376

2.

, ,
, .
.

3.

379

3.

: 4, ,
.

1.
,
(. 1 2 ) ,
dx. , .
.

2.
, ,
2 2.

3.
,
, # dx .
.
,
.

380

381

4.

( lf

2*

3 +

1
>-.
2

"

1 - 3 - 2 -1)

1
arcsinx = + >
- - - =
2-4 - 2 n ( 2 n + 1)
21

19.

- +

1-3-
+

2-3
20.

1-3-5*

+
2-4-5

1-3-5-7-
+

2-4-6-7

.
2-4-6-8-

<1.

arccosx=
2

2-4--- 2 ( 2 + 1 )
7

1-
1-3-5-*
= \ +
+
+
2 |^
2-3
2-4-5
2-4-6-7
3

1-3-5-7-*
+
...
2-4-6-8-

<1-

4.

: 4,
, 5.
, ,
: , , ,
. , ,
.
.
.

1
, ,
.

. : , ,
.
.
, .

2
, ,
.

. : , ,

382

.
.
, .

3
, .
, ,
. .
.
, .

4
,
.
.
.
, .

5
.
, .
, .


,
.
( ),
, , .
, .

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

8
.
(, , )

4.

383

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

10

2 + + .
, :

,
-;

, .
11
, .
, , .
.
.
, .

12

, .
, ,
. .
, .

13

,
.
.
.
, .

384

14 ,
. , , .
, .

15
, ,
.
. .

.
, .

16
, ,
.
.
.
.
, .

17
, - c o s ( x ) = 0.
,
(. for)
.
.
, .

18
2

, + + = 0.
,
. .
, .

19
, , ,
.
.
. .

5.

385

,
.
.
.
.
, .

20
. .
(, , ).

. [0, 255].
.
.
, .

5.

: 6, .

1
, , :

;
,
.

2
, , :

;
,
.

3
, , :

386

,
.

,
, (, ,
).

4
, , :

;
,
.

, ,
. .

5
, , :

;
,
.

, ,
[, ]. .

6
, , :

;
,
.

,
, , .

7
, , :

,
.

,
, ,
, .

1 5.

387

8
, , :

;
,
.

,
, , .

9
, , :

;
,
.

, , ,
.

10

, , :

;
, ,
.

,
, , ,
.

11
, , :

;
,
.

, ,
[, ]. .

12

, , :

;
,
.

388

,
, [, ], .

13
, , :

, ;

, .

14
, , :

, ;

, .

15
, , :

, ;
,
.

,
, (, ,
).

16
, , :

;
,
.

17
, , :

;
, ,
.

,
, , .

6.

389

18

, , :

, ;
,
.

, ,
20%, .

19

, , :

;
,
.

20

, , :

, .

, ,
, .

6.

: 6, .

1
. :

, ;

, .

2
. ,
.

. ,
.

390

3
. :

, ;

, .

4
. :

,
;

,
.

5
. :

, ;
,
.

6
. :

,
;

, ^
- j- .

7
8 x 8 , k-
k- .
,
.

8

. ,
.
,
.

6.

391

9
A{j , i - l < k < i + l ,
j - 1 < l<j + 1, (k, I)

,

.
10 10.
,
.

10
,
( .
9).
10 10.
, .

11

.
.
,
.

12
, ,
.
, .

13

( ),
.

14
N
k :
, ,
, ;
.

392

15
.
, .

. ,
.

16

.
,
.

17
,
,
(2, 2), (3, 3) . .,
.
,
.

18
. :

, ;
,
.

19
. :

,
;

,
.

20
. :

, *
;

7.

393

, -
t- ^'- .

7.
: 6, .

1
,
.

2
,
, .

3
,
, .

4
,
, .

5
, ,
.

6
,
, .

7
, ,
, .

8
,
, , .

394

9
,
, .

10 <
,
, .

11
,
, .

12
,
, ,
.

13
,
, ,
.

14
, ,
0 9 , ,

15
, ,
, .

16
,
, .

17
, ,
,
.

8.

395

18
,
.

19
,
, , .

20
,
, .

8.

: 7.
, ,
: , , ;
, ; .
, ,
. .
.

1
().
:

;

;

, .

2
.
:

396

;

;

, .

3
,
. :

;
,
-;

, ;

, .

4
,
,
, .
, .

5
,
. , , :

, ;

, .

6
.
, - ( -

397

8.

', ), ,
, .
, .

7
. , - (,
, ), ,
.
[ , .

( 8
) . > , - (iep, , , ), , , .
1 , .

9
,
; . :

, ( , );

(+=,

-=);

/;

, .

10
,
. :

, ( , );

(*=,

/=);

, .

398

11
,
. :

, (, ,
, , , , );

/;

, .

12
, :
, , .
, .

13
.
, , .
, , .
, ,
, .
, .

14
.
, ,
.
,
, .
, .

15
,
. .
, ,
6 .
, .

16
, :

8.

( );

399

.
, .
:

;
,
;

,
;

, .

.
, .

17
, :

, ;

.
, .
:

;
,
; , ;

, ;

, .

, .

18
, :

.
, .
:

400

,
;

,
;

,
.

.
, .

19
, :

, ;

( ).

.
, .
:

,
; , ;

, ;

, ;

, .

, .

20
- ,
.
.
.

9.

: 8.
(, ),

.
,

9.

401

.
Equals, , .
Mai ,
.
.

1
Point (). ColoredPoint Line
(). Line ColoredLine PolyLine (
). :

;
,
;

2
Vehicle ( ).
Plane (), () Ship ().

(, , . .) .
,
, .
.

3
.
:

word .

:
Q ;

, ;

, ;

( ).

_.
, i.

402

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

33il2
-700
+5i - 21
_ :

4
3.
_.

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

, ;

( );

( ).

5
3.
_.
' 0' ' 1'.
,
, .
.
.

9.

403

_ :

, ;

( );

6
1. .
:
( );
;
.
:
;
, 1
;
, .
:
( );
;
.
2. ,
( ),
.
.
:
;
, ;
,
;
, ( ).
3. ,
. ,

404

(. ), .
, .
:
,
( );
;
.
:
( );
;
, ;
,
;
, ( )
;
, == .
4. , .
:
;
;
;
(),
.
:
, ;
,
;
, .
,
pri vate.
.
,
,
, / .
5. ,
.
.
,
:

405

10.

2-

RS

RST

10

12

RS

10

JK

11

10

10

JK

11

RS

12

12

RST

13

10

14

2-

10

15

10

16

JK

17

RS

10

18

19

JK

20

2-

10

10.
: 9.

1
STUDENT, :

( ).

, :

, STUDENT
( );

, ,
, 4,0 ( ,
).

406

2
STUDENT, :

( ).

, :

, STUDENT
( );

,
4 5 ( , ).

3
STUDENT, :

( ).

, :

, STUDENT
( );

,
2 ( , ).

4
AER0FL0T, :

, :

, AER0FL0T
( );

,
, ,
( , ).

5
AER0FL0T, :

10.

407

, :

, AER0FL0T
(
);

,
, ( ,
).


WORKER, :

, :

, WORKER
( );

,
, ( ,
).

7
TRAIN, :

, :

, TRAIN
(
);

,
( ,
).

8
TRAIN, :

408

, :

, TRAIN
( );

, ,
( ,
).

9
TRAIN, :

, :

, TRAIN
( );

,
( , ).

10
MARSH, :

, :

, MARSH
( );

,
( , ).

11
MARSH, :

, :

, MARSH
( );

,
, (
, ).

1 10.

409

12
NOTE, :

, ;

( ).

, :

, NOTE
( );

,
( , ).

13
NOTE, :

, ;

( ).

, :

, NOTE
( );

,
, ( ,
).

14
NOTE, :

, ;

( ).

, :

, NOTE
( );

,
( , ).

15
ZNAK, :

, ;

( ).

410

, :

, ZNAK
( );

,
( , ).

16
ZNAK, :

, ;

( ).

, :

, ZNAK
( );

, ,
( ,
).

17
ZNAK, :

, ;

( ).

, :

, ZNAK
( );

, ,
( , ).

18
PRICE, :

, ;

, :

,
PRICE (
);

1 1 .

411

,
( , ).

19
PRICE, :

, ;

, :

,
PRICE (
);

, ,
( ,
).

20
ORDER, :

, :

,
ORDER (
);

, ,
( ,
).

1 1 .

: 9, 13.
9,
.
IComparable
-
.

412

12.
Windows-
: 14.

1.
: Windows-,
. . ., .
, -

1
Input, Calc Exit.
Input , :

TextBox ;

( ) CheckBox;

Button.

: /
.

Calc .
Exit .

2
Size, Color, Paint, Quit.
Paint . Quit .
Size , :

TextBox ;

(Red, Green, Blue) CheckBox;

Button.

Paint .
Paint
,
.

12. Windows-

413

3
Input, Work, Exit.
Exit .
Input , :

TextBox Radius, Height, Density;

(Volume, Mass) CheckBox;

Button.

, ;

: / .

Work .

4
Input, Calc, Draw, Exit.
Exit .
Input , :

TextBox Radius;

(Square, Length) CheckBox;

Button.

;
: (Square) /
(Length).

Calc .
Draw
, (
).

5
input, Calc, About.
About .
Input , :

TextBox Number 1, Number 2, Number 3;

(Summ, Least multiple) CheckBox;

Button.


: (Summ) /
(Least multiple). Calc
.

414

6
Input, Calc, Quit.
Calc . Quit .
Input , :

TextBox Number 1, Number 2;

(Summa, Max divisor, Multiply) CheckBox;

Button.

;
(
, ,
).

Calc .

7
Begin, Help, About.
About .
Begin , :

TextBox input;

Label ;

(2, 8, 16) RadioButton;

Button Do .

input;
:
, .

Do .

8
Input color, Change, Exit, Help.
Exit .
Input color , :

TextBox Red, Green, Blue;

(Left, Right) CheckBox;

Button.

RGB- .
Change (,
).

12. Windows-

415

9
Input size, Choose, Change, Exit.
Exit . Change
. Input size , :

TextBox Size , Size ;

Button.

Choose , :

(Increase, Decrease) RadloButton;

Button.

Size Size .
,
( ).
Change .

.

10
Begin, Work, About.
About .
Begin , :

TextBox Input word;

(Upper case, Lower case) RadioButton;

Button.


.
Work .

11
Input color, Change, Clear.
Input color , :

(Up, Down) CheckBox;

(Red, Green, Blue) RadioButton;

Button.

;
, , : ,
.

Change (
, ).
Clear .

416

12
Translate, Help, About, Exit.
Exit .
Translate , :

TextBox Binary number;

TextBox (read-only);

(8, 10, 16) RadioButton;

Do Button.

Binary number;
:
, .

Do .

13
Reverse, About, Exit.
About .
Reverse , :

TextBox Input;

(Upper case, Reverse) CheckBox;

Button.

:
/
.
.

14
Input, Show Exit.
Exit .
Input :

12. Windows-

417


length koef: , ,
/ .
Show
.

15
Input, About Exit.
Exit .
About .
Input :


. Euro $ .

16
Begin, Work, About.
About .
Begin , :

TextBox;

(First letter, All letters) RadioButton;

Button.


: (First letter),
(All letters). Work
.

17
,
,
.

418

OpenFileDialog.

.

,
.

18

H T M L . :



HTML

OpenFi leDialog.
SaveFileDlalog.
Memo.
H T M L :

&nbsp;;

<BR>;

< &lt;;

> &gt:;

& &;;

" ( ) Squot;.

19
Input, Draw, Clear.
Input , :

(Red, Green, Blue) RadioButton;

Button.

12. Windows-

419

D r a w
, .
Clear .

20
Input, Change, Exit.
Exit . C h a n g e
. 100 100 .
Input , :

TextBox Size , Size ;

(Increase, Decrease) RadioButton;

Button.

Size Size .
,
, (
).
C h a n g e .

. ,
.

21
Windows-,
.
Choose, S h o w , Quit.
S h o w . Quit .
.
.
, (price) (hard drive).
C h o o s e , :

TextBox ;

TextBox ;

(Hard drive, Price) RadioButton;

O K , Cancel Button.

S h o w .
S h o w , ,

.

422

(
).
Animate.
Animate
.
.
. .
.

5
Windows-

= - + - + -log2 .
Open, Coefficients, Show, About, Exit.
Exit . About
. ( Open)
OpenFileDialog.
()
.
Coefficients
,. Show
.
. .


#

.

(currency) .
( $ ) .
,
NumberFormatlnfo.
,

D d

.
, .
, , 12 D3
012

, d.ddd..E+ddd
d.ddd...e+ddd.
, .
3

F f

.
,
, , 0,12, F3,
0,120

G g

.
,
.
,
; single 7 , byte sbyte 3, decimal 29

&

424

. #

[)

d,ddd,ddd.ddd...,
,
.
,

(, 100,
%)

. ,

.
X,

using System;
namespace ConsoleApplicationl
{

class Classl
{
static void MainO
{
int i = 1234;
Console.WriteLineC i.ToStringC "C" ) );
Console.WriteLineC i.ToStringC "D5" ) );
Console.WriteLineC i.ToStringC "E" ). );
Console.WriteLineC i.ToStringC "G" ) );
Console.WriteLineC "{0,9:n2}", i ) ;
Console.WriteLineC

"{0,l:p3}",

Console.WriteLineC

"{0.1:x}",

i
i

);
);

}
}
}
:
1 234.00.
01234
1.234000+003
1234
1 234,00
123 400,000%
4d2
. 146.


1. . . # . - .: - -
- . , 2006. - 488 .
2. . -,
. - . : -, 2000. - 304 .
3. . #. - . : , 2004. - 880 .
4. . . - : , 2001. 352 .
5. . .NET Java-. - .: , 2003. 336 .
6. . .

++. . - .: , 1996. - 272 .

7. . # . . - .: , 2001. 304 .
8. . Visual + + .NET. - .: ; :
B H V , 2004. - 528 .
9. . # . - .: -, 2003. - 688 .
10. . # . : .
: , 2002. - 656 .
11. . # Builder. . .: , 2005. - 384 .
12. . # . : . : , 2002. - 656 .
13. . A S P . N E T # . - . :
, 2003. - 304 .
14. . . C / C + + . :
. - .: , 2001. - 464 .
15. . . . :
. - .: , 2003. - 393 .

426

16. ., . : Visual + + .NET. .: ,


2002. - 816 .
17. . MS Windows # . . 1. . : - , 2002. 576 .
18. . MS Windows # . . 2. . : - , 2002. 624 .
19. . . + + / # Visual Studio . N E T 2003.
. .: -, 2004. 352 .
20. ., . Visual C # . N E T . .

: ,

2004. - 960 .
21. , ., . . # . 2 . .:
, 2003. - 512 .
22. . # . .: , 2002. 352 .
23. . + + # . .
.: , 2003. - 608 .
24. . # . "". .: -,
2001. - 576 .
25. . # . :
, 2003. - 528 .
26. ., X. . .NET. . .: -, 2003.
336 .
27. . # .NET. . .: ,
2002. - 796 .
28. . ., . . # : . .: - ,
2003. - 560 .
29. . # : . - .: , 2002. - 512 : .
30. . # . . : ,
2004. - 752 .
31. Microsoft Corporation. Windows- Microsoft Visual
Basic . N E T Microsoft Visual C# .NET. .
70-306, 70-316. . : -
, 2003. - 512 .
32. Microsoft Corporation. Web- Microsoft Visual Basic
.NET Microsoft Visual C # .NET. M C A D / M C S D . - M.:
- , 2003. 704 .
33. Microsoft Corporation. Web- X M L
Microsoft Visual Basic . N E T Microsoft Visual C# .NET.
M C A D / M C S D . M.: - ,
2004. - 576 .


!, , 43, 49
I-, , 43, 54
#define, , 288
#elif, , 288
#else, , 288
#endif, , 288
#endregion, , 288
#, , 288
#if, , 288
#line, , 288
#pragma, , 288
#region, , 288
#warning, , 288
%, , 43, 51
% = , , 43
&&, , 43, 56
&, , 43, 55
&=, , 44
( ) , , 42
*, ,. 43, 50
* = , , 43, 57
, , 42
-, , 43
--, , 42, 43, 47
-, , 48, 53
., , 42
/, , 43, 50
/ = , , 43
?, , 43, 56
??, , 310
@, , 30

, , 43, 55

= , , 44
|, , 43, 55
|, , 43, 56
| = , , 44
-, , 43, 49
+, , 43, 52
+ + , , 42, 43, 47
+ = , , 43, 57
<, , 43, 54
, , 43, 5 4
= , , 4 4
< = , , 43, 54
>, , 43, 54
, , 43, 5 4
= , , 4 4
> = , , 43, 54
=, , 43, 57
- = , , 44, 57
, , 43, 54

Abort, , 240
Abs, , 65
abstract, ,
101,181

Component, , 337
Concat, , 144
Console, , 19, 59, 262
const, , 41
ContainerControl, , 338
ContextMenu, , 331

Acos, , 65
ADO.NET, 367
ANSI, 22
API, 312
Append, , 147
AppendFormat, , 147
Application, , 342
Array, , 133
ArrayList, , 296
as, , 43, 194
Asin, , 65
ASP.NET, 367
AsyncCallback, ,
242, 255
Atan, , 65
Atan2, , 65
base, , 117, 175
Beginlnvoke, , 242
BeginRead, , 250, 253
BeginWrite, , 250
BigMul, , 65
BinaryFormatter, , 268
Binary Reader, , 261
Binary Search, , 134
BinaryWriter, , 260
break, , 84
Brush, , 344
Button, , 326
Capture, , 363
case, , 74
catch, , 90
Ceiling, , 65
char, , 139
CheckBox; , 331
checked, , 42, 46
class, , 100, 305
Close, , 250, 255
CLR, 9
CLS, 14

Exception, , 89, 94
, , 65
explicit, , 168

Color, , 344
Combine, , 222
Compare, , 143
CompareOrdinal, , 144
CompareTo, , 144

File, , 263
FileAccess, , 249
FileAttributes,
, 265
Filelnfo, , 263

continue, , 86
Control, , 323, 338
Convert, , 61
, , 134, 144
Cos, , 65
Cosh, , 65
D

DataSet, , 367
decimal, , 34
default, ,
74, 306
delegate, , 220
Deserialize, , 268
DialogResult, , 327
Dictionary<T,K>, , 302
Directory, , 263
Directorylnfo, , 263
Dispose, , 345
DivRem, , 65
DLL, 312
do while, , 79
, , 65
Endlnvoke, , 242
EndRead, , 250, 253
EndWrite, , 250
Enum, , 218
enum, , 215
Epsilon, , 73
Equals, , 184
8-,
24,29
event, , 233
EventArgs, , 235
EventHandler, , 235

428
FileMode, , 249
FileShare, , 249
FileStream, , 250
FileSystemlnfo, , 263
finally, , 90
FindMembers, , 280
fixed, , 352
Floor, , 65
Flush, , 250, 255
Font, , 344
for, , 81
foreach, , 136
Form, , 337, 338
Format, , 144
G

GAC, 275
get, , 121
GetConstructors, , 280
GetEnumerator, , 207
GetEvents, , 280
GetFields, , 280
GetHashCode, , 184
Getlnterfaces, , 280
GetMembers, , 280
GetMethods, , 280
GetName, , 218
GetNestedTypes, , 280
GetNumericValue, , 140
GetProperties, , 280
GetType, , 184,280
global, , 287
goto, , 83
Graphics, , 344
Group, , 363
GroupBox, , 332
IAsyncResult, , 255
ICloneable, , 205
IComparable, , 199
IComparer, , 200
IDisposable, , 345
IEEERemainder, , 65
IEnumerable, , 198,
207, 219
IEnumerator, , 198,
207, 219
if, , 70
IIS, 368
ILDasm, , 273
implicit, , 168
IndexOf, , 134
Insert, , 144
internal, , 101
is, , 43, 194
IsControl, , 140
IsDigit, , 140

IsLetter, , 140
IsLetterOrDigit, , 140
IsLower, , 140
IsMatch, , 360
IsNumber, , 140
IsPunctuation, , 140
IsSeparator, , 140
IsUpper, , 140
IsWhiteSpace, , 140
JIT-, 9
Join, , 144
L N

Label, , 326
LastlndexOf, , 134
Length, , 144
List<T>, , 300
ListBox, , 334
lock, , 242
Log, , 65
Log 10, , 66
Main, , 156
MainMenu, , 330
MarshalByRefObject, , 337
Match, , 361
Match, , 361
MatchCollection, , 362
Matches, , 362
MatchEvaluator, , 363
, , 66
MaxValue, , 34, 140
MDI, 337
MemberwiseClone, , 205
Menultem, , 330
MessageBox, , 322
Min, , 66
MinValue, , 34, 140
Monster, , 119
MSIL, 9
NaN, , 34
new
, 175, 305
, 42, 48, 102
, 101
Next, , 149
NextBytes, , 149
NextDouble, , 149
NextMatch, , 362
null, , 30
nullable, , 36,309
O

object, , 13, 183


OLE, 312
operator, , 161
out, , 113

override, , 179
params, , 154
Parse, , 61, 140
partial, , 308
Peek, , 256
, , 344
PI, 66
Pow, , 66
private, , 101
protected, , 101
public, , 101
RadioButton, , 331
Random, , 148
Read, , 62, 250
ReadBlock, , 256
ReadByte, , 250
ReadLine, , 62
readonly, , 105
ReadToEnd, , 256
ref, , 112
ReferenceEquals, , 184
Regex, , 360
RegexOptions,
, 360
Remove, , 144, 225
Replace, , 144, 362
return, , 87
Reverse, , 134
Round, , 66
Run, , 316
S

sealed, , 101,182
Seek, , 250
Serialize, , 268
set, , 121
Sign, , 66
Sin, , 66
Sinh, , 66
sizeof, , 352
Sleep, , 240
Sort, , 134
Split, , 144,303,362
Sqrt, , 66
stackalloc, , 354
Start, , 241
static
, 118
, 101
Stream, , 250
StreamReader, , 255
Stream Writer, , 255
string, , 143
StringBuilder, , 147, 151
strong name, 275
struct, , 212, 305

429

Substring, , 144
switch, , 73
System. Collections,
, 295
System.Collections.Generic,
, 295, 300
System.Collections.Specialized,
, 295
System.Drawing,
, 344
System.Reflection,
, 279
System.Text. RegularExpression,
, 355
System.Windows.Forms,
, 315
Tan, , 66
Tanh, , 66
TextBox, , 327
TextReader, , 255
TextWriter, , 255
this, , 114
Thread, , 239
ThreadStart, , 239
throw, , 93
ToCharArray, , 144
ToLower, , 140
ToString, , 60, 184
ToUpper, , 140
try, , 90
, , 280
typeof, , 42
unchecked, , 42, 46
Unicode, , 22, 24
unsafe
, 348
, 348
using
, 286

, 345

value, , 122
virtual, , 178
178

void, , 109
volatile, , 105
where, , 305
while, , 76
Write, , 60, 250
WriteByte, , 250
WriteLine, , 19, 60
XML,

, 11
, 294
, 181
, 22
, 22

, 24
, 24
, 228
, 32
, 48
-, 253
, 242
, 293
, 283
, 87
, 10
, 215
, 182

, 312
, 10
, 164
, 292
, 40,70
, 62,246
, 246
-, 368
-, 368
-, 369

, 274
, 274
, 87
, 33
DOS-, 312
, 368
, 178

, 317
, 182

V-Z

VMT,

365

yield, , 208

, 101
, 169
, 32
, 239
, 246
, 23, 42, 70, 355
,
, 53

-
, 15, 330
, 337
, 275

, 294

, 102
, 102
, 248, 260, 271
, 291
, 291
, 47
, 220
, 242
, 221
, 220
, 226
, 225
, 182
, 50

, 292
, 293
, 267
, 169
, 326, 337
, 312
,
23, 287

XML, 365
, 237
, 32

, 274
, 30

, 248, 271
, 248
, 248
, 337
, 106
, 314
, 117
, 246
, 274
, 25, 42
, 48

, 35

, 274
, 24
, 24
, 12
, 284
, 24
, 100
, 19
, 275

430
, 126
, 157, 161
, 116
, 39
, 12
, 47
, 305
, 12, 124, 188
, 337
, 307
, 274

, 274
, 46, 89
, 89
, 223
, 207
, 75
, 11

, 368
, 368
, 13, 100
, 181
, 182
, 101, 169
, 172
, 172
, 299 '
, 118

, 205
, 292
, 25
, 326

, 32
, 121, 158
, 347
, 22
, 295
, 300
, 294
, 292
, 15
, 23, 359
, 30
, 30
, 8
, 314, 325
, 367

, 367
, 59
, 15
, 15
, 26, 42

, 114
, 117
, 117
, 174
, 48,115
, 117
, 117
, 295
, 331
, 292
, 9

-
, 23
, 24
, 59
, 293
, 26
, 30
, 29
, 49
, 39
, 273
, 28
, 126, 291
, 293
, 129
, 156
, 128
, 130
, 127
, 126
, 132

, 330
, 331
, 9, 273
, 355
, 84
, 18, 106
, 228
, 178
, 102
, 306
, 152
, 182
, 153
, 102
, 226
, 102

, 337
, 311
, 161

, 30
, 294

, 337
, 326
, 182
, 223
, 12, 172
, 347
, 348
, 143
, 337

, 45

, 274
, 274
, 274
, 25

, 40
, 111
, 309
, 306
, 8
, 233

, 91
, 313
, 359
, 226

, 14

, 9
, 11
, 101
, 211
-
, 69
, 161
, 305
, 128
, 291
, 291
, 30
, 311
, 337
, 326, 337
, 337
, 337
, 337
, 17
, 337
, 16
, 322
, 16

431

, 42
, 24
break, 84
continue, 86
lock, 242
, 83
, 70
, 87
, 73
, 136
, 83
, 70
, 70
, 75
, 42
, 164

, 354
, 53
, 47
, 50
, 105, 353
, 47
, 161
, 56
, 49
, 310
, 51
, 54
, 161
, 55
, 49
,
49, 168
, 57
, 351
, 351
, 54
, 52
, 48
, 50
, 162
, 48
, 56
, 18, 100
, 274
, 51
, 273
, 232

, 48
, 49
, 49
, 292
, 15

, 113
, 220
, 111
, 284
, 156
, 107
, 284
, 112
, 76

, 300

, 307
, 223
, 274
, 238
, 60

, 152
, 161

,
, 110
, 110
, 38
, 39
, 215
%
, 211
, 215
, 8

, 205
, 357
, 86
, 178
, 284
, 39, 104
, 13,153,180,188
, 182
, 232
, 55
, 49
, 28
,
248, 271
, 311
-, 246
, 239
, 238
, 238
, 172
, 24
, 172
, 45, 49
, 11

, 44

, 57
, 57
, 214
, 23
, 47
, 59

, 69
, 69

, 312
, 14
, 248
, 273
, 57
, 13, 285
, 299
, 237
, 130

, 287
, 286
, 239
, 70

, 312
, 351
, 9
, 26
, 127
, 152
, 351
, 177
, 36, 214

, 93
, 225
, 355
, 8

, 249
, 249

, 249
, 153
, 153
, 274, 345
, 279
, 15
, 337
, 9, 272
, 273
, 273

432
, 169
, 120

, 178
, 177
, 54
, 283
, 367
, 267
, 108
, 275

, 23
, 23
, 17
, 21
, 24
, 304
, 87
, 293
, 25
, 52
, 232,312
, 233
, 233
, 312
, 45, 350
, 313
, 306
, 70

, 220
, 101
, 157
, 101
, 215
, 233
, 212
, 146

, 291
, 291
, 292
, 291
, 291

, 9
, 8

, 35
, 118
, 117
, 102
, 102
, 31, 292

, 15
, 143
, 143
, 29
, 212
, 294

, 69
, 132
, 76

, 178
, 365

, 169
, 189
, 100
, 19,106
, 161
, 215
, 212
, 75, 137

, 32
, 33
, 169
, 32
, 31
, 220
, 35
, 19
, 143
, 309
, 215
, 304
, 233
, 35
, 32
, 308, 310
, 284

, 348
, 50
, 162
, 48
, 226
, 36, 214
, 312

, 29
, 56
, 248,260,271
, 368

, 226
, 19
, 161
, 294
, 19
-, 294
-, 293
-, 294
, 31
, 32
, 87
, 313
, 82
, 81
, 79
, 76
, 308, 310
, 273
, 246

, 224
, 146
, 13, 101

, 102
, 325

, 22
, 273

|



-

,
(60),
.

.
# 2.0 ( 2 0 0 5 )
, ,
, .
#
, BASIC + + ,
.
20
. - ^ & ^
h t t p : / / i p s . i f m o . r u .
, :
.NET;

#||

- ;
.NET;
, , , , , , ;
- ,

), ,

"

:
197198, -, / 619
.: (812) 703-73-74, postbook@piter.com
61093, -93, / 9130
.: (057) 712-27-05, piter@kharkov.piter.co

w w w . p i t e r . c o m