433
ДЛЯ ВУЗОВ Т. А. Павловская Программирование на языке высокого уровня ПИТЕР Москва • Санкт-Петербург • Нижний Новгород • Воронеж Ростов-на-Дону • Екатеринбург • Самара • Новосибирск Киев • Харьков • Минск 2009

Pavlovskaya t a s Programmirovanie Na Yazyke Vysokogo Urovny

Embed Size (px)

Citation preview

.

.

- --

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.

67

1. .NET- Visual S t u d i o . N E T ^ '

811 13 14 21

2.

2222 31 36

3. ,

3838 41 42 59 67

4. , checked unchecked

6970 70 75 87 89 95 95

5. : : this

100103 104 106 114 114 120 124

6. foreach Random

126126 136 138 139 148 150

4

7. : Main

152152 153 154 156 157 161 169 169 170

8. object

172172 178 181 182 183 186

9. . is as .NET

188188 190 194 195 198 212 215 219

10. ,

220220 232 237 245

1 1 . - - ()

246250 253 255 260 262 263 267 270

'. . .

12. , , ,

272272 275 278 279 283 285 287 290

5

13. , - System.Collections -

291291 295 299 308 309 310

14. W i n d o w s- Windows- Control Form Application

311312 314 323 325 337 338 339 342 344 346

15. # XML ,

347347 355 365 366 369

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

370370 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 + + . , # , , , . , # , -. # : -, , -, , , , , . N E T . , Microsoft # Express 2005, (http://msdn.microsoft.com/vstuclio/express/visualCsharp/), .1

1

, # Express 2005 W i n d o w s /2000, Pentium 600 128 .

7

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

# (http:// www.ecma-international.org/publications/standards/Ecma-334.htm) , . , 14 Windows, 15 - -. , , , , , . N E T . , # , , , , - . . , , , , , . - http://ips.ifmo.ru , . : , . , , , . , , , [email protected] - .

, [email protected] ( ) . ! - www.piter.com.

1

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

, . . N E T ( ) , Visual Studio.NET, , , , . , , : I riepettocztjvtocTnb ;

;

; ;

1

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

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

.NET

9

;

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

.

. N E T . 1.1. (. .dll) (IL ) CLR - ( ) 1 . 1 . 1 . .NET dll, IL . , , . , , . , .

t

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 (CLR) .NET Building Block Services

. 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 . . , , , . . , , . , , , . , . . N E T , , . N E T , object . 1

1

,

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

, , .; , ; , ;

Visual Studio.NET

15

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

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

(Assemblylnfo.cs).

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

Properties ( , View Properties ) . , .

Visual Studio.NET

17

. , , Classl, FileName ( Enter). , , . , . () 2 1

,

-, .

: , . , , . .

( 1.1). , . , . 1 . 1 . using System; namespace ConsoleAppli cati onl { / / / / / / Summary description for Classl. / / / class Classl { / / / / / / The main entry point for the application. / / / [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.2. # using System: namespace ConsoleApplicationl

1. .NET

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

2

# , .

. , , . # .

. , ( ) , . # 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 . # : ( ) (_), ; ; , +, *, { &; ( ); .

: , . (token ) , . : {);1

; ; ; ().

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

24

2.

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

, , , . . , . , , sysop, SySoP SYS0P . , . . . # . , gg . , escape- Unicode, \, , \u00F2. : 21 ate, Big g i g , #; , ( # ) .1

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

1

2

25

, .

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

, . , . # . 2.1.

, . . , += b += , b . , , , , & , | & ? ++

::||

+ . . > >

- * / * & |_

! -=

=*= /= %=

] = < = > = + =

&= | =

= = = ->

, , . # , , , , null. , , . . . 2.2. , , ( ). , .1

27 true () false () : (0, 1, 2, 3, 4, 5, 6, 7, 8, 9), ( U , u, L, 1, UL, Ul, uL, ul, LU, Lu, 1U, lu) : Ox O , X (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, , , , D, , F ) , , , ( U , u, L, 1, UL, Ul, uL, ul, LU, Lu, 1U, lu) true false 8 8u O u L 199226 199226L

2 . 2 . #

OxAU 0X00FF1

0x188 0xlB8LU

0X00FF

: [] [.] [] [] F, f, D, d, , m : [][.][]{|}[+|-][] [] F, f, D, d, , m

1

5.7 5.7F 5F

.001 .OOld .OOlf

35 35 35m

0.2E6 0.2E6D 5E10

.lle+3 .lle-3

5E-10

,

'A' '\0' '\xF' '\uA81B'

V '\n' '\x74'

'*'

,

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

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

, ( - ) , : -218, -022, -, -4.8, -0.14. , . - , , , . 2.3. , . 2 . 3 . L, 1 U, F, f D, d , m (long) (unsigned) ( f l o a t ) (double) (decimal)

. Unicode. , . 2.2: , ( ), ' ' , '', ' * ' ; ' \ 0 ' , ' \ ' ; 'VxF', '\74'; escape- Unicode '\uA81B'.

29, escape-,

, . : , (, \ ); , , , '. . 2.4 . , , . 2 . 4 . # \ \ \f \ \ \t \v () -

\\V V \0

, , \0, . 16

0 2 Escape-

- 1, .

Unicode

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

30 : ,

2.

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

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

I

31

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

/ , , . , , , : . . : , , ; ( ) . , , , ; , , , . . , , . . 1, , : (stack) , (heap) . , , . # .1

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

. , , , , !

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

. 2 . 1 . #

. , , . . , , , . , , . # . 2.5. . N E T , System , ? . , , . , , . . ( , 1 ). . ! , , , / . . , !

33i. # [

.NET Boolean SByte Byte Intl6 UIntl6 Int32 UInt32 Int64 UInt64 Char Single Double Decimal String

true, false -128 127 0 255 -32 768 32 767 0 65 535 -2 10 2 10 0 4 10 -9 10 18 9 1018 9 9

,

bool sbyte byte short ushort int uint long ulong

Unicode 7 15-16 28-29 Unicode

8 8 16 16 32 32 64 64 16 32 64 128

18

0 18 10 U+0000 U+ffff 1.5 10" 38 3.4 1045

1

char float double

5.0 10 308 1.7 10 1.0 10~ 28 7.9 10

324

decimal string

28

object

Object

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

, , , . , . , . : , 0,381 104

.

34

2.

381 4, 560,3 10

2

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. , ] , . , : # . , , , d 11 == 0 ) ) b++: e l s e { b *= : = 0: < 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 } ,7 1 // 2

} 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

1 2

case default

>

. 4 . 3 . switch :

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

741

4. |

. ( 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 { i s t r i n g buf; d o u b l e . b. res, " : " b u f i; " " ); ): s t a t i c void Main()

Corisole. Writpi j n e ' a = double. Parse Console.WriieLineC c h a r op Console.ReaciLmec);f

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

(char)Console.ReadO:

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 . , , , , . , . . . , . , . , . break, continue, return goto ( . , . 8 3 ) . ( ) .1

while :

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

, .

77

. false, . , .

, , , : 't, = = 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:f

4. /

I",

x. ):

// 11 //' //

2 2 3 4

, ( ( 6 ) . . .

, , . , 4. t c k j , . , Ctrl+Break, : ; ; .1

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 " ):

79break: case "2 // Console.WriteLineC ne( "oi;idaxa break; return; : Console.WriteLineC break; " !" n y i K i a - _ 2 " ), 2

case " 3 " default

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. , , . . . , (, ). . , , , , . , . ( 4.6) , . , , , ( ) , . 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 );2 1

.

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; "| x | |"; // // 1 , 4 , 5 // 2 // 2 &

Console.WriteLineC

f o r ( d o u b l e x = Xn; x = 0 && x < 10 ) = t * x ;

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

4.

/ / 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).1

, . 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 ( ) = 10~ = 1+ 2!2 6

+

4

+

6

+... 6!

4!

" + +... 2!

2

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

85 ( , , ). . break. , , , , , . . , , , , . : , , . : Cn+i :

1

=

C,i ' ^

. +1,

, _~ "2

2\ - - (2( + 1 ) ) !

2

=

2

(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 += c h ; if ( n > M a x l t e r ) + l ) / ( 2 * n + 2 ) ; } " + ); );

// { 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 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 a m = 10 m o health = 50 a m = 50 m o health = 80 a m = 80 m o

, . . # I Comparer, .

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

.NET

201

, , . ( ). ( 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 { p u b l i c Monster( int health, i n t ammo, s t r i n g name ) Monster

{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 { Console.WriteLineC "Monster {0} \t h e a l t h = {1} name, h e a l t h , ammo ); ammo = { 2 } " , public void PassportO

}p u b l i c c l a s s SortByName : IComparer o b j e c t ob2 ) //

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

202 9.2 ()

9.

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( m1.Name, m2.Name );

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

:

IComparer object obi, o b j e c t ob2 )

IComparer.Compare(

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 ) r e t u r n 0. return 1; if ( ml.Ammo < m2.Ammo ) r e t u r n - 1 ;

string mt

name: ammo;

health,

Class{

Class1

s t a t i c void M a m ( ) const int n=3. s t a d o new M o n s t e r ] , "" "" "" ); ); ): ); j;

Monster3

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

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

) elem.Passport();

: : Monster M o n s t e r M o n s t e r h e a l t h 50 ammo h e a l t h = 40 ammo h e a l t h = 80 ammo 50 10 30

.NET

20310 50 80

: M o n s t e r M o n s t e r Monster h e a lltth = 40 ammo hea h h e a lltth = 50 ammo hea h h e a lltth = 80 ammo hea h

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 { p u b l i c Monster( { this.health = health; this.ammo = ammo; this.name = name; Equals( o b j e c t obj II GetTypeO ) ) return false; int health, i n t ammo, s t r i n g name ) : IComparable

p u b l i c o v e r r i d e bool { if ( obj == n u l l

!= obj.GetType()

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 == temp.ammo == temp.name; &&

public override

i n t GetHashCode()

iP

204 { return name.GetHashCode(); 9.3 ()

9.

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

public int CompareTo( object obj

)

.NET

205

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 e l s e if else ( > ) ( = ) "" ); "" ); " " " " ); ); " " );

Console.WriteLine( Console.WriteLine( Console.WriteLine(

} } :

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

206

9.

, , , , . . (. 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 { c l a s s Monster { p u b l i c Monster( i n t h e a l t h , { this.health = health: this.ammo = ammo: this.name = name; i n t ammo, s t r i n g name ) : ICloneable ConsoleApplIcationi

}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 { Console.WriteLineC "Monster {0} \t h e a l t h = { 1 } ammo = { 2 } " , name, h e a l t h , ammo ); public void PassportO

}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

.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 { 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; // 2 StadoO

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

.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 N m : IEnumerable u { 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 ) .

210namespace { { public static IEnumerable Count( ConsoleApplicationi class Classl

9.

int from,

int to )

{from = 1; while ( from = 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, { a = 44; b = 33; Console.WriteLine( " {0} {1}", a, b ); ref int 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 " {0} {!}", a, b ): " {0} {1}", a, b );

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 { s t a t i c v o i d P( i n t a. out int b ) Classl

{ }^

a = 44; b = 33: Console.WriteLineC void MainO

"

{0}

{1}".

a,

b );

static

&

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

5. :

{ 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 { { Demo a = new Demo( 3 0 0 , 0 . 0 0 2 ) Console.WriteLineC Console.WriteLineC a.GetyO b.GetyO ): ); Demo b = new Demo( 1, 5 . 7 1 ); // // ; // : 0,002 5,71 // Classl s t a t i c void MainO

}}

}

116

5. :

, . , : class Demo { public D m C int a ) e o // 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 D m C 5.71 ); e o

// 1 // 2

. - , -, . this , : class Demo { public D m C int a ) e o // 1

{this.a = a;

}public D m C int a, double ) : thisC a ) e o { t h i s . = ; } // 1

}, . , # object. , , . , , ,

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

II []

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; a l . P r i n t ( " 1 + 100" ); al = 100 + a l ; al.PrintC "100 + 1" ); a2 += ++a2 + 1 ; a2.Print( "++a2, a2 + a2 + 1" ); 113 ! // 2 // 1

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

: 1: 5 i 2: 2 -1 3 3 100 200 9 102 202 99 199

1 0 1 2: 7 3 J 1 + 100: 106 103 100 + 1: 206 203 ++2. 2 + 2 + 1: 5 3

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

168

7. :

, . 3 , .

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

. :

implicit operator ( ) explicit operator ( )

// //

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

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

// //

:

, ; , ; ; .

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

. ( ) . , . , Monster Gun. , : using System; namespace { { class G n u { ConsoleApplicationi class Monster1

}

} } : , . 9.

, , , ( p r i v a t e ) : , , ; , ; , . , , , , , , . (, + -, ). - , , + ++ ( ). , .1

: (. . 104).

171

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

8

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

, , , .

# .

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 ) this.brain = brain;

: base( name )

// 1

public Daemon( int health,

int ammo,

string name,

int brain ) // 2

: base( health, ammo, name ) this.brain = brain;

new public void PassportO

// 3 &

174 { Console.WriteLineC 8.1 ()

8.

"Daemon { 0 } \t health = { 1 } a m = { 2 } brain = { 3 } " . m o 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 ) Dima. Passport (); Dima.ThinkO; Dima.Health -= 10; Dima.PassportO; // 5 // 6 // 7 // 8

}

} } Daemon brain Think, , Passport. Monster Daemon . : Daemon Dima Dima is Daemon Dima health = 100 a m = 100 brain = 3 m o health = 90 a m = 100 brain = 3 m o1

thinking thinking thinking...

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

Monster , Daemon, .

175

, . Daemon.

, , , . , , , . , .

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

, , new . 8.1 Passport. . Passport Daemon , . base, : case.PassportO: , . , , , . , .1

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

, . (warning) , , ...

176

8.

, private, . Passport name, health a m m o . , protected, , Monster. . , , .

, . , . : , ; , , ; , ; , . , 2 1

.

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

}class Daemon : Monster1

2

, . , . .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; for ( int i = 0; i < n; ++i ) stado[i].Ammo = 0 ; Console.WriteLineC); foreach ( Monster elem in stado ) el em. PassportO; // 1 111

// 3

}}} : Monster Monia Monster Monk Monster Dimon Monster Monia Monster Monk Monster Dimon health = 100 ammo = 100 health = 100 a m = 100 m o health - 100 a m = 100 m o 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 { Console.WriteLineC "Monster { 0 } \t health = { 1 } a m = { 2 } " , m o name, health, a m ); m o public void PassportO

} }class Daemon ; Monster { override public { Console.WriteLineC "Daemon {0} \t health = { 1 } a m = { 2 } brain = { 3 } " , m o Name. Health, Ammo, brain ); void PassportO

} }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 { class 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 // Demo System; ConsoleApplicationl

namespace

{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 : Monster Monia Monster Monk Daemon Dimon Monster Monia Monster Monk Daemon Dimon health = 100 a m = 100 m o health = 100 ammo = 100 health = 100 ammo = 100 brain = 3 health = 100 a m = 0 m o health = 100 a m = 0 m o health = 100 a m = 0 brain = 3 m o

8.

, 1 3 Passport, , . . . : , . : V M T , V M T , . , , . , , V M T , .

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

181

. , , . . . , , . , . , . abstract. , , . .

, , : abstract class S p i r i t i

public

abstract :

void

PassportO:

}class Monster { override public void PassportO { Console.Writel_ine( "Monster { 0 } \t health = { 1 } a m = { 2 } " , m o name, health, a m ); m o Spirit

} }class Daemon : Monster

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

}. . . // 12

182

8.

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

# s e a l e d , , , , : sealed i 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 { class { p u b l i c v o i d ) ConsoleApplicationi

object

183Console.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 , , , . . , . : public Type Get ;1

ReferenceEquals

true,

. : public static bool( object obi, object ob2 );

ToString , , . , . : public virtual string ToStringO j ! j

. , Equals , , ( ) , ( ). object Monster 8.5.1

*

12 (. . 279).

object

185

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 && a m m o name == temp.ammo == temp.name; &&

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

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

}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.5 () if ( X == Z ) Console.WriteLineC X == Z " ) ; else Console.WriteLineC X ! = Z " ) ;

8.

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

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

, , .

187 , . , , , . , , , , . , , . , . , , . , , . , , , -. ( ) , , new, , . , , , , ( ) . . , . Y X Y X. , Y X ( Y X ) . , , X Y , Y X, Y X ( Y X ) .

9

, .

. , , . , , . , . -. : - . :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, A m Name m o ). .

: publ i , ; ;

Console.WriteLineC x . G e t y O // Console.WriteLinet GetsO ); ); ); // // s //

109

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

} }} , . , 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 { s t a t i c i n t MaxCint a , { if ( > b ) r e t u r n ; else r e t u r n b; i n t b) / / Classl

}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 : { public void DrawO { Console.WriteLineC " " + name ): IAction

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

191r e t u r n ;

}public { Console.WriteLineC h e a l t h = 0: " M o n s t e r " + name + " RIP" ); void OieO

}public I ciet { r e t u r n ammo * h e a l t h ; int Power

}, . publ i c . , 1: Monster V a s i a = new MonsterC 5 0 . 5 0 , Vasia.DrawC): I A r t i o n A c t o r = new MonsterC 10, Actor.DrawC); 10, "" ); "" ); // M o n s t e r // : // // :

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 1 ); "" );

, . , , , .

192

9.

: . . , : class Monster IAction { int IAction.Power { get { return a m * health; m o

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

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

// Monster Vasia = new MonsterC 50, 50, "" ); // Vasia.DrawO; ! , . , - . , , . , , Monster : , : interface { void DrawO; ITest1

}interface IAction1

, , .

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

1 94

9.

. 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 ); // F

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

;

IBase

void G ( ) : interface Iderived : I Left, I Right {} class A void Test( IDerived d ) { d.F( 1 ); ( ( I B a s e ) d ) . F ( 1 ): ( ( I L e f t ) d ) . F( 1 ); v ( I R i g h t ) d ) . F ( 1 ); // I Left F // IBase.F //' I Left. F /7 IBase.F

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

, .

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

9.

,

// IRight.F( int i ) -

}, , , : class : { // IRight.GO { . . . } !

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

} , , . new, . , : interface { void : IBase

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

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

}Derived d = new Derived ( ) ; d.AO; IBase id = d; id.AO; // Derived.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 ;

1 98

9.

}c l a s s Base : I void IBase.A() { ... } / / D e r i v e d IBase

c l a s s D e r i v e d : Base, p u b l i c v o i d AO {

IBase ... }

i i

, , , : 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-2F

:

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 { p u b l i c MonsterC i n t . h e a l t h , { this.health = health; this.ammo = ammo: this.name = name; i n t ammo, s t r i n g name ) : IComparable

}virtual { 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 } " , public void PassportO

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

.NET

211

{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 ) foreach ( Monster i in s.MonstersOnly() i.PassportO i.Passport ) i.PassportO

}, , . , get , IEnumerable IEnumerator . : y i e l d return , ; y i e l d break .1

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

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

212

9.

-\ . , ^ ^ ^ , :

^ ^\

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

, ( , 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 i s t r u c t Complex { p u b l i c double r e , im; ConsoleApplicati onl

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 , Complex b ) this.im

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,

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 { { Complex a = new Complex! 1.2345, 5 . 6 ); 1

Classl s t a t i c void MainO

i

.

214 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 = " + ); }

9.

} } : b = = = = (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 ) ; Console.WriteLine( "x = {0} Flags b = (Flags) 65; Console.WriteLine( "b = {0} ): = 10 = 11 = 11 D = 65 0 0 41 { 0 . 2 : X } " , b ); ( { 0 , 2 : } { 0 . 2 : X } " . ):

{ 0 . 2 : X } " , x );

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.10 () Console.WriteLineC

9.

" : {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 , # , . .

, . , , , \ . , . .

:

, 0 ) health = value; else health -= 0;

}}public int A m m o { get { return a m ; m o

}set { if ( value > 0 ) a m = value; m o else a m = 0; m o

}}public string N m a e {

277

pet

{} }

return inSTfe ,-

override public void PassportO { Console.WriteLineC "Monster {0} \t health = {1} a m = { 2 } " , m o name, health, a m ); m o

}string name; int health, a m ; m o

}public class Daemon : Monster { public Daemon() brain = 1 ; public DaemonC string name, int brain ) : baseC name ) this.brain = brain;

public D e o C int health, int a m , string name, int brain ) : amn m o baseC health, a m , name ) m o this.brain = brain;

override public void PassportO Console.WriteLineC "Daemon {0} \t health = {1} a m = {2} brain = { 3 } " . m o Name, Health, A m , brain ); m o

public void ThinkO Console.WriteC N m + " is" ); a e 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) Member Info ,

FindMembers 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 Memberlnfo Methodlnfo Module Parameterlnfo Propertylnfo , (, , , . .) , Eventlnfo, Fieldlnfo, Methodlnfo 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.3 ()

12. , , ,

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 A m m o 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 A m mo System.String Name , , . , . , .

(, , . .). . System. Attribute. . .NET , . , , , , . () , , . , . . , , . : [Serializable] class Monster

{

284[NonSerialized] string name; int health, a m ; m o

12. , , ,

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

1

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

8

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) , . . . , , , (. ) , , (, #1 i 200 "ku_ku.txt"). , 1 , , . f error (, ferror ). fwarning , . # 2.0. (#pragma warning disable) (#pragma warning restore )

#if,#elif, felse, fendif #line

terror, fwarning

#region, fendregion fpragma

:

#if [ #e1if ...] [ #elif

...][ #else

...]#endif # e l i f . , . , ==, ! =, !, && 11. . true, #def i ne.1

... , . . . .

289

12.4. 1 2 . 4 . // #define V R A 1 // #define V R A 2 using System; namespace ConsoleApplicationi { class Classl { #if VAR1 static void F ( ) { Console.WriteLine( " 1" ): } #elif V R A 2 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 V R A 1 fdefine V R A 2 using System; using System.Diagnostics: namespace ConsoleApplicationi { class Classl { [Conditional ("VAR1")] static void A ( ) { Console.WriteLineC " A" ); } [Conditional ("VAR2")] &

290 12,5 ()

12. , , ,

static void ( ) { Console.WriteLineC " " ); } static void MainO { : } BO;

}

} Main , , V R . A 1

, : , , , . , [20], [21], [26], [27], [30].

13 , -3 , , . N E T . , , # 2.0, - [generics), (partial types) (nullable types) .1

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

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

System.Collections

295

. , . , , , . . :

, . , . , , , . .

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

IDictionary IDictionaryEnumerator

296 13.2 () IEnumerable IEnumerator IHashCodeProvider I Li st

13. , -

IEnumerator foreach - ,

. 13.3 , System. Collections . 1 3 . 3 . System.Collections ArrayList BitArray Hashtable Queue , - 2 1

IList, ICollection, IEnumerable, ICloneabl I Col lection, IEnumerable, ICloneable IDictionary, ICollection, IEnumerable, ICloneable ICollection, ICloneable, IEnumerable IDictionary, ICollection, IEnumerable, ICloneable

SortedList , . Stack

ICollection, IEnumerable

System.Collections.Specialized , StringCollection - StringDictionary. ArrayLi st.

ArrayList , , , . ArrayList .1 2

13.2 13.3 [27] . , Hashtabl System.Object.GetHashCode.

System.Collections

297

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 IndexOf Insert InsertRange LastIndexOf Remove RemoveAt RemoveRange Reverse SetRange Sort TrimToSize ( , ) t ArrayLi st ( -1, ) ( ) , 1

(. . 205).

298

13. , -

ArrayLi st Array, . # object, . , , object, -. , , . , , ArrayLi st. , , , . ArrayLi st: arrl.AddC 123 ) ; arrl.Add( - 2 ) : arrl.AddC "" ); , , : int int = (int) b = (int) arrl[0] 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 a m = 100 m o health = 100 a m = 100 brain = 3 m o

, -, . , . # , 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 Dictionary LinkedList List Queue SortedDi cti ona ry Stack Comparer HashTable

ArrayList Queue SortedList Stack

, . N E T 1.0 1.1, , , object: , , , ; , . - , . . ArrayLi st List Monster Daemon, 5 8, . 1 3 . 2 . List using System; using System.Collections.Generic; using System.Text: (. . 275)1

-

301

namespace ConsoleAppl icationi { using MonsterLib; class Program { static void MainO { List stado = new List(); stado.Add( new MonsterC "Monia" ) ); stado.Add( new MonsterC "Monk" ) ); stado.AddC new Daemon ( "Dimon", 3 ) ); foreach ( Monster x in stado ) x.PassportO; List lint = new List(); 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 M n ok Daemon Dimon 5 1 3 5 health = 100 a m = 100 m o health = 100 a m = 100 m o health = 100 a m = 100 brain = 3 m o

1