Шилдт Г. Полный справочник по С#

744

Upload: sanechka86

Post on 27-Jul-2015

785 views

Category:

Documents


5 download

TRANSCRIPT

#

"" - 2004

32.973.26-018.2.75 57 681.3.07 "" . .. .. "" : [email protected], http://www.williamspublishing.com

, . 57 #. : . . . : "", 2004. 752 . : . . . .

ISBN 5-8459-0563- (.) # , .NET, : , , , , , , , , . #. C++, Java # , , . , #. 32.973.26-018.2.75 . , , , Osborne Publishing. Authorized translation from the English language edition published by McGraw-Hill Companies, Copyright 2003 All rights reserved. No part of this book may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording or by any information storage retrieval system, without permission from the Publisher. Russian language edition published by Williams Publishing House according to the Agreement with R&I Enterprises International, Copyright 2004

ISBN 5-8459-0563- (.) ISBN 0-07-213485-2 (.)

"", 2004 by The McGraw-Hill Companies, 2003

I. # 1. # 2. # 3. , 4. 5. 6. , 7. 8. 9. 10. 11. 12. , 13. 14. - 15. 16. , 17. , 18. , , 18 21 22 30 53 80 102 126 154 179 224 256 277 319 349 375 409 431 449 484

II. # 19. System 20. 21. 22. 23. Internet III. # 24. 25. Windows- 26.

501502 541 575 610 645 669 670 689 707

IV. . XML . # "

731732 737 740

1718

I. # 1. #

2122

# , C++ Internet Java # # .NET Framework .NET Framework CLR 2. # - csc.exe Visual Studio IDE " ", " " if for # # 3. ,

23 23 24 25 26 27 27 28 28 29 30 31 32 32 33 33 34 34 34 35 35 38 40 41 42 44 45 45 47 48 50 50 51 51 5253

# decimal

54 54 55 57 58 60

bool 4.

61 62 65 65 66 66 68 68 69 70 72 73 74 76 7880

, , ? 5.

81 82 84 87 89 89 90 90 96 99 99 101 101102

if if- if-else-if switch switch for for for while do-while foreach break continue return goto 6. ,

103 104 105 106 110 110 112 112 114 115 116 116 117 117 119 120 120 122 123 123 126 127 7

Building Building Building new new "" this 7. " " Length Length \ foreach . switch- t 8. * # public private : ref- out- ref out ref out

127 128 132 133 134 134 137 138 140 142 144 144 146 146 147 148 149 149 151 154 155 155 157 158 159 159 160 161 162 164 165 167 168 172 172 173 175 177 178 179 180 180 182 182 187 189 191 191 193 195 197 199

8

this Main() Main() Main() static 9.

202 203 208 212 213 213 213 215 218 223224

#- true false " , 10.

225 226 228 232 236 237 240 ~240 242 246 250 251256

11.

257 257 260 263 264 266 271 271277

base sealed object object

278 281 283 285 286 290 291 293 296 297 301 305 305 309 313 313 315 317

'

9

12. ,

319

.NET Framework : 13. System. Exception try- catch- catch- try- finally 4 " ' checked unchecked 14. - #- - Stream - FileStream - 10

320 321 325 327 328 330 331 331 332 333 334 334 335 340 343 345 347 347 347 349 350 350 351 351 353 354 356 357 358 358 360 360 362 363 365 367 370 372 375 376 376 376 377 377 378 378 380 380 380 382 383

FileStream FileStream - StreamWriter StreamReader BinaryWriter BinaryReader - MemoryStream StringReader StringWriter 15.

383 385 386 387 389 389 391 392 394 394 395 396 400 402 404 405409

System.Delegate .NET Framework EventHandler : 16. ,

,

410 413 415 416 416 418 419 421 425 426 428 429431

using using #define #if #endif #else #elif #undef #error #warning #line #region #endregion internal internal 17. ,

432 432 434 436 437 438 440 441 441 442 442 444 445 446 446 446 447 447 447449

450 11

is as typeof : System.Type GetMethods() AttributeUsage Conditional Obsolete 18. ,

450 451 453 454 454 455 455 458 459 462 466 471 474 474 474 475 475 477 480 480 481 482484

"*" "&" unsafe fixed sizeof lock readonly stackalloc using const volatile II. # 19. System

485 486 486 487 487 488 489 489 491 492 492 494 494 495 496 496 496 496 497 498 499 501 503 504 509 510 511 502

System Math 12

Decimal Char Boolean Array BitConverter Random GC Object IComparable IConvertible ICloneable IFormatProvider IFormattable 20.

514 518 523 523 524 526 527 532 534 536 537 537 538 538 540541

# String String , String String String , "" Substring() String. Format() ToString() String. Format() ToString() - 21.

542 542 543 543 544 544 544 547 549 552 555 556 557 558 558 559 560 561 562 564 565 565 569 571 573575

Thread , IsBackground

576 577 577 580 581 583 585 586 588 13

Monitor lock Wait(), Pulse() PulseAll() Wait() PulseQ MethodlmplAttribute , Abort() Abort() 22.

592 593 594 594 595 598 599 601 603 604 606 606 608 608610

ICollection IList I Dictionary IEnumerable, I Enumerator I Dictionary Enumerator IComparer IHashCodeProvider Dictionary Entry Array List Array List- Hashtable SortedList Stack Queue BitArray I Dictionary Enumerator , IComparable IComparer 23. Internet

611 612 612 613 614 615 615 616 616 616 617 621 622 623 625 629 631 633 636 636 637 638 639 641 642 644645

System.Net Internet- WebRequest WebResponse HttpWebRequest HttpWebResponse , Create() , GetReponse() 14

646 647 647 648 650 650 650 653 653 654

, GetResponseStream() UR1 HTTP- cookie- LastModified : MiniCrawler WebClient

654 654 656 657 658 659 660 661 665

III. # 24.

"

669670

#- #- - IComponent Component CipherLib , CipherComp Dispose() Dispose(bool) using 25. Windows-

671 671 672 672 672 673 673 674 675 676 677 678 683 684 685 686 688689

Windows- Windows-, Windows Windows- Form Windows-, Windows- (IDE) 26.

690 691 691 692 692 692 694 694 695 695 696 696 696 697 699 700 702 706707

:

708 709

15

IV. . XML XML XML- XML- . #

710 711 713 719 720 728 728 731 732 733 734 734 737 740

16

(Herbert Schildt) , , C++, Java #. 3 . . , , C++, C++: A Beginner's Guide, C++from the Ground Up, Java 2: A Beginner's Guide Windows 2000 Programming from the Ground Up. ( . ). : (217) 586-4683.

, - : , . , , , . , - . . , . , (, , ) . , : . #. Microsoft .NET Framework, # . (Anders Hejlsberg). # - : C++. , . , C++. , # : Java. , , # Java " ". , , . , , # , . , # , (), , . , ; , COM (Component Object Model Microsoft , , ). . Java # , . , Java, C# . , # C++ Java, (type checking) (template class). , # , ( ). , . # . , # . , .

# . # , # . , . I, #. II, #. III, #. I #. , , , , - , . II #. .NET Framework. . .NET Framework , II , System. #. , , . , #. III #. 24 , 25 Windows Windows Forms. 26 (recursive descent parser).

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

, Visual Studio .Net 7 ( ), .NET Framework.

- Web- , , Web- : www.osborne.com. 19

# "" , . , . # #: A Beginner's Guide. , C++, : C++: Beginner's Guide C++ Teach Yourself C++ C++from the Ground Up STL Programming from the Ground Up The C/C++ Programming Annotated Archives , Java, : Java 2: Beginner's Guide Java Java 2: Programmer's Reference , , Teach Yourself

ILJ , , . , , . , . , Web- . , , , , . , , . . :E-mail WWW [email protected] http://www.williamspublishing.com

: 115419, , / 783 1 03150, , / 152 20

# I #. , . , # (, - ), #.

#

# . , . , , # . # ( Microsoft .NET Framework) . # , Windows, Internet, . # "" . # , , . # .NET Framework.

# . , . " " , , . . #. # "" . ( C++) Java. #. # .

, . (Dennis Ritchie) 1970- PDP11 DEC (Digital Equipment Corporation), UNIX. , Pascal, , . 1960- . , "-" (spaghetti code) goto. . , . .

1. #

23

, , , , . , ( ), . , , , , , . 1980- . . . , . " " , , , 5 000.

C++ 1970- , " ". , . - (). , , . , , , . C++. C++ (Bjarne Stroustrup) Bell Laboratories ( , -), 1979-. " ", 1983 C++. C++ . , , C++. , , - . , C++ . "" C++ , "" . , - , . 1980- C++ 1990- . , . C++ - . , C++ . . ( ) , .

24

I. #

Internet Java Java, Oak ( . ""). 1991 Sun Microsystems. Java (James Gosling). (Patrick Naughton), (Chris Warth), (Ed Frank) (Mike Sheridan). Java - , "" C++. Java ( ), . Internet , . , , , , . Internet, , . , Java. , , Java ( ) ( ) , Java Internet, a , . 1993 , , , Internet. Internet , "" . , , .. Internet. Java , - (bytecode), .. , Java-. - Java (Java Virtual Machine JVM) - . , Java- , JVM. JVM , . Java- - - ++, . . , /++- , , . , /++-, , () . . , Java- . #.

1. #

*

25

, Java C++. , C++. Java- C++ , , , C/C++- Java. , Java ( ), . " " C++, Java . , Java , C++ "" "" .

# Java , Internet, . (cross-language interoperability) , (mixed-language programming). , , . (), , , . , Java Windows. Java- Windows ( Java), Java Windows . Windows , Windows Java. , Microsoft #. # 1990- .NET- Microsoft. - 2000 . # (Anders Hejlsberg) , . , 1980- Turbo Pascal, . # , C++ Java. . . , C++, Java. # , , "" "" . "", . # . 1.1. "" # . # , . , # , C++. C++, # . # Java . , Java C++. . Java C# . # Java. # Java , , 26 I. #

"". Java, #. , Java, , #, Java.

C++

Java

#

. 1.1. # # , . . # - . , # , : , . - .

# .NET Framework # , .NET Framework. . -, # Microsoft , .NET Framework. -, , #. # .NET Framework, , .NET Framework , #.

.NET Framework .NET Framework , . " " , Windows. , .NET Framework no Windows, .. , , , Windows. .NET Framework # . , Common Language Runtime (CLR), , . CLR .NET Framework, , . 1. #

27

, .NET-, . , , , - , .NET- . , . , : , . , .NET- , (.. ), .NET. # .NET- , # .NET-.

- J CLR CLR .NET-. . , , , Microsoft (Microsoft Intermediate Language MSIL). MSIL , . , MSIL . MSIL - Java, . CLR- . , , MSIL-, , CLR-. .NET Framework . , Microsoft, -. "" . "yust-/n-rime", ( ). . .NET- CLR- -, MSIL- "" , . , "" , , MSIL-. , , "" , "" MSIL-. MSIL- (metadata). , , . , MSIL-.

I , (managed code). CLR. , . , 28 I. #

, , MSIL-, CLR-, , -, .NET Framework. , , , . ? Windows- .NET Framework , CLR-. , #- .

, CLR-, , , (Common Language Specification CLS). , . CLS- , , . CLS- (Common Type System CTS). CTS- . , # CLS-, CTS-.

1. #

29

#

, , , . , , "". # . . #, , . , . I.

- # - (). - #, # - . , , . "", . , , . , ( ) . , . , , . (, FORTRAN COBOL), . , . , . , , , - , . " " - . , , . , - . - . . , : (.. , , ), (.. , ). . , . 2. # 31

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

, () , , . - , . . . , , . , . . , , . , ( , ) . , . # . . , , . # . . , , , . , , . , , (instance variable), , , - (member method), . "" , # . C++, , , , , #- , /++- . # C++, "" , #-.

( polymorphism, " ") , . . () , . , : , . , . , , , , , , . 32 , I. #

. , , (stack), .. , " ". , , . , , . , . "" , . # "" , . , , , . : " ". , . , . ( ) () . . .

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

. /* . Example.cs. */

2. #

33

using System; class Example { // Main() public static void Main() { Console.WriteLine(" .");

# Visual Studio .NET. , , : c s c . e x e (Integrated Development Environment IDE). . ( , , .)

esc. , , Visual Studio, # , . #- . 1. , . 2. . 3. .

, , Web- Osborne : www.osborne.com. . - , Notepad. , , , , #-. , Example. cs.

, #-, c s c . e x e , . C:\>csc Example.cs esc Example.exe, MSIL- . MSIL- , -. Example.exe Common Language Runtime -. : Example. ( -, MSIL-) , .NET Framework, CLR. 34 I. #

I

csc.exe , , vcvars32 .bat, //Program Files/Microsoft visual studio .NET/Vc7/Bin. . # Microsoft Visual Studio .NET&Visual Studio .NET Command Prompt ^Visual Studio .NET Tools ^, .

. I :\>Example : I .

Visual Studio IDE 7 Visual Studio .NET, Visual Studio IDE . , Visual Studio ( 7), . ( Visual Studio, , .) 1. () #-, File^New^Project ( => ). = 2. ( Project Types ( )) Visual C# Projects ( Visual C#), ( . 2.1) Templates () Empty Project ( ). New Project Project Types: wJ Visual B s Projects ai c il* Visual C# Projects ' (LJ Visual C++ Projects ; d S t p and D p y e t Projects eu el mn o E&J Oh r Projects te' Visual Studio Solutions Templates:

ASP.NET Web Ap...

ASP.NET Web Control;: WebS... Library

I P Console Application

Windows Service

;An e py projectforcreating a local application mt jProjectl J D\ okNn \ i u l Studio Projects :W r \ i aVs a location: Project will be created at D\ ok Si aV u l Studio Projects\projectl. :W r V I \ i a n s O KPuc. 2.1. New Project ( )

Browse.. Hl ep

Cne acl

2. #

35

3. , S o l u t i o n Explorer ( ) , , Add^Add New Item (^ ) , 2 2 4. Add New Item ( ) Categories () Local Project Items ( ), Templates Code File ( ) , 2 3

I] > S uo Projectl (1 poe t otn li r j c) u l i + a Bd R bd eu li crH < Nw m A& et le A$j f\ Ad E i tn Ie d xsi g t m A d R f r ne d e ec e tJ N F d r e oe w l Ad Wb R f r ne d e e ec e Ad Wd w Fr d no s o i m S t a Sat p Poe t e s t ru r j c .3 A d I h re Fr d n ei d o t m Dbg eu >i Save Projectl Ad Ue C nr l d sr o to 3 Ad I h rt d C nr l d n eie o fo X Remove ij Ad Cm oet d o p nn Rename Ad Cs d as l Properties 2 2 Add&Add New Item

User Control Data Form Wizard

Data Set

XML File

X L M Schema

Name

IcodeFilelcs Open Cancel Help

2 3 Add New Item 36

I. #

5. Example.cs. (, , , Web Osborne : www.osborne.com.) , . 2.4. f E a pec * xmt.s

1 I F #-. Example.cs.

using System; *j class Example "

iL. 2.4. Example, cs

6. Build^Build Solution (1^ ). 7. Debug^Start Without Debugging ( ). , . 2.5.!^D:\Work\Nta\V^ -. Press any key to continue,

-

;*!

. 2.5. Add New Item

\\\'d

, , . #-. . .

2. #

37

, ( ) , , , , .

" ", " 9' , Example.cs , . , . (, Java), , #- . Example, cs, , , Sample, cs, T e s t . c s X.cs. .cs, . , . ( , , ) Example.cs. , . , . . /* #-. Example.cs. */ . # , . , , , . Example.cs. , . # . , , . /* , (*/). , , . , , . . I u s i n g System; , System. # (namespace) . , , . , , , , . System, , .NET Framework, #. 38 I. #

using , . . 1 c l a s s Example { c l a s s , . , # . Example . ({) (}) . , , , . , , # . , -. . I // Main(). , #. / / . , , () . . I p u b l i c s t a t i c void Main() { Main(). , # (method). , . #- Main (). ( : /++- main().) , #. , . p u b l i c (access specifier). , . p u b l i c , , , , . ( p u b l i c p r i v a t e , , () .) Main () public-, ( ) ( ).1

# , Main () public-. , Visual Studio .NET. -. Main () public. , Main ().

s t a t i c Main() . , 2. # 39

Main () . void , Main () . , . Main () , . , Main () ( ) , . ({), Main(). , . . , Main (). I Console.WriteLine(" #-."); " #-. " . WriteLine (). , . . , WriteLine () . Console , - . Console WriteLine (), , WriteLine () Console. , # , - . , , WriteLine (), , u s i n g System. # . , , , . (}) Main (), Example. . # . . , Main main WriteLine w r i t e l i n e , . , # , Main (), . , Main (main), . , , Example.exe .

, ( ). . , (). #- - , "". , "" , . , 40 I. #

( ) , IDE "" , 2 6 ( esc)

}

Description , expected

__

_

i

File

_

_ _

_ trie 1 3 1 7

D \ okN aVs a \Projectl\fx m t cs W r\ m \ iu l a pe D \Work\Nna\Visual \Projectl\|Example cs

T p or n m s a e definition or end of file expected y e a ep c i 2 6

, , , , , " " , , , "" , , "" " "

I u s i n g System; , , , # , , I Console.WriteLine(" #-.");

I System.Console.WriteLine(" #-."); , // u s i n g System. class Example { // #- ( ) . public static void Main() { // Console.WriteLine . System. Console. WriteLine ( " . ") ;

2. #

41

System , , #- u s i n g System ( , ). , , , .

, . , . . , , . . // . u s i n g System; c l a s s Example2 { p u b l i c s t a t i c void Main() { i n t x; // . i n t ; // . = 100; // 100. Console.WriteLine(" " + ) ;

' - / 2; Console.Write(" / 2: " ) ; Console.WriteLine(); : 100 / 2: 50

|

? , I i n t x; // . . # . , . . , .. . # i n t . , i n t x; i n t . . I i n t ; // . , , . . , , : 42 (. #

_; , _ . i n t , C# . 100. I = 100; // 100. # (=). , , , . , " ". 1 Console.WriteLine(" " + ) ; "" , . , " + " , WriteLine () . , 2. | - / 2; 2, . , 50. . , # , : + * / : Console.Write(" / 2: " ) ; Console.WriteLine(); . -, " / 2: " WriteLine (), Write (). . , . , Write () WriteLine (), , . -, , WriteLine () , .. . , WriteLine (), Write () #-. , . . , : I i n t , ; // .

I

2. #

43

i n t . i n t . , . , int- 18, 18,3. , i n t , #. # , f l o a t double, , . ( double " ".) double , : I double r e s u l t ; r e s u l t double. r e s u l t double, , 122,23, 0,034 19,0. i n t double, : /* int double. */ using System; class 1 { public static void Main() { int ivar; // int. double dvar; // double. ivar = 1 0 0 ; // ivar // 100.

dvar 100.0; // dvar = // 100.0. Console.WriteLine( " ivar: " + ivar); Console.WriteLine( " dvar: " + dvar); Console.WriteLine(); // . // 3. ivar = ivar / 3; dvar = dvar / 3.0; Console.WriteLine("ivar : " + ivar); Console.WriteLine("dvar : " + dvar);

: I i v a r : 100 I dvar: 100 44 I. #

I

lvar : 33 dvar : 33.3333333333333 , i v a r 3 , 33, .. . dvar 3 . , , . , . , # 100 , 100.0 . , WriteLine . , .. . , ^ , 3,1416. // . using System; class Circle { static void Main() { double radius; double area; radius = 10.0; area = radius * radius * 3.1416; Console.WriteLine(" " + area);

: I 314.16 , .

, , . , #. , , , .

i f i f . i f # . , C++ Java, . :

i f () ; 2. # 45

( ). , . ( ) . : I i f (10 < 11) Console.WriteLine("10 1 1 " ) ; 10 11, .. , WriteLine () . : I i f ( 1 0 < 9) Console.WriteLine(" . " ) ; 10 9, WriteLine () . # , . : < >5 == != if. // if. using System; class IfDemo { public static void Main() { int a, b, c; a = 2; b = 3; if(a < b) Console.WriteLine(" b " ) ; // , if(a == b) Console.WriteLine( " ."); Console.WriteLine(); = - b; // -1. Console.WriteLine(" - 1 " ) ; if( >= 0) Console.WriteLine( " "); if( < 0) Console.WriteLine(" "); Console.WriteLine(); = b - ; // 1. Console.WriteLine(" 1"); if( >= 0) Console.WriteLine( " "); if( < 0) Console.WriteLine(" ");

46

I. #

: b -1 1 . 1 i n t a, b, ; , . , ^ , .

f o r , . # . for. , C++ Java, , for # , . for . for{; ; ) ; . , . , for , . , , . , for. // using System; c l a s s ForDemo { public s t a t i c void Main() { i n t count; for(count = 0; count < 5; count = count+1) Console.WriteLine(" : " + count); Console.WriteLine("!") ; for.

: : 0 : 1 : 2 : 3 : 4 !

2. #

47

count. for . ( ) count < 5. , WriteLine (), . , , , . , #-, , , . , , : I count = count + 1; , # , . "" (++) :I COUnt++;

, for : I f o r ( c o u n t = 0; count < 5; count++) , , , . # (). , .

U , , # . . . , , . , _ if- for-. if-:i f ( w < h) { v = w * h; w = 0;}

w h, , w < h, . , , . , , , . . , . I // .

48

I. #

using System; class BlockDemo { public static void Main() { int i, j, d; i = 5; j = 10; // if- // , , if(i != 0) { Console.WriteLine("i "); d = j / i; Console.WriteLine("j / i " + d);

I

: i j / i 2

i f- , . if- ( ), , . . | i = 5; i = 0; . . 1 10 . // 1 10. using System; class ProdSum { static void Main() { int prod; int sum; int i; sum = 0; prod = 1; for(i=l; i ".

3. ,

61

WriteLineO , #. # , . - , . , , . , , #. "". : Console.WriteLine( " " + 2 + " $" + 3 + " .");

I

, "" . , , 13. : Console.WriteLine( " 10/3 : " + 1 0 . 0 / 3 . 0 ) ; : | 10/3 : 3.33333333333333 , , . , . WriteLine (), . WriteLine (" ", argO, argl, . . . , argN); WriteLineO , " + " . _ : "" "". , " ", . : {_ , : ) _ ( ). , . WriteLineO , ( ) , _. , _ , , . . , {0} , {1} argl .. .

62

I. #

I Console.WriteLine("

{0} {1} . " , 28, 2 9 ) ;

: I 28 2 9 . , {0} 28, {1} 29. , ( 28 29). , " + " , . "" , . Console.WriteLine( " {0,10} {1,5} . " , 28, 2 9 ) ;

^

: I 28 2 9 . , , . , . , . , , , . , > . // . using System; class DisplayOptions { public static void Main() { int i ; Console. WriteLine ( "^^") ; f o r ( i = 1; i < 10; Console.WriteLine("{0}\t{1}\t{2}" /

: 1 1 1 2 4 8 3 9 27 64 4 16 5 25 125 36 216 7 49 343 64 512 8 9 81 729 . , . . , WriteLine (). *'#", . 3. , 63

, . 10 3. .Console.WriteLine( " 10/3 : {0:#.##}", 10.0/3.0);

-: I 10/3 : 3.33 # . ##, WriteLine () . , , , . . Console.WriteLine("{0:###,###.##}", 123456.56);

: | 123,456.56 , . : decimal balance; balance = 12323.09m; Console.WriteLine(" {0:C}, balance); : I $12,323.09 , . /* . */ using System; class UseDecimal { public static void Main() { decimal price; decimal discount; decimal discounted_price; // . price = 19.95m; discount = 0.15m; // 15%. discounted_price = price - ( price * discount); Console.WriteLine(" : {0:}", discounted_price);

, , : I : $16.96 64 I. #

# , . , 100 . . , . . #- . . , . , ' \ f % ' . . , 10 -100 . , . 11.123. # ( ). # , . : ? , , 12, 123987 0.23? , # , . -, , , , i n t . , , , i n t , u i n t , long ulong. -, double. , #, , . long, 1 L. , 12 i n t , 12L long. , U. , 100 i n t , 100U u i n t . u l UL (, 987 654UL ulong). f l o a t , f F (, 10.19F). decimal, m (, 9. 95). i n t - , uint-, long- ulong- , byte, sbyte, s h o r t u s h o r t , , , .

, , 16, . 0 9 F, "" 10, 11, 12, 13, 14 15. , 10 16. #, , 3. , 65

I

. ( ""). . count = OxFF; // 255 i n c r = Oxla; // 26

, , (, ), . , , # , . # (ESC-), . 3.3. , . , ch :ch ='\t';

ch :ch = \';

3.3. ESC- \ \ \f \ \ \t \v \0 \f \" \\ () ( ) - ()

# : . , . , I " " . ( WriteLine ()) . , . , , . ESC-, \n, \ t \ " . II/ ESC- , u s i n g System; 66 I. #

class StrDemo { public static void Main() { Console.WriteLine( " \ \ "); Console.WriteLine ("OflMH\tflBa\tTpii") ; Console.WriteLine (llepe\t\tec") ; // . Console.WriteLine("\"3?\", .");

: "?", . , \ , WriteLine () . , , ESC \. , ( ESC- \ " ) . (verbatim) . @ , . , . , , ., . ("). , (""). , . // . u s i n g System; c l a s s Verbatim { p u b l i c s t a t i c void Main() { Console.WriteLine(@" , . 1 2 5 6 Console.WriteLine(@" : 3 4 7 8 Console.WriteLine( @" : "" # . " " " ) ;

:

3. ,

67

, . : 1 2 3 4 5 6 7 8 : " #." , , . , . . , . . . (, X') char. , (, "X"), - .

2, : _; , _ . . . , . , bool . , . , m t char. # . , , , , "" . "" # . . , , , , .

. . . . : _ ;

68

I. #

I

, , , . . : i n t count = 1 0 ; // count // 10. char ch = 'X 1 ; // ch X. f l o a t f = 1.2F // f // 1.2. ( ) . , i n t a, b = 8, c = 1 9 , d; // b // .

, # , , . , , , , . // . using System; c l a s s Dynlnit { p u b l i c s t a t i c void Main() { double s i = 4 . 0 , s 2 = 5 . 0 ;

// . ' );

// hypot. double hypot = Math.Sqrt( (si * s i ) + (s2 * s2)

Console.Write(" " + s i + " " + s2 + " " ) ; Console.WriteLine("{0:#.###}.", hypot);

: I 4 5 6.4 03. : s i , s2 hypot. ( s i s2) , , hypot, . , M a t h . S q r t ( ) . , , . Math. Sqrt () ( ) , hypot. , , , , . 3. , 69

, , Main(). # . , . , (scope) . , , , . . # , . ( , ) , , , . , , . , . , , , (.. ) , . , / . . . , . . , , , . : , , . , : // . using System; class ScopeDemo { public static void Main() { int x; // // Main(). = 10; if( 10) { // , int = 20; // // . // . Console.WriteLine(" : " + + " " + ) ; = * 2; } // = 100; // ! . // . Console.WriteLine(" " + ) ;

70

I. #

, Main () . i f . , . , , I // = 100; // ! . . , , if-. if-, (.. ) , . , . , , . , , , . , . ^ . , , , . , , . , . , , , , , : // .

using System; class VarlnitDemo { public s t a t i c void Main() { / int x; for(x = 0; x < 3; x++) { int = -1; // // . Console.WriteLine(" : " + ) ; // // -1. = 100; Console.WriteLine(" : " + ) ;

: ; - 1 : 100 : - 1 : 100 : -1 : 100

3. ,

71

, for 1. 100, . : , , , , , . , - . /* , , . *** */ using System; class NestVar { public static void Main() { int count; for(count = 0; count < 10; count = count+1) { Console.WriteLine("This is count: " + count); int count; // !!! for(count = 0; count < 2; count++) Console.WriteLine(" !"); . ***

C/C++, , , , . , C/C++ count for . . #, , , .

. , , f l o a t i n t . int i; float f; i = 10; f = i; // float- int-. , ( ) "" . , 72/

I. #

, int- i , f l o a t , f. , # , . , bool i n t . - . .

, ; (.. ), . , . , i n t "" , byte, i n t , b y t e , . , , . , , long double , . // // long double. using System; class LtoD { public static void Main() { long r e double D; L = 100123285L; D = L; Console.WriteLine("L D: " + L + " " + D ) ;

long double, ( double long) , . , : // *** . *** using System; class LtoD { public static void Main() { long L; double D; D = 100123285.0; L = D; // !!! . , 73

Console.WriteLine("L D: " + L + " " + D ) ;

decimal f l o a t ( double), char ( bool). , char bool .

, . . . : (_) _ . , , / i n t , :double , ; // . . . (int) ( / ) ;

I

, , double, i n t . , / , . ( ) i n t , . , double i n t . , . , long i n t , long , i n t , "" long. . , 1,23 1. (0,23) . , , . // .using System; class CastDemo { public s t a t i c void Main() { double x, y; byte b; int i ; char ch; uint u;

74

I. #

short s; long 1; x = 10.0; = 3.0; // double int. i = (int) (x / y ) ; // . Console.WriteLine( " / : " + i ) ; Console.WriteLine(); // int byte . i = 255; b = (byte) i; Console.WriteLine("b 255: " + b + 11 11 . ); // int byte i = 257; b = (byte) i; Console.WriteLine("b 257: " + b + 11 ."); Console.WriteLine(); // uint short , = 32000; s = (short) u; Console.WriteLine("s 32000: " + s + " ."); // uint short . = 64000; s = (short) u; Console.WriteLine("s 64 000: " + s + " -- ."); Console.WriteLine(); // long uint . 1 = 64000; u = (uint) 1; Console.WriteLine("u 64000: " + + " -- ."); // long uint . 1 - -12; u = (uint) 1; Console.WriteLine("u -12: " + + " -- ."); Console.WriteLine(); // byte char. b = 88; // ASCII- X. ch = (char) b; Console.WriteLine("ch 88: " + ch);

. ,

75

: / : 3 b 255: 255 , b 257: 1 -- . s 32000: 32000 s 64000: -1536 , .

64000: 64000 -- , -12: 4294967284 -- . ch 88: X . ( / ) i n t , .. . , b 255, b y t e 255. b 257 , 257 byte. , i n t b y t e . s s h o r t 32 000 ( u i n t ) , short- 32 000. , 64 000 s h o r t , . , u i n t s h o r t . ( u i n t ) 64 000 ( 1 long). , 64 000 u int-. 12, , , u i n t . , long u i n t . , byte- char " ", .. , .

I , . , . , s h o r t long, . , . "" . .

76

I. #

decimal, TO " ", .. " " decimal ( f l o a t double, ). double, TO double. f l o a t , TO float. ulong, TO ulong ( sbyte, s h o r t , i n t long, ). long, TO long. u i n t , sbyte, s h o r t i n t , long. u i n t , TO uint. i n t . "" . - . , f l o a t double decimal. ulong . - , . -, . , i n t , . , char-, sbyte-, byte-, ushort- short- i n t . "" int- (integer promotion). , "" i n t . , , . , byte "" i n t , - byte. . . , , byte-. . byte- "" i n t , , i n t . , byte- byte- int-. VL . : // ! using System; class PromDemo { public static void Main() { byte b; . , 77

b = 10; b = (byte) (b * b ) ; // !I Console.WriteLine("b: "+ b ) ;

I

, b * b . , * b b "" i n t , .. b * b int-, byte . , , , , , . char. , "" - char- i n t . c h a r c h l = f a ' , ch2 = ' b 1 ; chl

= (char) ( c h l + c h 2 ) ; c h l 2 i n t , int- char-. (, ). , i n t (.. sbyte-, byte-, s h o r t - ushort-), "" i n t . To char. , uint- long.

, . . , , . 1 10. . , Math. Sqrt () i n t . // . using System;class CastExpr {

public s t a t i c void Main() { double n; for(n = 1.0; n (ThreeD opl, ThreeD op2) { if((opl.x > op2.x) && (opl. > op2.y) && (opl.z > op2.z)) return true; else return false; } // X, Y, Z. public void show()

236

I. #

Console.WriteLine(x + ", " + + ", " + z ) ;

class ThreeDDemo { public static void Main() { ThreeD a = new ThreeD(5, 6, 7 ) ; ThreeD b = new ThreeD(10, 10, 10); ThreeD = new ThreeD(1, 2, 3 ) ; Console.Write(" : " ) ; a.show(); Console.Write(" b: " ) ; b.show(); Console.Write(" : " ) ; .show(); Console.WriteLine() ; if(a if(a if(a if(a > < > < c) c) b) b) Console.WriteLine("a > Console.WriteLine("a < Console.WriteLine("a > Console.WriteLine("a < b b - ") - ") - ") - ")

: : 5, 6, 7 : 10, 10, 10 : 1, 2, 3 > - < b - : . , "", . :

"==" " ! = " , Object.Equals () Object.GetHashCode (). ( ) 11.

t r u e f a l s e t r u e f a l s e . . t r u e f a l s e , if, while, for do-while, ?-. (, ).

9.

237

t r u e f a l s e . . : p u b l i c s t a t i c bool o p e r a t o r true{_ op){

I/ true f a l s e .}

public s t a t i c bool operator false{_{

op)

//}

true

false.

, bool. t r u e f a l s e ThreeD. , ThreeD- , . , . . // t r u e f a l s e ThreeD. using System; // , class ThreeD { int x, , z; // 3-- . public ThreeD() { = = z = 0; } public ThreeD(int i , i n t j , i n t k) { x = i;

= j ; z = k; }

// true. public static bool operator true(ThreeD op) { if((op.x != 0) M (op. != 0) || (op.z != 0)) return true; // 0. else return false; } // false. public static bool operator false(ThreeD op) { if((op.x == 0) && (op. == 0) && (op.z == 0)) return true; // , else return false; // " . public static ThreeD operator (ThreeD op) op.x; op.y; op.z; return op; // X, Y, Z.

238

I. #

public void show() { Console.WriteLine(x + ", " + + ", " + z) ;

class TrueFalseDemo { public static void Main() { ThreeD a = new ThreeD(5, 6, 7 ) ; ThreeD b = new ThreeD(10, 10, 10); ThreeD = new ThreeD(0, 0, 0 ) ; Console.Write(" : " ) ; a. show() ; Console.Write(" b: " ) ; b.show(); Console.Write(" : " ) ; .show(); Console.WriteLine() ; if(a) Console.WriteLine("a - ."); else Console.WriteLine("a - ."); if(b) Console.WriteLine("b - ."); else Console.WriteLine("b - ."); if() Console.WriteLine(" - ."); else Console.WriteLine(" - ."); Console.WriteLine(); Console.WriteLine( " , ThreeD.") do { b.show(); b; } while(b);

: : 5, 6, 1 : 10, 10, 10 : 0, 0, 0 - , b - , - . , ThreeD. 10, 10, 10 9, 9, 9 8, 8, 8 7, 7, 7 6, , 5, 5, 5 4, 4, 4 3, 3, 3 2, 2, 2

9.

239

, ThreeD if- while-. if-, ThreeD- t r u e . . do-while- . , b (.. ). b , () ( t r u e ) , .

, # : &, | , !, & & | |. , &, | , !. & & | | , .

. , , & | , bool. ! , , bool. &, |, ! ThreeD. , , ThreeD- , . , . // !, | & // ThreeD. using System; // , class ThreeD { int x, , z; // 3-- . public ThreeD() { = = z = 0; } public ThreeD(int i, int j, int k) { x = i; = j; z = k; } // "|". public static bool operator |(ThreeD opl, ThreeD op2) { if( ((opl.x != 0) || (opl. != 0) || (opl.z != 0)) | ((op2.x != 0) || (op2.y != 0) || (op2.z != 0)) ) return true; else return false; } // "&". public static bool operator &(ThreeD opl, ThreeD op2)

240

I. #

if( ((opl.x != 0) && (opl.y != 0) && (opl.z != 0)) & ((op2.x != 0) && (op2.y != 0) && (op2.z != 0)) ) return true; else return false; // "!". public static bool operator !(ThreeD op) { if((op.x != 0) || (op. != 0) || (op.z != 0)) return false; else return true; // X, Y, Z. public void show() { Console.WriteLine(x + ", " + + ", " + z ) ;

class TrueFalseDemo { public static void Main() { ThreeD a = new ThreeD(5, 6, 7 ) ; ThreeD b = new ThreeD(10, 10, 10); ThreeD = new ThreeD(0, 0, 0 ) ; Console.Write(" : " ) ; a.show(); Console.Write(" b: " ) ; b.show(); Console.Write(" : " ) ; .show(); Console.WriteLine(); if(!a) Console.WriteLine("a - ."); if(!b) Console.WriteLine("b - ."); if(!c) Console.WriteLine(" - ."); Console.WriteLine(); if (a & b) Console.WriteLine("a & b - ."); else Console.WriteLine("a & b - ."); if(a & c) Console.WriteLine("a & - ."); else Console.WriteLine("a & - ."); if(a | b) Console.WriteLine("a | b - ."); else Console.WriteLine("a | b - ."); if(a | c) Console.WriteLine("a | - ."); else Console.WriteLine("a | ~ .");

(

: : 5, , 7 : 10, 10, 10

9.

'

241

: , , - . & b - , & - , | b - , | - . o p e r a t o r | ( ) , o p e r a t o r &() o p e r a t o r ! () bool. , "" (.. , bool). , bool. , , bool. , , , .

, & & | | , , . -, & | . -, &- |- , . -, , . -, t r u e f a l s e . . , & | ThreeD, & | | , & . /* !, | & ThreeD. && ||. */ using System; // , class ThreeD { int x, , z; // 3-- . public ThreeD() { = = z = 0; } public ThreeD(int i, int j, int k) { x = i; = j; z = k; } // |" // . public static ThreeD operator |(ThreeD opl, ThreeD op2) { if( ((opl.x != 0) || (opl. != 0) || (opl.z != 0)) |( ( o p 2 . x ! = 0) | | ( o p 2 . y ! = 0) r e t u r n new T h r e e D ( 1 , 1, 1 ) ; else r e t u r n new T h r e e D ( 0 , 0, 0 ) ; || (op2.z != 0)) )x v

242

I. #

// " & " // . public static ThreeD operator &(ThreeD opl, ThreeD op2) { if( ((opl.x != 0) && (opl. != 0) && (opl.z != 0)) &( ( o p 2 . x ! = 0) & & ( o p 2 . y ! = 0) r e t u r n new T h r e e D ( 1 , 1, 1 ) ; else r e t u r n new T h r e e D ( 0 , 0, 0 ) ; & & (op2.z ! 0)) )

// "!". public static bool operator !(ThreeD op) { if(op) return false; else return true; // true. public static bool operator true(ThreeD op) { if((op.x != 0) || (op. != 0) || (op.z != 0)) return true; // // , else return false; // false. public static bool operator false(ThreeD op) { if((op.x == 0) && (op. == 0) && (op.z == 0)) return true; // , else return false; // X, Y, Z. public void show() { Console.WriteLine(x + ", " + + ", " + z)

class TrueFalseDemo { public static void Main() { ThreeD a = new ThreeD(5, 6, 7 ) ; ThreeD b = new ThreeD(10, 10, 10); ThreeD = new ThreeD(0, 0, 0) ; Console.Write(" : " ) ; a.show(); Console.Write(" b: " ) ; b.show(); Console.Write(" : " ) ; .show(); Console.WriteLine();

9.

243

if(a) Console.WriteLine("a - ."); if(b) Console.WriteLine("b - ."); if() Console.WriteLine(" - ."); if(!a) Console.WriteLine("a - ."); if(!b) Console.WriteLine("b - ."); if(!c) Console.WriteLine(" - ."); Console.WriteLine(); Console.WriteLine(" & | " ) ; if(a & b) Console.WriteLine("a & b - ."); else Console.WriteLine("a & b - ."); if(a & c) Console.WriteLine("a & - ."); else Console.WriteLine("a & - ."); if(a | b) Console.WriteLine("a | b - ."); else Console.WriteLine("a | b - ."); if(a | c) Console.WriteLine("a | - ."); else Console.WriteLine("a | - ."); Console.WriteLine(); // && ||, / / . Console.WriteLine( " "" && | | " ) ; if(a && b) Console.WriteLine("a && b - ."); else Console.WriteLine("a && b - ."); if(a && ) Console.WriteLine("a && - ."); else Console.WriteLine("a && - ."); if(a || b) Console.WriteLine("a M b - ."); else Console.WriteLine("a || b - ."); if(a || c) Console.WriteLine("a I - ."); I else Console.WriteLine("a II - .");

: : 5, , 7 : 10, 10, 10 : 0, 0, 0 - , b - . - . & | & b - , & - , | b - , | - .

244

I. #

"" && || && b - , && - . || - , | 1 - . ' & | . .

// "|" // . public static ThreeD operator |(ThreeD opl, ThreeD op2) { if( ((opl.x != 0) || (opl. != 0) || (opl.z != 0)) | ((op2.x != 0) || (op2.y != 0) || (op2.z != 0)) ) return new ThreeD(1, 1, 1 ) ; else return new ThreeD(0, 0, 0 ) ; } // " & " // . public static ThreeD operator &(ThreeD opl, ThreeD op2) { if( ((opl.x != 0) && (opl. != 0) && (opl.z != 0)) &( ( o p 2 . x ! = 0) & & ( o p 2 . y ! = 0) r e t u r n new T h r e e D ( 1 , 1 , 1 ) ; else r e t u r n new T h r e e D ( 0 , 0 , 0) ; & & (op2.z != 0)) )

}

, ThreeD, , . , ThreeD (.. , ). , ThreeD- (.. , ). , I i f ( & b) Console.WriteLine("a & b - ."); I e l s e Console.WriteLine("a & b - ."); & b ThreeD-, . ThreeD t r u e f a l s e , t r u e , bool. t r u e , " & b - . ". , ThreeD . . o p e r a t o r t r u e ( " | | " ) o p e r a t o r f a l s e ( "&&"). , &- |- . "&" " | " . , &&- | | - &- | - , . , , : 9. 245

if(a | ) Console.WriteLine("a I - ."); J I t r u e . , | - . -: I i f ( | | a) C o n s o l e . W r i t e L i n e ( " II - ."); t r u e , . | -, , , . , , , , . t r u e f a l s e , . , . , "&" " | ", .

, . . ( ). # o p e r a t o r , . . , . #- , " ". : . : p u b l i c s t a t i c e x p l i c i t o p e r a t o r _ ( __ v) [ r e t u r n ; ] public s t a t i c implicit operator _ ( _ v) [return ;] _ , ; _ , ; v . _, . i m p l i c i t , , .. , _. e x p l i c i t , . , , e x p l i c i t - , implicit- . ThreeD. ,

246

I. #

ThreeD , i n t . . implicit- , : public static implicit operator int(ThreeD opl) return opl.x * opl. * opl.z; , . // implicit- . using System; // , class ThreeD { int x, , z; // 3-- . public ThreeD() { = = z = 0; } public ThreeD(int i, int j , int k) { x = i ; = j ; z = k;

}

// "+". public static ThreeD operator +(ThreeD opl, ThreeD op2) ThreeD result = new ThreeD(); result.x = opl.x + op2.x; result. = opl. + op2.y; result.z = opl.z + op2.z; return result; // ThreeD int. public static implicit operator int(ThreeD opl) return opl.x * opl. * opl.z; // X, Y, Z. public void show() Console.WriteLine(x + ", " + + ", " + z ) ;

c l a s s ThreeDDemo { p u b l i c s t a t i c v o i d Main() {, ThreeD a = new T h r e e D ( 1 , 2, 3 ) ; ThreeD b = new ThreeD(10, 10, 1 0 ) ; ThreeD = new T h r e e D ( ) ; int i; C o n s o l e . W r i t e ( " : a.show(); 9. ");

247

Console.WriteLine(); Console.Write(" b: " ) ; b.show(); Console.WriteLine() ; = a + b; // b. Console.Write(" + b: " ) ; c.show(); Console.WriteLine() ; i = a; // int. Console.WriteLine( " i = a: " + i) ; Console.WriteLine(); i = a * 2 - b ; // int. Console.WriteLine( " * 2 - b: " + i)

:

: 1, 2, 3 : 10, 10, 10 + : 1 1 , 12, 13 i = : * 2 - : -988 , ThreeD ( i = ), . 6, , . int-, . = + b o p e r a t o r i n t () . , . , - double- long-. . , , , , . , . . , , , i n t : // . using System; // .

248

I. #

class ThreeD { int x, y, z; // 3-- . public ThreeD() { x = = z = 0; } public ThreeD(int i, int j, int k) { x = i; = j; z = k; } // "+". public static ThreeD operator +(ThreeD opl, ThreeD op2) { ThreeD result = new ThreeD(); result.x = opl.x + op2.x; result. = opl. + op2.y; result.z = opl.z + op2.z; return result; / / explicit-, public static explicit operator int(ThreeD opl) { return opl.x * opl. * opl.z; // X, Y, Z. public void show() { Console.WriteLine(x + ", " + + ", " + z) ;

class ThreeDDemo { public static void Main() { ThreeD a = new ThreeD(1, 2, 3 ) ; ThreeD b = new ThreeD(10, 10, 10); ThreeD = new ThreeD(); int i; Console.Write(" : " ) ; a.show(); Console.WriteLine(); Console.Write(" b: " ) ; b.show(); Console.WriteLine() ; = a + b; // b. Console.Write(" + b: " ) ; c. show() ; Console.WriteLine(); i = (int) a; // // int, // . Console.WriteLine(" i = : " + i ) ; Console.WriteLine(); / i = (int)a * 2 - (int)b; // .

9.

249

Console.WriteLine( " * 2 - b :

ff

+ i);

e x p l i c i t , i n t . , I i = ( i n t ) a; , . . . , double i n t . Object . , . . ( . 11.) . ( . 12.) , . , . , . -, , , . -, . , .

, , #-. . , " + " , ThreeD, " + " , . , , " + " , (/). , ( ) , .

250

I. #

I

. . , , . . , ( , "+="). , . && | | [] () new is sizeof typeof ? -> = (()) , , , , . , "+=". , , . , "+=" operator+ (). , ThreeD. ThreeD a = new ThreeD(l, 2, 3 ) ; ThreeD b = new ThreeD(10, 10, 10); b += a; // b . o p e r a t o r + ( ) ThreeD, b 11,12,13. . ( [ ] ) , ( o p e r a t o r ()), , .

ThreeD. , . , , , . , . , , , . . , nybble. , 1 nybble, . , . nybble- . Nybble, . i n t , 015. : Nybble- Nybble-; 251

9.

int- Nybble-; Nybble- int-; (>) ((Nybble opl, Nybble op2) { if(opl.val > op2.val) return true; else return false; // " 0) return v a l ; else return 0;

class NSDemo { public static void Main() { // CountDown // . CountDown cdl = new CountDown(10); 436 I. #

int

i;

do { i = cdl.count(); Console.Write (i + " " ) ; } while(i > 0); Console.WriteLine(); CountDown cd2 = new CountDown(20); do { i = cd2.count(); Console.Write(i + " " ) ; } while(i > 0 ) ; Console.WriteLine(); cd2.reset(4); do { i = cd2.count(); Console.Write (i + " " ) ; } while(i > 0 ) ; Console.WriteLine();

: . , . , System Counter.

using using : using = ; , . , , Count Counter.CountDown. // . using System; // Counter.CountDown. using Count = Counter.CountDown; // . namespace Counter { // . class CountDown { int val; public CountDown(int n) { val = n; } p u b l i c v o i d r e s e t ( i n t n) { , 437

16. ,

val

= n;

public int count() { if(val > 0) return v a l ; else return 0;

class NSDemo { public static void Main() { // Count // Counter.CountDown. Count cdl = new Count(10); int i; do { i = cdl.count(); Console.Write(i + " " ) ; } while(i > 0 ) ; Console.WriteLine(); Count cd2 = new Count(20); do { i = cd2.count() ; Console.Write(i + " " ) ; } while(i > 0) ; Console.WriteLine(); cd2.reset (4); do { i = cd2.count(); Console.Write (i + " " ) ; } while(i > 0 ) ; Console.WriteLine();

Count Counter.CountDown, CountDown . , I Count cdl = new Count(10); CountDown.

. . , Counter. CountDown, Countup. Counter . ll , using System; I. #

I

438

// "" Counter, using Counter; // // Counter, namespace Counter { // . class CountDown { int val; public CountDown(int n) { val = n; public void reset(int n) { val - n; public int count() { if(val > 0) return v a l ; else return 0;

// // Counter, namespace Counter { // , class CountUp { int val; int target; public int Target { get{ return target;

public CountUp(int n) { target = n; val = 0; public void^ reset (int n) { target = n; val = 0; public int count() { if(val < target) return val++; else return target;

class NSDemo { public static void Main() {

16. ,

439

CountDown cd = new CountDown(10) CountUp cu = new CountUp(8); int i; do { i = cd.count(); Console.Write (i + " " ) ; } while (i > 0 ) ; Console.WriteLine(); do { i = cu.count(); Console.Write(i + " " ) ; } while(i < cu.Target);

1

: 10 9 8 7 6 5 4 3 2 1 0 0 1 2 3 4 5 6 7 8

. I using Counter; "" . CountDown CountUp , . , Counter , .

. : // . using System; namespace NS1 { class ClassA { public ClassA() { Console.WriteLine(" ClassA."); } } namespace NS2 { // , class ClassB { public ClassB() { Console.WriteLine(" ClassB.");

c l a s s NestedNSDemo { p u b l i c s t a t i c v o i d Main() { N S 1 . C l a s s A a= new N S 1 . C l a s s A ( ) ; // N S 2 . C l a s s B b = new N S 2 . C l a s s B ( ) ; // ! ! !

440

I. #

// NS2 . NS1.NS2.ClassB b = new NS1.NS2.ClassB(); // // . : ClassA. ClassB.

I

NS2 NS1. , ClassB , : NS1, NS2. NS2 . , . , . , namespace OuterNS { namespace InnerNS {

: namespace OuterNS.InnerNS {

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

# , . , , . "" C++, # , C++. , , , (preprocessor). , . 16. , 441

# :#define #endregion, #region #elif terror #undef #else #if #warning #endif #line

" # " . , . , # , , . , . . # define #def i n e , . # i f # e l i f , . #def i n e : #define , . #def i n e , . , EXPERIMENTAL, : 1 #define EXPERIMENTAL^SB^SSiil

C/C++ #define , , , . #define C# . # #define .

#if #endif # i f #endif , , . , . . , #def ine, . # i f : #if _ _ #endif , # i f (_), , , #endif ( _), . . #endif tif-.

442

I. #

. : ! ==, !=, && | | , . . #if, #endif #def ine. // #if, #endif // tdefine. #define EXPERIMENTAL using System; class Test { public static void Main() { #if EXPERIMENTAL Console.WriteLine( " ."); #endif Console.WriteLine( " ."); : . .

I

#define EXPERIMENTAL. # i f EXPERIMENTAL , ( ) WriteLine ()-. EXPERIMENTAL , WriteLine ()- , # i f . WriteLine () - , # if-. , # i f . : // . #define EXPERIMENTAL #define TRIAL using System; class Test { public static void Main() { #if EXPERIMENTAL Console.WriteLine( " ."); #endif #if EXPERIMENTAL && TRIAL Console.Error.WriteLine( " ."); #endif

16. ,

443

Console.WriteLine( " .") : . . .

(

, EXPERIMENTAL TRIAL. WriteLine () - , .

#else #elif # e l s e else- #, .. , # i f . . // #else. #define EXPERIMENTAL using System; class Test { public static void Main() { #if EXPERIMENTAL Console.WriteLine( " ."); #else Console.WriteLine(" -."); #endif #if EXPERIMENTAL && TRIAL Console.Error.WriteLine( " ."); #else Console.Error.WriteLine( " ."); #endif Console.WriteLine( " ."); : . . .

(

TRIAL , #1- . , # e l s e #if-, telse-, #if #endif. 444 I. #

# e l i f " " i f - e l s e- i f - . # e l i f . , (_) , #elif- . telif-. #el i f - : #if _ _ # e l i f _ _ # e l i f 1_ _ # e l i f _ _ # e l i f _

#endif : // #elif. #define RELEASE using Systemsclass Test { public static void Main() { #if EXPERIMENTAL Console.WriteLine( " ."); #elif RELEASE Console.WriteLine(" -."); #else Console.WriteLine( " ."); #endif #if TRIAL && !RELEASE Console.WriteLine(" ."); #endif Console.WriteLine( " ."); : -. .

(

#undef #undef , . #undef : #undef 16. , 445

: #define SMALL #if SMALL // ... #undef SMALL // SMALL . #undef SMALL . #undef , . # error # e r r o r . . t e r r o r : terror ___ t e r r o r __ . , I t e r r o r ! , " !". #warning twarning t e r r o r , , . . twarning : twarning _ #line t l i n e , t l i n e . . t l i n e :

t l i n e "_"

, , ___ , . t l i n e . , d e f a u l t : | t l i n e default

446

I. #

# region # end region #region #endregion , (IDE) Visual Studio. :

#region _ // _

#endregion , __ .

internal #- (assembly), . .NET-. Microsoft, " .NET Framework." , . . . (manifest). . , , , " ". , , . , . , Microsoft Intermediate Language (MSIL). , , . , # , ( ) . , , #, , . , . . ( .NET-, #.) # : i n t e r n a l . - .

i n t e r n a l p u b l i c , p r i v a t e p r o t e c t e d , , # i n t e r n a l . , , , . , , i n t e r n a l , , - . i n t e r n a l . 16. , 447

i n t e r n a l , . i n t e r n a l . i n t e r n a l p r o t e c t e d . p r o t e c t e d i n t e r n a l , . , p r o t e c t e d i n t e r n a l , . . i n t e r n a l . // internal. using System; class InternalTest { internal int x; class InternalDemo { public static void Main() { InternalTest ob = new InternalTest(); ob.x = 10; // : .

Console.WriteLine(" ob.x: " + ob.x);

I n t e r n a l T e s t i n t e r n a l . , , InternalDemo, .

448

I. #

,

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

(runtime type identification RTTI) , . , , . RTTI , , , . ( ). # , : i s , as typeof. .

i s i s , . : i s . ( ) , . . , , , . i s . // i s . using System;c l a s s A {} c l a s s : A {}

class Usels { public static void Main() { A a = new A () ; b = new ();

450

I. #

if(a is A) Console.WriteLine(" A . " ) ; if(b is A) Console.WriteLine(" b , " + " . " ) ; if(a is ) Console.WriteLine(" , " + " .") if(b is ) Console.WriteLine(" b . " ) ; if(a is object) Console.WriteLine(" .");

: . b , . b . -- .

fI

, bie . : i f ( b i s A) Console.WriteLine(" b , " + " . " ) ;

i f- , b , . , b . . i f ( a i s ) Console.WriteLine(" , " + " . " ) ; if- , , . , .

as , , . as, : as , , . . . a s i s . , , , i s : // i s // . using System;c l a s s A {} c l a s s : {}

17. ,

451

class CheckCast { public static void Main() { A a = new A ( ) ; b = new (); // , . if(a is ) // // , b = () ; else // // , b = null; if(b==null) Console.WriteLine( " b = () ."); else Console.WriteLine( " b = () .")

: b = () . , , , if. , . , . as , . // a s . using System; class A {} class : A {} class CheckCast { public static void Main() { A a = new A(); b = new () ; b = a as ; // , // . if(b==null) Console.WriteLine(" " + "b = () ."); else Console.WriteLine( " b = () .");

: b = () .

452

I. #

as , , , , .

typeof as i s , ( -) . . # typeof. System.Type . , . typeof : typeof() , . , typeof, , . -, , , , . , , . , , , : FullName, I s C l a s s I s A b s t r a c t . FullName . I s C l a s s t r u e , . I s A b s t r a c t t r u e , . // typeof. using System; using System.10; class UseTypeof { public s t a t i c void Main() { Type t = typeof(StreamReader); Console.WriteLine(t.FullName); if (t. IsClass) Console. WriteLine (" ") ; if(t.IsAbstract) Console.WriteLine( " ."); else Console.WriteLine(" .");

(

: System.10.StreamReader . .

StreamReader. , , .

17. ,

453

, (reflection) #, . : , , , . " " , () , . , , , . , , .NET Reflection API, System.Reflection. , , , : I using S y s t e m . R e f l e c t i o n ;

: system. Type System. , "" , . , . System.Reflection.Memberlnf. Member Info , : D e c i a n n g T y p e ,

MemberTypes MemberTypes t r i n g Name Type ReflectedType

, MemberType MemberTypes. MemberTypes , , . : MemberTypes.Constructor v MemberTypes.Method MemberTypes.Field MemberTypes.Event MemberTypes.Property , , MemberType. , MemberType MemberTypes .Method, , . Memberlnf : GetCustomAttributes () I sDefined (). . , Memberlnf , "". , .

454

I. #

C o n s t r u c t o r i n f [ ] G e t C o n s t r u c t o r s () E v e n t l n f [ ] G e t E v e n t s () F i e i d i n f [ ] G e t F i e i d s () M e m b e r i n f [ ] GetMembers () M e t h o d i n f [ ] G e t M e t h o d s () P r o p e r t y i n f [ ] G e t P r o p e r t i e s ()

, . Assembly Assembly TypeAttributes BaseType s t r i n g FuliName bool isAbstract bool i sArray bool isClass b o o l isEnum s t r i n g Namespace Attributes , , , ,

, . , , , , . , , . Reflection API , . ( Reflection API !) Reflection API , , , , . : , , .

Type- , . GetMethods (). : Methodinf[] GetMethods()

17. ,

455

GetMethods () Methodlnf , , . Methodlnf System. Reflection. Methodlnfo MethodBase, , , Memberlnfo. , , . , , Name. Methodlnfo: ReturnType GetParameters (). ReturnType, , , . GetParameters () , . : Parameterlnfo[] GetParameters() Parameterlnfo. Parameterlnfo , . : Name, , , ParameterType, . . , , MyClass. , , . // . using System; using System.Reflection; class MyClass { int x; int y; public MyClass(int i, int j) { x = i; = j; } public int sum() { return x+y; } public bool isBetween(int i) { if(x < i && i < y) return true; else return false; public void s e t ( i n t a, i n t b) { x = a; = b;}

public void set(double a, double b) { x = (int) a; = (int) b; 456 I. #

public void show() { Console.WriteLine(" x: {0}, y: {1}", x, y ) ;

class ReflectDemo { public static void Main() { Type t = typeof(MyClass); // Type-, // MyClass. Console.WriteLine( " , " + t.Name); Console.WriteLine(); Console.WriteLine(" Methodlnfo[] mi = t.GetMethods(); // , MyClass. foreach(Methodinf m in mi) { // , , / / . Console.Write(" " + m.ReturnType.Name + 11 " + m.Name + " (") ; // . '~ Parameterlnfo[] pi = m.GetParameters(); for(int i=0; i < pi.Length; i Console.Write(pi[i].ParameterType.Name + 11 " + pi[i] .Name) ; if(i+l < pi.Length) Console.Write(", " ) ; : " ) ;

Console.WriteLine(")"); Console.WriteLine();

: , MyClass : Int32 G e t H a s h C o d e O Boolean Equals(Object String ToStringO obj)

Int32 sum() Boolean isBetween(Int32 i) Void set(Int32 a, Int32 b)

17. ,

457

Void set(Double a, Double b) Void show() Type GetType() , , MyClass, , o b j e c t . , # o b j e c t . , .NET-. , s e t () . : s e t ( ) . i n t , double. . , , MyClass public- public-, s e t (). Main () , MyClass: Type t = typeof(MyClass); // Type-, // MyClass.

I

t Reflection API, , MyClass. : I MethodlnfoU mi = t . G e t M e t h o d s ( ) ; f oreach, , : // , , / / . Console.Write( " + m.ReturnType.Name + " " + m.Name + "(") ; // . Parameterlnf [ ] pi = m.GetParameters () ; for(int i=0; i < pi.Length; i++) { Console.Write(pi[i].ParameterType.Name + " " + pi[i].Name); if(i+l < pi.Length) Console.Write(", " ) ; } GetParameters () p i . for p i . , , .. MyClass.

GetMethods () GetMethods () , . : Methodlnfo[] GetMethods(BindingFlags flags) , . BindingFlags . :

458

I. #

DeciaredOnly instance NonPublic Public static

, . -- public- static-

. P u b l i c NonPublic I n s t a n c e S t a t i c . GetMethods () . BindingFlags-, GetMethods ( ) , , . , , . , , GetMethods () : // , // MyClass. Methodlnfo[] mi = t.GetMethods(BindingFlags.DeciaredOnly | BindingFlags.Instance | BindingFlags.Public) ; : , MyClass : Int32 sum() Boolean isBetween(Int32 i) Void set(Int32 a, Int32 b) Void set(Double a, Double b) Void show() , , MyClass.

, , . Invoke , Methodlnfo. : object Inyoke(object ob, o b j e c t [ ] args) ob , . static- ob n u l l . , , args. , args n u l l . args , . , , args , , , . 17. , 459

invoke () Methodlnfo, GetMethods (). : // . using System; using System.Reflection; class MyClass { int x; int y; public MyClass(int i, int j) { x = i; = j; } public int sum() { return x+y; } public bool isBetween(int i) { if((x < i) && (i < y)) return true; else return false; } public void set (int a, int b) { Console.Write(" set(int, int). " ) ; . x = a; = b; show(); } // set. public void set(double a, double b) { Console.Write(" set(double, double). " ) ; x = (int) a; = (int) b; show(); p u b l i c v o i d show() { Console.WriteLine( " x:

{0},

:

{1}",

x,

);

class InvokeMethDemo { public static void Main() { Type t = typeof(MyClass); MyClass reflectOb = new MyClass(10, 20); int val; Console.WriteLine(" , t.Name); Console.WriteLine() ; M e t h o d l n f o [ ] mi = t . G e t M e t h o d s ( ) ;

460

I. #

// , foreach(Methodlnfo m in mi) { // . Parameterlnf[] pi = m.GetParameters(); if(m.Name.CompareTo("set")==0 && pi[0].ParameterType == typeof(int)) { object [] args = new object[2]; args[0j = 9; args[l] = 18; m.Invoke(reflectOb, args); } else if(m.Name.CompareTo("set")==0 && pi[0].ParameterType typeof(double)) { object[] args = new object[2]; args[0] = 1.12; args[l] = 23.4; m.Invoke(reflectOb, args); } else if(m.Name.CompareTo("sum")==0) { val = (int) m.Invoke(reflectOb, null); Console.WriteLine( " sum " + val) } else if(m.Name.CompareTo("isBetween")==0) { object[] args = new object[1]; args[0J = 14; if((bool) m.Invoke(reflectOb, args)) Console.WriteLine("14 . " ) ; } else if(m.Name.CompareTo("show")==0) { m.Invoke(reflectOb, null);

" , MyClass sum 30 14 . s e t ( i n t , i n t ) . : 9, : 18 set(double, double). : 1, : 23 : 1, : 23 , . . foreach . , i f /else-, . s e t (): if(m.Name.CompareTo("set")==0 && pi[0].ParameterType == typeof(int)) { object[] args = new object[2]; args[0] = 9; args[l] = 18; m.Invoke(reflectOb, args);

17. ,

461

e l s e if(m.Name.CompareTo("set")==0 & & p i [ 0 ] . P a r a m e t e r T y p e typeof(double)) o b j e c t [ ] a r g s = new o b j e c t [ 2 ] ; args[0] = 1.12; a r g s [ l ] = 23.4; m.Invoke(reflectOb, args);

{

s e t , , s e t ( ) . , s e t ( i n t , i n t ) , a r g s int- s e t ( ) . s e t ( ) double.

, ( MyClass), MyClass . , . , . . , . , . , G e t C o n s t r u c t o r s () . : Constructorlnf[] GetConstructors() C o n s t r u c t o r l n f o , . C o n s t r u c t o r l n f o MethodBase, Member Info. C o n s t r u c t o r l n f o . GetParameters ( ) , , . GetParameters ( ) , Methodlnf . , , Invoke ( ) , C o n s t r u c t o r l n f o . Invoke () : object Invoke(object[] args) , , args. , args null-. args . invoke () . MyClass: // . using System; using System.Reflection;

462

I. #

class MyClass { int x; int y; public MyClass(int i) { Console.WriteLine( " MyClass(int). " ) ; x = = i; public MyClass(int i, int j) { Console.WriteLine( " MyClass(int, int). " ) ; x = i; = j; show(); public int sum() { return x+y; public bool isBetween(int i) { if((x < i) && (i < y)) return true; else return false; public void set(int a, int b) { Console.Write(" set(int, int). " ) ; x = a; = b; show () ; // set(). public void set(double a, double b) { Console.Write(" set(double, double). ") ; x = (int) a; = (int) b; show(); public void show() { Console.WriteLine( " : {0}, : {1}", x, )

c l a s s InvokeConsDemo { p u b l i c s t a t i c void Main() { Type t = typeof(MyClass); int val; // . Constructorinf[] ci = t.GetConstructors(); Console.WriteLine(" : " ) ;

17. ,

463

foreach(Constructorinf in ci) { // . Console.Write(" " + t.Name + " ( " ) ; // . Parameterlnf[] pi = .GetParameters(); for(int i=0; i < pi.Length; Console.Write(pi[i].ParameterType.Name + " " + pi[i].Name); if(i+l < pi.Length) Console.Write(", " ) ; Console.WriteLine(")"); } Console.WriteLine(); // , int x; for(x=0; x < ci.Length; x++) { Parameterlnf[] pi = ci[x].GetParameters(); if(pi.Length == 2) break; if(x == ci.Length) { Console.WriteLine( " ."); return; } else Console.WriteLine( " .\"); // . object[] consargs = new object[2]; consargs[0] = 10; consargs [1] = 20; object reflectOb = ci[x].Invoke(consargs); Console.WriteLine( " reflectOb."); Console.WriteLine(); Methodlnfo[] mi = t.GetMethods(); // . foreach(Methodinf m in mi) { // . Parameterlnf[] pi = m.GetParameters(); if(m.Name.CompareTo("set")==0 && pi[0].ParameterType == typeof(int)) { // set(int, int). object[] args = new object[2]; args[0] = 9; args [1] = 18; m.Invoke(reflectOb, args);

464

I. #

else if (m.Name.CompareTo ("set")0 && pi[0].ParameterType == typeof(double)) { // set(double, double). object[] args = new object[2]; args[0] = 1.12; args[l] = 23.4; m.Invoke(reflectOb, args); } else if(m.Name.CompareTo("sum")==0) { val = (int) m.Invoke(reflectOb, null); Console.WriteLine( " sum() " + val); } else if(m.Name.CompareTo("isBetween")==0) { object[] args = new object[1]; args[0] = 14; if((bool) m.Invoke(reflectOb, args)) Console.WriteLine("14 . " ) ; } else if(m.Name.CompareTo("show")==0) { m.Invoke(reflectOb, null);

: : MyClass(Int32 i) MyClass(Int32 i , Int32 j) . MyClass(int, : 10, : 20 reflectOb. int).

sum() 30 14 . s e t ( i n t , i n t ) . : 9, : 18 set(double, double). : 1, : 23 : 1, : 23 , MyClass. : I Constructorlnf[] ci = t.GetConstructors(); , . , , : for(x=0; < ci.Length; x++) { Parameterlnfo[] pi = ci[x].GetParameters(); if(pi.Length == 2) break;

|

1

( ), , : 17. , 465

// . object[] consargs = new o b j e c t [ 2 ] ; consargs[0] = 10; consargs[1] = 20; object reflectOb = ci[x].Invoke(consargs);

Invoke () r e f l e c t O b MyClass. , , int-, , MyClass. .

MyClass, : MyClass. MyClass, , MyClass, typeof , ( ). , , . 16, , ., . Reflection API , . , , " " . . , , , . , , , , . , . Assembly. Assembly p u b l i c . Assembly , . , LoadFromO . : s t a t i c Assembly LoadFrom(string _) _ . Assembly, GetTypes (). : [] GetTypes() , . , . . MyClasses.cs : I // . | // MyClasses.cs. 466 I. #

using System; class MyClass { int x; int y; public MyClass(int i) { Console.WriteLine( " MyClass(int). x = = i; show(); public MyClass(int i, int j) { Console.WriteLine( " MyClass(int, int). ") x = i; = j; show(); public int sum() { return x+y; public bool isBetween(int i) { if((x < i) && (i < y)) return true; else return false; public void set(int a, int b) { Console.Write(" set(int, int). " ) ; x = a; = b; show () ; // set. public void set(double a, double b) { Console.Write(" set(double, double). " ) ; x = (int) a; = (int) b; show(); public void show() { Console.WriteLine( " : {0}, : {1}", x, ) ;

class AnotherClass { string remark; public AnotherClass(string str) { remark = str;

17. ,

467

public void show() { Console.WriteLine(remark);

class Demo { public static void Main() { Console.WriteLine(" .");

MyClass, . , AnotherClass Demo. , , , . , MyClasses.exe. , . , ^lyClasses.exe. /* , , . */ using System; using System.Reflection; class ReflectAssemblyDemo { public static void Main() { int val; // MyClasses.exe. Assembly asm = Assembly.LoadFrom("MyClasses.exe"); // , MyClasses.exe. [] alltypes = asm.GetTypes(); foreach(Type temp in alltypes) Console.WriteLine(": " + temp.Name); Console.WriteLine(); // , // MyClass. Type t = alltypes[0]; // // . Console.WriteLine(": " + t.Name); // . Constructorlnfo[] ci = t.GetConstructors(); Console.WriteLine(" : " ) ; foreach(Constructorlnfo in ci) { // . Console.Write(" " + t.Name + " ( " ) ; // . Parameterlnfo[] pi = c.GetParameters(); for(int i=0; i < pi.Length; i++) { Console.Write(pi[i].ParameterType.Name + 468 I. #

" " + pi[i].Name); if(i + l < pi.Length) Console.Write(", ") ; } Console.WriteLine(")"); } Console.WriteLine() ; // , int x; for(x=0; x < ci.Length; x++) { Parameterlnfo[] pi = ci[x].GetParameters(); if(pi.Length == 2) break; } if(x == ci.Length) { Console.WriteLine( " ."); return; } else Console.WriteLine( " .\n") ; // . object[] consargs = new object[2]; consargs[0] = 1 0 ; consargs[1] = 20; object reflectOb = ci[x].Invoke(consargs); Console.WriteLine( " reflectOb."); Console.WriteLine(); Methodlnfo[] mi = t.GetMethods(); // . foreach(Methodlnf m in mi) { // . Parameterlnfo[] pi = m.GetParameters(); if(m.Name.CompareTo("set")==0 && pi[0].ParameterType == typeof(int)) { // set(int, int). object[] args = new object[2]; args[0] = 9; args[1] = 18; m.Invoke(reflectOb, args); } else if(m.Name.CompareTo("set")==0 && pi[0].ParameterType == typeof(double)) { // set(double, double). object[] args = new object[2]; args[0] = 1.12; args[l] = 23.4; m.Invoke(reflectOb, args); } else if(m.Name.CompareTo("sum")==0) {

17. ,

469

val = (int) m.Invoke(reflectOb, null); Console.WriteLine( " sum() " + val) ; } else if(m.Name.CompareTo("isBetween")==0) { object[] args = new object[1]; args[0] = 14; if((bool) m.Invoke(reflectOb, args)) Console.WriteLine("14 . " ) ; } else if(m.Name.CompareTo("show")==0) { m.Invoke(reflectOb, null);

: : MyClass : AnotherClass : Demo : MyClass : MyClass(Int32 i) MyClass(Int32 i, Int32 j) . MyClass(int, int). : 10, : 20 reflectOb. sum() 30 14 . set(int, int). : 9, : 18 set(double, double). : 1, : 23 : 1, : 23 '

, , MyClasses.exe. , MyClass, . MyClasses. . , MyClasses.exe, , Main (): // MyClasses.exe. Assembly asm = Assembly.LoadFrom("MyClasses.exe"); // , MyClasses.exe. Type[] alltypes = asm.GetTypes(); foreach(Type temp in alltypes) Console.WriteLine(": " + temp.Name);

470

I. #

, . , -. (dynamic link library DLL), d l l . , MyClasses. cs : I esc / t : l i b r a r y MyClasses.es MyClasses.dll. DLL- Main(). - , Main (), . Demo Main (). DLL- . MyClass DLL-, LoadFromO : I Assembly asm = Assembly.LoadFrom("MyClasses.dll");

, . MyClass , MyClass. , ( s e t ( ) sum ). , . , , . , , , , . , , , MyClasses.exe, MyClass, . // MyClass // . using System; using System.Reflection; class ReflectAssemblyDemo { public s t a t i c void Main() { int val; Assembly asm = Assembly.LoadFrom("MyClasses.exe"); Type[] alltypes = asm.GetTypes(); Type t = a l l t y p e s [ 0 ] ; // // . Console.WriteLine(": " + t.Name); Constructorlnfo[] ci = t.GetConstructors(); // . 17. , * 471

Parameterlnfof] cpi = ci[0].GetParameters(); object reflectOb; if(cpi.Length > 0) { object[] consargs = new object[cpi.Length]; // . for(int n=0; n < cpi.Length; n++) consargs[n] = 10 + n * 20; // . reflectOb = ci[0].Invoke(consargs) ; } else reflectOb = ci[0].Invoke(null); Console.WriteLine( " reflectOb."); Console.WriteLine(); // . Methodlnfo[] mi = t.GetMethods( BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public) ; // . foreach(Methodinf m in mi) { Console.WriteLine(" {0} ", m.Name); // . Parameterlnfo[] pi = m.GetParameters(); // , switch(pi.Length) { case 0: // if(m.ReturnType == typeof(int)) { val = (int) m.Invoke(reflectOb, null); Console.WriteLine(" " + val); } else if(m.ReturnType == typeof(void)) { m.Invoke(reflectOb, null); } break; case 1: // if(pif0].ParameterType == typeof(int)) { object [] args = new object[1]; args[0] = 14; if((bool) m.Invoke(reflectOb, args)) Console.WriteLine( "14 . " ) ; else Console.WriteLine( "14 . " ) ; } break; case 2: // if((pi[0].ParameterType == typeof(int)) &&

472

I. #

(pi[1].ParameterType == t y p e o f ( i n t ) ) ) o b j e c t [ ] a r g s = new o b j e c t [ 2 ] ; a r g s [ 0 ] = 9; a r g s [ 1 ] = 18; m.Invoke(reflectOb, a r g s ) ;}

{

e l s e i f ( ( p i [ 0 ] . P a r a m e t e r T y p e == t y p e o f ( d o u b l e ) ) & & (pi[1].ParameterType == t y p e o f ( d o u b l e ) ) ) { o b j e c t [ ] a r g s = new o b j e c t [ 2 ] ; a r g s [ 0 ] = 1.12; args[1] = 23.4; m.Invoke(reflectOb, a r g s ) ;}

break; Console.WriteLine();}

:

: MyClass M y C l a s s ( i n t ) . : 10, : 10 sum 20 isBetween 14 . s e t s e t ( i n t , i n t ) . : 9, : 18 reflectOb.

'

s e t s e t ( d o u b l e , d o u b l e ) . : 1, : 23 show : 1, : 23 . -, ( ) , MyClass. BindingFlags- GetMethods (). . -, , , . switch-. case- () () . .

17. ,

473

# . , , .. , , , . , , . , . .

, System. A t t r i b u t e . , A t t r i b u t e . A t t r i b u t e , . A t t r i b u t e . , , , E r r o r A t t r i b u t e . A t t r i b u t e U s a g e . , .

, . . , , , . : [AttributeUsage(AttributeTargets.All)] public class RemarkAttribute : Attribute { string pri_remark; // remark. public RemarkAttribute(string comment) { pri_remark = comment; } public string remark { get { return pri_remark; } . RemarkAttribute. A t t r i b u t e U s a g e , , RemarkAttribute . A t t r i b u t e U s a g e , ; . RemarkAttribute, A t t r i b u t e . RemarkAttribute pri_remark, remark, . , . 474 I. #

RemarkAttribute , p r i r e m a r k . , , RemarkAttribute, .

, . , , . , RemarkAttribute : [RemarkAttribute(" .")] class UseAttrib { RemarkAttribute, " .". UseAttrib. A t t r i b u t e . , : [Remark(" .")] class UseAttrib { Remark. , , .

, . . GetCustomAttributes ( ) , Memberlnfo . , , : object[] GetCustomAttributes(bool searchBases) searchBases t r u e , . , . GetCustomAttribute , A t t r i b u t e . : s t a t i c Attribute GetCustomAttribute(Memberlnfo mi, Type attrib type) mi Memberlnfo, , . attribtype. , , . , RemarkAttribute, : // RemarkAttribute. Type tRemAtt = typeof(RemarkAttribute); RemarkAttribute ra = (RemarkAttribute) Attribute.GetCustomAttribute(t, tRemAtt); 17. , 475

, . , , , , , . , remark: I Console.WriteLine(ra.remark); Rema r kAt t r i b u t e . // . using System; using System.Reflection; [AttributeUsage(AttributeTargets.All)] public class RemarkAttribute : Attribute { string pri_remark; // remark. public RemarkAttribute(string comment) { pri_remark = comment;}

public string remark { get { return pri_remark;}

[RemarkAttribute(" .")] class UseAttrib {

class AttribDemo { public static void Main() { Type t = typeof(UseAttrib); Console.Write(" " + t.Name + ": ") ; object[] attribs = t.GetCustomAttributes(false); foreach(object in attribs) { Console.WriteLine(o); Console.Write("Remark: " ) ; // RemarkAttribute. Type tRemAtt = typeof(RemarkAttribute); RemarkAttribute ra = (RemarkAttribute) Attribute.GetCustomAttribute(t, tRemAtt); Console.WriteLine(ra.remark); : UseAttrib: RemarkAttribute Remark: .

I

476

I. #

Remark At t r i b u t e . , .. , comment, RemarkAttribute (), . , . # . , . , , . . , . , , : [ a t trib (__, ___1 = value, __2 = value, . . . ) ] ( ) . . . . . , , . RemarkAttribute, supplement, . [AttributeUsage(AttributeTargets.All)] public class RemarkAttribute : Attribute { s t r i n g pri_remark; // remark. public s t r i n g supplement; // . public RemarkAttribute(string comment) { pri_remark = comment; supplement = " ";}

public string remark { get { return pri_remark;

, supplement " " . . , , supplement : [RemarkAttribute(" .", supplement = " .")] class UseAttrib {

17. ,

477

, RemarkAttribute. . , , supplement, . . , . . , , . . , supplement: // . using System; using System.Reflection; [AttributeUsage(AttributeTargets.All)] public class RemarkAttribute : Attribute { string pri__remark; / / remark. public s t r i n g supplement; / / . public RemarkAttribute(string comment) { pri_remark = comment; supplement = " "; public string remark { get { return pri_remark;

[RemarkAttribute(" .", supplement = " .")] class UseAttrib {

class NamedParamDemo { public s t a t i c void Main() { Type t = typeof(UseAttrib); Console.Write(" " + t.Name + " : ") ;

object[] a t t r i b s = t.GetCustomAttributes(false); foreach(object in a t t r i b s ) { Console.WriteLine(o); // RemarkAttribute. Type tRemAtt = typeof(RemarkAttribute); RemarkAttribute ra = (RemarkAttribute) Attribute.GetCustomAttribute(t, tRemAtt); Console.Write("Remark: ") ; Console.WriteLine(ra.remark);

478

I. #

Console.Write("Supplement: " ) ; Console.WriteLine(ra.supplement);

I

: UseAttrib: RemarkAttribute Remark: . Supplement: .

, . , jlema r kAt tribute int- priority. // // . using System; using System.Reflection; [AttributeUsage(AttributeTargets.All)] public class RemarkAttribute : Attribute { string pri__remark; // remark. int pri_priority; // priority, public string supplement; // . public RemarkAttribute(string comment) { pri_remark = comment; supplement = " "; } public string remark { get { return pri_remark;

// , public int priority { get { return prijpriority; } set { prijpriority = value; }

[RemarkAttribute( " .", supplement = " .", priority = 10)] class UseAttrib {

class NamedParamDemo { 17. , 479

public static void