784
ПОЛНОЕ РУКОВОДСТВО С#4.0

Шилдт г. - c# 4.0 Полное Руководство - 2011

Embed Size (px)

DESCRIPTION

Carte de programare

Citation preview

#4.0C# 4.0

TheCompleteReferenceHERBERT SCHILDT

Do more.Learn more.MHPROFESSIONAL.COM#4.0

- 2011 32.973.26-018.2.75 57 681.3.07 "". .. .. "" :[email protected], http://www.williamspublishing.com, .57 C# 4.0: . : . . . : ".. ", 2011. 1056 .: . . . .ISBN 978-5-8459-1684-6 (.)

32.973.26-018.2.75 . , , , McGrow-Hill Higher Ed.Authorized translation from the English language edition published by McGraw-Hill Companies, Copyright 2010All rights reserved. Except as permitted under the Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of publisher, with the exception that the program listings may be entered, stored, and executed in a computer system, but they may not be reproduced for publication.Russian language edition published by Williams Publishing House according to the Agreement with R&I Enterprises International, Copyright 2011- C# 4.0:

.. .. .. . 17.09.2010. 70x100/16. Times. .. . . 85,14. .-. . 51,55. 1500 . 24007. CtP " " . . . 197110, -, ., 15. ". . ", 127055, . , . , . 43, . 1ISBN 978-5-8459-1684-6 (.) "", 2011ISBN 0-07-174116- (.) by The McGraw-Hill Companies, 2010

2325. #29 1. C#31 2. C#41 3. , 67 4. 97 5. 121 6. , 147 7. 177 8. 209 9. 269 10. 303 11. 329 12. , 375 13. 403 14. -431 15. , -473 16. , 513 17. , 537 18. 575 19. LINQ637 20. , , 681 II. C#717 21. System719 22. 783 23. . : 833 24. . : TPL885 25. , 923 26. 1011. 1039 1044

22 222325 27 27 27, 27 28 28. #29 1. C#31 C#32 32 C++33 Java33 C#34 C#36 C# .NET Framework37 NET Framework37 CLR38 38 39 2. C#41- 41424344 44 csc.exe45 Visual Studio46 50 53 54 54 57 58 58 60 61 63 C#6465 .NET Framework66 3. , 67 67 C#68 69 71 7374 75 7679 80 80 81 83 83 84 85 86 89 90 91 93 95 4. 97 97 98 101 104 106 107 107 , , 108 114 117 ?117 119 119 5. 121 if121 if122 if-else-if '124 switch125 switch129 for129 for131 while137 do-while138 foreach139 break 139 continue142 return143 goto143 6. , 147 147 148 149 153 154155 Building156 158 159 162 Building164 166166 168 Building169 new170 new 170" " 171172 this174 7. 177177 178 182 182 183 184 185 187 Length189 Length 191 192 foreach194198 198 199 203 205 switch206 8. 209 209 210 212 212 218 220 ref out222 ref223 out224 ref out 227 229 231 ,234 235 241 this 245 246 247 249 250 251 252 Main()254 MainQ254 Main()255257 static260 265 266 9. 269 270 270 273 C# 277 281 true false283 286 286 288 292 297 298 10. 303303 304 307 310 311313 318 319, 320 320 324 . 329 329 333 336 337 339 343 base 344 346 349 351 355 359 360 363 sealed367 object368 370 object 372 12. , 375375 377 381 383 385 387 388 388 391 .NET Framework391391 395397 399 399 399 13. 403 System.Exception404 404 try catch404 405 407 408 "" 410 catch411 412 try413 414 415 finally416 Exception418 420 422 426 checked unchecked428 14. -431 - C# 431 432 432 432 Stream432 434- 434 436 -436 436 ReadKey()438 440 FileStream - 441 441 -444 446 FileStream 448 - 449 Stream Writer449 StreamReader451 452 454 Binary Writer454 BinaryReader455 -457 461 MemoryStream463 StringReader StringWriter465 File467 ()467 Exists() GetLastAccessTime()468 469 15. , -473(473 476 '477 478 481 System.Delegate483 483 483 484 484 485 486-488-488 -489 -492494 496 497 500 504 - 504 .NET Framework506 EventHandler EventHandler 508 509 16. , 513 513 514 516 using518 using520 521 523 524 ::524528 #define529 #if #endif529 #else #elif531 #undef533 #533 #waming534 #534 #region #endregion534 #pragma '534 internal535 internal536 17. , 537 537 is538 as539 typeof540541 System.Type 541 543 544 548 550 555 560562 563 566 570 AttributeUsage570 Conditional571 Obsolete572 18. 575 576 576 580 580 583 585 585 586 594 new() 598 599 602 603 , 604 606 607 609 610 610 612 615 620 620 622 623 625 626 626 630 633 635 , *636 636 19. LINQ637 LINQ638 639 641 642 643 where644 orderby646 select649 from653 group655 into657 let 659 join 660 663 666 669 669 670 673 , 673 : 675 676 678PLINQ680 20. , , 681 681 682 unsafe684 fixed685 686 686 688 688 690 691 692 693 695 695 697 ??698 , 699 700 701 703 707 708 708 lock708 readonly709 const volatile710 using711 extern712 II. C#717 21. System719 System720 Math721 .NET, 727 728 730 Decimal735 Char741 Boolean748 Array750 763 766 767 768 Action769 BitConverter771 Random773 GC774 object776 Tuple777 IComparable IComparable778 IEquatable778 IConvertible779 ICloneable779 IFormatProvider IFormattable781 IObservable IObserver781 22. 783 C#783 String784 String784, String785 String786 808, 810 811 Substring()811 String812812 812 814 815 String.Format() ToString() 816 String.Format() 816 ToStringQ 819 820- 820 824 827 ^829 830 23. . : 833 834 Thread835 836 838 839 841 844 IsBackground846 847849 853 Monitor 855 Wait(),Pulse() PulseAll()855 Wait() Pulse()856 860 MethodlmplAttribute860 862863867 870 Interlocked873 , .NET Framework 4.0874 875 Abort()876 Abort()878 880 880 880 , .NET Framework 4.0882 882 882 24. . : TPL885 886 Task887 887 890 891 Dispose()895 TaskFactory 895 - 896 897 899 AggregateException901 905 Parallel906 Invoke()906 For()909 ForEach()915 PLINQ917 ParallelEnumerable917 AsParallel()918 AsOrdered()919 920 PLINQ922 PLINQ922 25. , 923. 924 925 926 DictionaryEntry931 931 BitArray950 953 954 954 KeyValuePair960 960 983 , , 988 IComparable990 IComparable 990 IComparable 992 IComparer994 IComparer994 IComparer996 StringComparer997 998 999 IDictionaryEnumerator1000 IEnumerable IEnumerator1001 1003 1005 yield1006 1006 1008 1009 26. 1011 System.Net1012 1013 1014 WebRequest1015 WebResponse1017 HttpWebRequest HttpWebResponse1018 1018 1021, Create()1021, GetResponse()1022, GetResponseStream()1022 1022 Uri1024 , HTTP1025 1026 cookie-1027 LastModified1029 MiniCrawler1030 WebClient1034. 1039 XML-1039 1041 XML1041

#

# 104406 (Herbert Schildt) #, C++, Java. . , Java, C++, ( "" 2007 2008 .). , , , . . . , - www.HerbSchildt.com.0 (Michael Howard) , (TwC) Microsoft, , . (Security Development Lifecycle SDL) , Microsoft. Microsoft 1992 , Windows (Product Support Services) , (Microsoft Consulting Services), . 1997 Windows - Internet Information Services, - Microsoft, 2000 . IEEE Security & Privacy, , , . .

. , , .

#

\

,

, . . , . , , . , , , , . , , : , , . #. C# Microsoft .NET Framework . (Anders Hejlsberg) . C# : C++. , , C++ . , C# Java . , , C# Java , .

, . , , , . Java, C# , C++. , C# , C++, , Java. , . , , . C# . , C# . , C# 1.0 2000 . , C# 2.0, , , . , C# 2.0 . , #. , . , C# 3.0 . , , C# . : LINQ -. LINQ . , #. - - =>, - LINQ-. , C# 4.0, . . , , ; dynamic, C# , , , () ; , #, , . .NET Framework, #, TPL (Task Parallel Library ) PLINQ (Parallel LINQ ). , - . , C# 4.0 . C# - . , . , . . 4.0 #. . I #, , 4.0. , , , - , . II #, .NET Framework. , . II , System. , , , , TPL PLINQ. , , #. . C++ Java, , C# . , #, , . C# 4.0, , Visual Studio 2010 ( )., , , , www .mhprofessional. com. "" , . , . Java . Java ( " ", 2007 .)Java: ( " ", 2008 .)SWING: ( "", 2007 .) Java ( "", 2005 .)Java. ( "", 2008 .) C++ . C++ ( "", 2007 .)C++. ( "", 2005 .)STL Programming From the Ground Up C++C++. ( "", 2009 .) , , ( "", 2007 .) , , . , , . , . . , Web- . , , , , . , , . . :E-mail:info0williamspublishing. comWWW:http://www.williamspublishing.com ::127055, . , . , . 43, . 1

#

#

:03150, , / 152 1 C#

C# I #, , . , #, - . 2 C# 3 , 4 I 5 6 , 7 8 9 10 11 12 , 13 14 - 15 , - 16 , 17 , 18 19 LINQ 20 , ,

C# .NET Microsoft. , . , , XXI .

# I. C#

1. C# # C# , , , . , C# .NET Framework. , C# .NET Framework .

C# . , , . , , , . . C# . C# " ". : C++. : Java. #. , C# . - . (Dennis Ritchie) 1970- - DEC PDP-11 Unix. , Pascal, , , . 1960- . , "" , , . , . , . , , . , , , , . , " ", . 1980- . , , . . , , . , , , "", . C++ 1970- , . - (). , "77 . , , , . - C++. C++ 1979 (Bjarne Stroustrup), - Bell Laboratories, -, . - . " 77, 1983 C++. C++, , , C++. , , . , , . 1980- C++ , , 1990- , , . 1990- - . , C++ . , . , , , . Java Java. Java, Oak (), 1991 Sun Microsystems. 77 77 Java (James Gosling), (Patrick Naughton), (Chris Warth), (Ed Frank) (Mike Sheridan).Java , - , C++. Java , , . , . , , , , , . , , . , Java. ( ) Java , , , , Java , , . 1993 , , , . , , "" . , , , . Java , -. - Java (JVM) Java. , Java , JVM. JVM , . - Java C++, , , , . , C++ , . , C++, , . , . Java . #. , Java C++. , C++. Java C++ , , , C++ Java. , Java , . C++, , .. Java . , Java C++ , . C# Java , . , , , . , , , . , . , , . , Java, Windows. Java Windows, , Java, Java Windows . Windows , Windows Java. Microsoft 1990- C# .NET. - 2000 . C# , . , 1980- Turbo Pascal, . C# , C++ Java. . . , C# , C++ Java. C# , , C++ Java . ( ) , #. . 1.1 #. C# , , . , C# , C++. C++, #.

. 1.1. C#

C# Java . , Java C++ . Java, C# , C# Java. , C# Java , , , . , Java, C# . , Java, , #, Java. C# , , . C# , . , C# , , . - , , . C# 1.0 C# . 1.0 Microsoft 1.1, , . C# 2.0. 2.0 #, , , , , . 2.0 "" #. Microsoft . C# 3.0. C# 2.0 #, , . 3.0 Microsoft , . , , -, (LINQ), . , , , : (LINQ) -. LINQ - C# . C# 4.0, . #, . , . , . . dynamic, , , . , in out. , Office Automation API , , . , , C# 4.0, #. , 4.0 #, , .NET Framework 4.0. (TPL) (PLINQ). , . , , . , , #. TPL PLINQ . C# .NET Framework C# , .NET Framework. . -, C# , .NET Framework. -, C# .NET Framework. , C# .NET Framework , C# .NET Framework. .NET Framework #.0 NET Framework .NET Framework . , , Windows. #, .NET Framework . (Common Language Runtime CLR). , . CLR .NET Framework , . .NET Framework . . , -, - , .NET. , , . , , - , . , .NET, , .NET. C# .NET , C# .NET Framework. CLR CLR .NET. . C# , , , Microsoft Intermediate Language, MSIL ( Microsoft). MSIL , . , MSIL . , , , , MSIL - Java, . CLR . , , MSIL, , CLR. .NET Framework. MSIL ]1-. JIT . . CLR JIT-, MSIL . , C# , , MSIL. , , , , MSIL. MSIL, C# , , , . , MSIL. , C# . , CLR, , . : - MSIL, CLR, .NET, #. , , , , , . , CLR. , .NET Framework Windows . , , , C# , * - .

#!. #

# I. #

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

#!. #

2. C# #

C# , . , . C# . #, #, . , #. , , I.- C# - (). #, C# - . , #. . , , , . , , , . , . , , . , , , FORTRAN COBOL, , - . , , . "" . , . . ( ) , . , , . , . . : (.. , ) (.. , ). , , , . " , ". - . , : " ". - , . , , . - , #, : , . . , , , , . - . . , . , , . , . . , , . , , . , . C# , . , , . C# , . , , , . , , . , , , . , , -, . C# . ( - , .) , , , ., - " ", , . , . , : , . , , . , , , , . . , .. , " ". , : , , . , , , , . , -, . C# . , , . : " ". , . , . (.. ) . . . , . , . , . , "" , , , , . , (, ..), . , (, ..), . ( , ..). , "" , , , . , . , , . . , . #. ./* #. Example.cs.*/using System; class Example {// C# Main(). static void Main() {Console.WriteLine(" #."); C# Visual Studio Microsoft. , , , C# 4.0, Visual Studio 2010 , #., #, Visual Studio, : , -, Visual Studio, -, esc . . . esc. , , Visual Studio, , , , . , . , , . , (Command Prompt) Windows, , . , , Visual Studio. C# - , , . C# .1'. , .2. esc . .3. . , , www. mhprof es-sional. com. . , Notepad. , , , , , , #. , Example. cs. C# esc. , .C:\>csc Example.cs esc Example . , MSIL . MSIL , . . CLR JIT- Example . . , , , Example . ( , MSIL) , .NET Framework , , CLR. esc. , , HacTpoeHHoenoAVisualStudio. Visual Studios Visual ^^ Visual Studio (Visual Studio^Visual Studio Tools^Visual Studio Command Prompt) ^ (Start^AII Programs) Windows. , , vsvars32.bat, Visual Studio. .:\>1 . #. Visual StudioVisual Studio , Microsoft. , , #, . Visual Studio , . , , , , . , C# Visual Studio 2010. , Visual Studio 2010 Professional. Visual Studio .1. () #, =>1^ (File => New ^Project). Windows (Installed Templates) (Empty Project), . , .

, .2. Visual Studio , .

# I. #

#!. #

2. C# # - (Solution Explorer) , (View^Solution Explorer).3. , #. ( Project 1) , (Add) . , .

4. (New Item), (Add New Item). (Code) , (Code File) Example. cs, .

5. , . , .

6. Example. cs, . ( , , www.mhprofessional. com, .) , .

7. , (BuildBuild Solution).8. , (DebugMStart Without Debugging). , .

, . . #. . . . . , , Visual Studio , . , . . , Visual Studio. Example. cs , , #. , . , Java, , C# . Example . cs, , C# . , Sample. cs, Test. cs X. cs. C# . cs, . , , . Example. cs. C# , , . ., ./* #. Example.cs.*/ . , C# . . , , , . , Example. cs. , . C# . . /* */. , , . , . .using System; , System. C# . , . . , , , , . System, , .NET Framework, #. using , . ( , , .) .class Example {

# I. #

#!. #

2. C# # class . , #, a Example . ({) (}). , , . , , C# , , . .// C# Main(). , #. //. , , . ( , #, .) .static void Main() { Main (). , C# . , , . C# Main (). , #. , , . static. , static, . , Main () . void , Main () . , . Main , . Main () , . {, Main (). , , . . , Main ().Console.WriteLine(" #."); " C# . WriteLine (). WriteLine () , . , , . , WriteLine () . Console , - . Console WriteLine () , WriteLine () Console. C# - . , , WriteLine (), , , , using System. , C# . , { }. . . } Main (), Example. , C# . . , main Main writeline WriteLine, . , C# , Main (), . Main, , , Example. . , ( ) , . . , . , - , . , , . , , . C# - , . , , . Main () esc. ( Visual Studio.)EXl.CS(12,21): CS1002: ; 1.CS(13,22): CS1519: '(' , EXl.CS(15,1): CS1022: , , , . . , , , . . , , , . , . , , .using System; . , C# , . , Console.WriteLine(" #."); .System.Console.WriteLine(" #."); , .// "using System;".class Example {// C# Main(). static void Main() {// Console.WriteLine .System.Console.WriteLine(" #."); System , , , C# using System , , . , , , , , . , , , . , - . , . , . .// .using System;'class Example2 {static void Main() {int x; // int ; // = 100; // 100Console.WriteLine(" " + ); = / 2;Console.Write(" / 2: ");Console.WriteLine(); . 100 / 2: 50 . , int ; // . C# . , , . . , .. . C# int. , int. .int ; // , , , , . , : _; , _ . int, C# . 100. = 100; // 100,

# I. #

#!. #

2. C# # C# (=). , , , .Console.WriteLine(" " + ) ; + , . , + WriteLine (). , 2. = / 2; 2, . , 50. . , C# , .+

-

*

/

.Console.Write(" / 2: ");Console.WriteLine(); . -, " / 2 :" Write (). . , . Write () WriteLine (), , . -, , WriteLine () . , Write () nWriteLine (), C# . > . . . , .int , ; // C# , . , . 3. int. int . , . , int 18, 18,3. , int , #. , , C# : float double. . double. double double result; result double. result , , , , 122,23, 0,034 -19,0. . int double ./* int double.*/using System;class Example3 {static void Main() {int ivar; // double dvar; // ivar = 100; // ivar 100dvar = 100.0; // dvar 100.0Console.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); . ivar: 100 dvar: 100 ivar : 33 dvar : 33.3333333333333 , ivar int 3 33, . dvar double 3 . , . , 100 C# , 100,0 . . WriteLineO . , . , , 3,1416 "".// .using System;class Circle {static void Main() { double radius; double area;radius = 10.0;area = radius * radius * 3.1416;Console.WriteLine(" " + area);} . 314.16, , . (.. ) , .. . , #. , , . if C# . if C# , IF . , i f , C++ Java. .if () ; , .. , , : "" "". , . , , . .if (10 < 11) Console.WriteLine("10 11"); , 10 11, WriteLine () . .if(10 < 9) Console.WriteLine (" "); 10 9. , WriteLine () . C# , . .

=

==

i =

, if.// if.using System;class IfDemo {static void Main() { int a, b, c;a = 2; b = 3;if(a < b) Console.WriteLine(" b");// He .if(a == b) Console.WriteLine(" ");

" " .# I. C#

# I. C#

2. C# #Console.WriteLine(); c=a-b; // -1Console.WriteLine(" -I);if( >= 0) Console.WriteLine(" "); if( < 0) Console.WriteLine(" ");Console.WriteLine(); = b - ; // 1 Console.WriteLine(" I);if( >= 0) Console.WriteLine(" "); if( < 0) Console.WriteLine (" ");1 . b -1 1 . int , , ; , , , , . , , , . . C# . for. if, f or C# , C++ Java. .for (; ; ) ; . , . , . , . , . , for.// - for. using System;class ForDemo {static void Main() { int count;for (count = 0; count < 5; count = count+1) Console.WriteLine(" : " + count); Console.WriteLine("!");} . : 0 : 1 : 2 : 3 : 4 ! count . for . , , count < 5. , , WriteLine (). for, count 1. , count 5. , . , ., , #, , , .. .count = count +1; , C# , 1 , . + (++). , .count++; , for .for (count = 0; count < 5; count++) . , , . C# , (). 1. C# , . . , , , . , if for. if.if(w < h) { v = w * h; w = 0;} w h, . , . , ( ) , . ./ , , .// .using System;class BlockDemo {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 j / i 2 if , . , i f, , . i, , . , 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

=

>

-100; x -= 5)Console.WriteLine(x);} for , . , , . .for(count=10; count < 5; count++)x += count; // , count, , 5. , count < 5 , .. . for . for 2 20. , .// , . // , .using System;class FindPrimes { static void Main() { int num; int i; int factor; bool isprime;for(num = 2; num < 20; num++) { isprime = true; factor = 0;// , num . for(i=2; i class Comma {static void Main() { int i, j;int smallest, largest; int num;num = 100;smallest = largest = 1;for(i=2, j=num/2; (i = 2); i++, j){if((smallest == 1) & ((num % i) == 0)) smallest = i;if ( (largest == 1) & ((num % j) == 0)) largest = j;}Console.WriteLine(" : " + largest); Console.WriteLine(" : " + smallest);}} . : 50 : 2 for. , i . 2 , num. j . num , 2. , , i j, . . , for, , bool. . for done.// bool, using System;class forDemo {static void Main() {int i, j;bool done = false;for(i=0, j=100; !done; i++, j){if(i*i >= j) done = true;Console.WriteLine("i, j: " + i + " " + j);}}} .i, j: 0 100 i, j: 1 99 i, j: 2 98 i, j: 3 97 i, j: 4 96 i, j: 5 95 i, j: 6 94 i, j: 7 93 i, j: 8 92 i, j: 9 91 i, j: 10 90

# I. C#

# I. #

5. # for , done (true). done , i j. for , . C# , for. .// for .using System;class Empty {static void Main() { int i;for (i = 0; i < 10; ){Console.WriteLine(" " + i);i++; // for , .. . i, , . , , , i 10, . i , , . 0 1 2 3 4 5 6 7 8 9 for .// for.using System;class Empty2 {static void Main() { int i;i = 0; // for(; i < 10; ){Console.WriteLine(" " + i);i++; // } i , for. , for. , , for. for, , .. , . , C# .for(;;) // , {//. . .} . , , "" . ( " break " .) C# for , . . , 1 5.// .using System;class Empty3 {static void Main() { int i;int sum = 0; // 1 5for(i = 1; i 0){result *= 2; e;}Console.WriteLine ("2 " + i + " " + result);}}} .2 0 1 2 1 2 2 2 4 2 3 8 2 4 16 2 5 32 2 6 64 2 7 128 2 8 256 2 9 512 , while , . , for, while . do-while C# do-while. forn while, , do-while . , do-while . do-while.do {;} while () ; . , do-while while. do-while , . do-while .// .using System;class DoWhileDemo { static void Main() { int num; int nextdigit;num = 198;Console.WriteLine(": " + num);Console.Write(" : "); do {nextdigit = num % 10;Console.Write(nextdigit); num = num / 10;} while(num > 0) ;Console.WriteLine() ;}} .: 198 : 891 . ( num) 10. . num 10. , . , num . f oreach f oreach , . C# , . f oreach 7, . break break , , . break, , , . .// break .using System;class BreakDemo {static void Main() {// break . for(int i=10; i 0) break; // , // i Console .Write (i + " ") ;.}Console .-WriteLine (" ! ") ;} .-10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0 ! , for -10 10, , , break , i . break , #. , do-while.// break do-while, using System;*class BreakDemo2 { static void Main() { int i;i = -10;do {if (i > 0) break;Console.Write(i + " ") ; i++ ;} while (i = numsl.Length) // , // nums2 for(i=0, j=numsl.Length-1; i < numsl.Length; i++, j--) nums2[j] = numsl[i];Console.Write(" : "); for(i=0; i < nums2.Length; i++)Console.Write(nums2[i] + " ");Console.WriteLine();}} . : 0123456789 : 9876543210 Length . - , , . -, for, . , Length, , . Length Length . , . , () , .// Length // .using System;class Jagged {static void Main() {int[][] network_nodes = new int[4][]; network_nodes[0] = new int[3]; network_nodes[1] = new int[7]; network_nodes[2] = new int[2]; network_nodes[3] = new int[5];int i, j;// . for(i=0; i < network_nodes.Length; i++)for(j=0; j < network_nodes[i].Length; j++) network_nodes[i][j] = i * j + 70;Console.WriteLine(" : " + network_nodes.Length + "\n");for(i=0; i < network_nodes.Length; i++){for(j=0; j < network_nodes[i].Length; j++){Console.Write(" " + i +" " + j + ": ");Console.Write(network_nodes[i][j] + "% ");Console.WriteLine ();}Console.WriteLine();}} . :: 4

0070%

0170%

0270%

1070%

1171%

1272%

1373%

1474%

1575%

1676%

20:: 70%

21:: 72%

30:: 70%

31:: 73%

32:: 76%

33:: 79%

34:: 82%

, Length network_nodes. , . , network_nodes.Length , network_nodes ( ). , , .network_nodes[0].Length . 3, C# 3.0 var. , , . , . , . , , LINQ, 19. "" . #. var, [ ]. , - , . . .var vals = new[]{ 1, 2, 3, 4, 5 }; int, . vals. , int , . , [ ]. , , . . , double.var vals = new[,] { {1.1, 2.2}, {3.3, 4.4}, { 5.5, 6.6} }; vals 2x3. . .// .using System;class Jagged {static void Main() {var jagged = new[]{new [ ] { 1, 2, 3, 4 }, new[] { 9, 8, 7 }, new[] { 11, 12, 13, 14, 15 }};for(int j =0; j < jagged.Length; j++){for(int i-0; i < jagged[j].Length; i++)Console.Write(jaggedfj][i] + " ");Console.WriteLine();}} .12 3 49 8 711 12 13 14 15 j agged.var jagged = new[]{new [ ] { 1, 2, 3, 4 }, new[]{ 9, 8, 7 },new [ ] { 11, 12, 13, 14, 15 }

# I. C#

# !. #

7. #}; , new [ ] . -, . -, , . , . , , . , LINQ- . . f oreach 5, C# f oreach, . . f oreach , . C# , . foreach.foreach ( __ in ) ; __ , foreach. , . , . , var. , . , . , , . foreach . , . , . , . , foreach ., , , foreach . , , , . foreach. , , .// foreach.using System;class ForeachDemo { static void Main() {int sum = 0;int[] nums = new int [10];// nums. for(int i = 0; i < 10; i++) nums[i] = i;// foreach // , foreach(int in nums) {Console.WriteLine(" : " + ); sum += ;Console.WriteLine(" : " + sum); .:0

:1

:2

:3

:4

:5

:6

:7

:8

:9

: 45

, foreach . foreach , , , break. , nums.// break foreach.using System;class ForeachDemo { static void Main() { int sum = 0;int[] nums = new int [10];// nums. for(int i = 0; i < 10; i++) nums[i] = i;// foreach // .foreach(int in nums) {Console.WriteLine(" : " + ); sum += ;if( == 4) break; // , 4}Console.WriteLine(" 5 : " + sum); . : : 1 : 2 : 3 : 4 5 : 10 , foreach . foreach . , .// foreach .using System;class ForeachDemo2 { static void Main() { int sum = 0;int[,] nums = new int*[3,5];// nums. for (int i = 0; i < 3; i++) for (int j=0; j < 5; j++) nums[i,j] = (i+l)*(j+l);// foreach // , foreach(int in nums) {Console.WriteLine(" : " + ); sum += ;}Console.WriteLine(" : " + sum); . : 1 : 2 : 3 : 4:5

:2

:4

:6

:8

:10

:3

:6

:9

:12

:15

: 90

f reach : , , , . . , , . , f oreach . , .// foreach.using System;class Search {static void Main() {int[] nums = new int[10]; int val;bool found = false;// nums. for (int i = 0; i < 10; i++) nums[i] = i;val = 5;// foreach // nums. foreach(int in nums) { if(x == val){found = true; break;if(found)Console.WriteLine(" !"); .

# !. #

# I. C#

7. # ! foreach , . foreach , , .. , foreach . string #. . . C# . , string . string C# , , . string , 2, , . . , :Console.WriteLine(" C# ."); " C# #. , string . , . . , str .string str = " C# ."; str " C# .". string char. :char[] charray = {'t',' ','s',' t'};string str = new string(charray); string , , , . , string WriteLine ().// .using System;class StringDemo { static void Main() {char[] charray ={'','1,'o','','',' t',1 p','o','','a',1 1} ;string strl = new string(charray); string str2 = " .";Console.WriteLine(strl);Console.WriteLine(str2);}} . . . string . . 7.1. , StringComparison. , , . ( 12, StringComparison .) , . , , . . , , . qpedu. ( .) , . Compare (), Equals (), IndexOf () Last IndexOf (), , , , . . , (.. ) StringComparison . CurrentCulture. , StringComparison . Ordinal, : StringComparison . CurrentCulturelgnoreCase StringComparison . OrdinallgnoreCase. , ( 22). , Compare () . 7.1 static. static 8, , : Compare ()

= string.Compare(strl, str2, ); . , , 22, . ToUpper () ToLower (), . , . 7.1, Culture Inf , , , . , , (.. ). Culturelnf . CurrentCulture. Culturelnf System. Globalization. , , , Culturelnf . CurrentCulture . string Length, . 7.1.

static int Compare(string strA, string strB, StringComparison comparisonType)bool Equals(string value, StringComparison comparisonType)int IndexOf(char value)int IndexOf(string value, StringComparison comparisonType)

# I. #

, . , Compare () :# I. #

7. # , strA strB; , strA strB; , . comparisonType true, , value. comparisonType , value. . -1, , value. -1, . comparisonType , value. . -1, , value. -1, . comparisonType

int LastlndexOf(char value)int LastlndexOf(string value, StringComparison comparisonType)string ToLower(Culturelnfo. CurrentCulture culture)string ToUpper(Culturelnfo. CurrentCulture culture) . culture . - culture , .string str = "";Console.WriteLine(str [0] ) ; "", "". , . , , , . . ==. , == , , . string. == , . ! =. . Equals (), StringComparison . CurrentCulture. , Compare () , . , Equals () . .// , using System;using System.Globalization;

class StrOps {static void Main() {string strl = " .NET #."; string str2 = " .NET #."; string str3 = " C# ."; string strUp, strLow; int result, idx;Console.WriteLine("strl: " + strl);Console.WriteLine(" strl: " + strl.Length);// strl, // .strLow = strl.ToLower(Cirlturelnf.CurrentCulture); strUp = strl.ToUpper(Culturelnfo.CurrentCulture) ;Console.WriteLine(" strl, " +" :\n " + strLow); Console.WriteLine(" strl, " +" :\n " + strUp);Console.WriteLine();// strl .Console.WriteLine(" strl .") ; for (int i=0; i < strl.Length; i++)Console.Write(strl[i]);Console.WriteLine("\n");// , if (strl == str2)Console.WriteLine("strl == str2"); elseConsole.WriteLine("strl != str2"); if (strl == str3)Console.WriteLine("strl == str3"); elseConsole.WriteLine("strl != str3");// .result = string.Compare(str3, strl, StringComparison.CurrentCulture); if(result == 0)Console.WriteLine(" strl str3 "); else if (result < 0)Console.WriteLine(" strl- str3"); elseConsole.WriteLine(" strl str3");Console.WriteLine();// str2. str2 = " ";// .idx = str2.IndexOf("", StringComparison.Ordinal);Console.WriteLine(" : " + idx)idx = str2.LastlndexOf("", StringComparison.Ordinal);Console.WriteLine(" : " + idx) ;}} .strl: .NET #. strl: 41 strl, : .net #. strl, : .net #. strl . .NET #.strl == str2 strl != str3 strl str3 : : 13 , , Compare () .result = string.Compare(strl, str3, StringComparison.CurrentCulture); , Compare () static, , . + (.. ) . , :string strl = "";string str2 = "";string str3 = "";string str4 = strl + str2 + str3; str4 "". : string System. String, .NET Framework, .. . , , string, System. String, . System. String II . , . .// .

. 7.1 7. #

# I. C#

7. #using System;class StringArrays { static void Main() {string[] str = { "", "", "", "." };Console.WriteLine(" : "); for(int i=0; i < str.Length; i++)Console.Write(str[i] + " ");Console.WriteLine("\n");// .str[l] = "";str[3] = " !";Console.WriteLine(" : "); for(int i=0; i < str.Length; i++)Console.Write(str[i] + " ");}} . : . : ! . . , 19 " ".// , using System;class ConvertDigitsToWords { static void Main() { int num; int nextdigit; int numdigits; int[] n = new int[20];string[] digits = { "", "", "","", "", "","", "", "","" };num =1908;Console.WriteLine(": " + num);Console.Write(" : ");nextdigit = 0; numdigits = 0;// .// , do {nextdigit = num % 10; n[numdigits] = nextdigit; numdigits++; num = num / 10;} while(num > 0); numdigits--;// .for( ; numdigits >= 0; numdigits--)Console.Write(digits[n[numdigits]] + " ");Console.WriteLine() ; .: 1908 : digits 0 9. . , int. . , , . , string . , . . , , . , , , . "", ., , , (.. string) , , . string . , , : Substring (). , . , , , , . Substring ():string Substring(int _, int ) _ , . , Substring().// Substring().using System;class SubStr {static void Main() {string orgstr = "B C# .";// string substr = orgstr.Substring(5, 20);Console.WriteLine("orgstr: " + orgstr);Console.WriteLine("substr: " + substr); .orgstr: C# , substr: , orgstr , substr. : , , #, . # StringBuilder, System.Text. , . , , C# string, StringBuilder. switch string switch. , switch. , . , , "", "" "".// switch .using System;class StringSwitch { static void Main() {string[] strs = { "", "", "", "", "" };foreach(string s in strs) { switch (s){case "":Console.Write (1);break;case "":Console.Write (2); break; case "":Console.Write (3); break;Console.WriteLine (); .

# !. #

# I. C#

7. #12321

# !. #

8. #

. . , , , Main (), static. . -, . , 6. -, . , . #, , : , . , . . , . . - , . , , , , . . , . , " ", , . C# : public, private, protected internal. public private. protected , , 11. internal , C# , 16. public, , , . private, . , (private) . 6, , . private . . , . .public string errMsg; private double bal;private bool isError(byte status) { // ... public private, .// public private , using System; class MyClass {private int alpha; // , int beta;// . public int gamma; // // , alpha beta .// .public void SetAlpha(int ) { alpha = ;public int GetAlphaO { return alpha;public void-SetBeta(int a) { beta = a;public int GetBetaO { return beta;}class AccessDemo { static void Main() {MyClass ob = new MyClassO;// alpha beta // , ob.SetAlpha(-99) ; ob.SetBeta(19) ;Console.WriteLine("ob.alpha " + ob.GetAlpha()); Console.WriteLine("ob.beta " + ob.GetBeta());// alpha beta // .// ob.alpha =10; // ! alpha - !// ob.beta =9;// ! beta - !// gamma , // , ob.gamma = 99; , MyClass alpha private, beta private , gamma public. , alpha beta , . , AccessDemo. (public) , SetAlpha () GetAlpha (). , :// ob.alpha =10; // ! alpha - ! - . , alpha MyClass, , MyClass, SetAlpha () GetAlpha (). beta. : , . - . , , . , , . , , , . , , .. , , . , , , . , . , , . , , ., , , . , , , . , . - , , , " ". , . . - , . , , , , . , . : . , . , : , . Stack, . . Stack. , " ". , Stack , .// , using System; class Stack {// , char[] stck; // , int tos;// // Stack , public Stack(int size) {stck = new char[size]; // tos = 0;// , public void Push(char ch) { if(tos==stck.Length) {Console.WriteLine(M - ."); return;stck[tos] = ch; tos++;// , public char Pop(){if(tos==0){Console.WriteLine(" - ."); return (char) 0;tos ;return stck[tos];// true, , public bool IsFullO { return tos==stck.Length;}// true, , public bool IsEmptyO { return tos==0;// , public int Capacity(){return stek.Length;// , , public int GetNum() { return tos;} Stack . .// , char[] stek; // , int tos;// stek ( ). , . Stack. tos . , tosnstek, , " ". stek, . , tos , , tos , Stack, , . stekntos Stack , . Stack.// Stack , public Stack(int size) {stek = new char[size]; // tos = 0; . tos . , tos , . Push () , .// , public void Push(char ch) { if (tos==stck.Length) {Console.WriteLine(" - ."); return;}

# I. C#

# I. #

8. #stek[tos] = ch; tos++;

}, , ch. , , : tos stck. stck , , tos, . , tos stck. Pop (), .// , public char () { if(tos==0){Console.WriteLine (" - ."); return (char) 0;tos ;return stck[tos]; tos. , , . tos , . Push () Pop (), . Stack : IsFull (), IsEmpty (), Capacity () GetNum (). .// true, , public bool IsFull(){return tos==stck.Length;}// true, , public bool IsEmpty(){return tos==0;}// , public int Capacity(){return stck.Length;}// , , public int GetNum(){return tos;

8. #

# !. #

8. IsFull () true, , false. IsEmpty () true, , false. (.. , )

} Capacity (), , , GetNum (). , , , tos. , . Stack .// Stack, using System;// , class Stack {// , char[] stek; // , int tos;// // Stack , public Stack(int size) {stek = new char[size]; // tos = 0;// , public void Push(char ch) { if(tos==stck.Length) {Console.WriteLine (" - ."); return;}stek[tos] = ch; tos++;// , public char Pop(){if(tos==0){Console.WriteLine(" - ."); return (char) 0;tos ;return stck[tos];// true, , public bool IsFullO { return tos==stck.Length;}// true, - , public bool IsEmptyO {return tos==0;}// , public int Capacity(){return stck.Length;// , , public int GetNumO { return tos;}

{Stack(10); Stack(10); Stack(10);class StackDemo { static void Main() Stack stkl = new Stack stk2 = new Stack stk3 = new char ch; int i;// stkl.Console.WriteLine(" A-J stkl."); for(i=0; !stkl.IsFull(); i++) stkl.Push((char)('A' + i)) ;if(stkl.IsFull()) Console.WriteLine(" stkl .");// stkl.Console.Write(" stkl: "); while( !stkl.IsEmpty()){ch = stkl.Pop();Console.Write(ch);Console.WriteLine();if(stkl.IsEmpty()) Console.WriteLine(" stkl .\");// stkl.Console.WriteLine(" A-J stkl."); for(i=0; !stkl.IsFull(); i++) stkl.Push((char)('A' + i));// stkl stk2. // stk2 . Console.WriteLine(" stkl\n" +" stk2."); while( !stkl.IsEmpty()){ch = stkl.Pop(); stk2.Push(ch);

# I. #

# I. #

8. #

}

Console.Write(" stk2: ") ; while( !stk2.IsEmpty() ){ch = stk2.Pop();Console.Write(ch);Console.WriteLine("\n");// 5 .Console.WriteLine(" 5 stk3.M); for(i=0; i < 5; i++)stk3.Push((char) ('A' + i) ) ;Console.WriteLine(" stk3: " + stk3.Capacity()) ; Console.WriteLine(" stk3: " + stk3.GetNum());} . A-J stkl. stkl . stkl: JIHGFEDCBA stkl . A-J stkl. stkl stk2. stk2: ABCDEFGHIJ 5 stk3. stk3: 10 stk3: 5 , , , int double. , , . . .// .using System;class MyClass { int alpha, beta;public MyClass(int i, int j) { alpha = i; beta = j;// true, ob // , , public bool SameAs(MyClass ob) {if ((ob.alpha == alpha) & (ob.beta == beta)) return true; else return false;}// ob. public void Copy(MyClass ob) { alpha = ob.alpha; beta = ob.beta;public void Show() {Console.WriteLine("alpha: {0}, beta: {1}", alpha, beta);class PassOb {static void Main() {MyClass obi = new MyClass(4, 5) ;MyClass ob2 = new MyClass (6, 7);Console.Write("obi: "); obi.Show ();Console.Write("ob2: "); ob2.Show();if(obi.SameAs(ob2))Console.WriteLine("obi ob2 ."); elseConsole.WriteLine("obi ob2 ."); Console.WriteLine() ;// obi ob2. obi.Copy(ob2);Console.Write("obi : "); obi.Show();if(obi.SameAs(ob2) )Console.WriteLine("obi ob2 ."); elseConsole.WriteLine("obi ob2 .");}} .obi: alpha: 4, beta: 5 ob2: alpha: 6, beta: 7obi 2 . : alpha: 6, beta: 7 2 . Same As () () MyClass . Same As () alpha beta , ob. true , . () alpha beta , ob, alpha beta . , , . , . . , . . . . , , , , . . , . , . , , , , . C# , , . , , int double, , , , . .// , using System; class Test {/* , . */ public void NoChange(int i, int j) { i = i + j; j = -j;}} class CallByValue {

8. #

#!. #

8. #

}static void Main() {Test ob = new Test();int a = 15, b = 20;Console.WriteLine(" b : " + a + " " + b) ;ob.NoChange(a, b);Console.WriteLine(" b : " + a + " " + b) ;} . b : 15 20 b : 15 20 , , NoChange (), , . , i j , b i j. , i . . - . , , , , . (, , , .) , , , , , . ., . , . , , , , . , , . .// , using System;class Test {public int a, b;public Test(int i, int j) { a = i; b = j;

/* . ob.a ob.b , , . */ public void Change(Test ob) { ob.a = ob.a + ob.b; ob.b = -ob.b;}}class CallByRef {static void Main() {

Console.WriteLine("ob. ob.b ob.a + " " + : ob.b);" +Test ob = new Test(15, 20);ob.Change(ob);

: " + ob.b);Console.WriteLine("ob. ob.b ob.a + " " +} .ob.a ob.b : 15 20 ob.a ob.b : 35 -20 , Change () , ., . , , , . - , . , . ref out , , int char, . , , , , , . , ref out . , . ref out, , . , : . . , , . Swap (), . , , C# , , , int. ref. , return. . , , , ? , out. ref ref , . , . . Sqr (), . ref.// ref , using System; class RefTest {// . // ref. public void Sqr(ref int i) { i = i * i;}}class RefDemo {static void Main() {RefTest ob = new RefTest();int a = 10;Console.WriteLine(" : " + a); ob.Sqr(ref a); // ref Console.WriteLine(" : " + );} , ref . , , Sqr (). : 10 : 100, ref, , . , Swap () , .// , using System; class ValueSwap {// , public void Swap(ref int a, ref int b) { int t; t = a; a = b; b = t;}}class ValueSwapDemo { static void Main() {ValueSwap ob = new ValueSwap();int x = 10, = 20;Console.WriteLine("x : " + + " " + ); ob.Swap (ref , ref );Console.WriteLine(" : " + + " " + );} . : 10 20 : 20 10 ref . , , . , , , , . , ref . out , . , , , , . , . , ref . , ref, . , C# out. out ref, : . , out, ( ) - . , out , .. , . , . , out . out. GetParts () Decompose. , .// out.using System; class Decompose {/* . */ public int GetParts(double n, out double frac) { int whole;whole = (int) n;frac = n - whole; // frac return whole;// }class UseOut {static void Main() {Decompose ob = new Decompose(); int i; double f;i = ob.GetParts(10.125, out f);Console.WriteLine(" " + i); Console.WriteLine(" " + f); . 10 0.125 Get Parts () . -, return. -, f out. , out, ., out . . out. HasComFactor () . -, ( 1) , true, , false. -,. out , .// out. using System; class Num {/* , v . , * out. */ public bool HasComFactor(int x, int y,out int least, out int greatest) {int i;int max = x < ? x : y;bool first = true;least = 1;greatest = 1;// .for(i=2; i . using System;// , class ThreeD {int , , z; // public ThreeD(){ = = z = 0; }

k; }opl.z) < op2.z))opl.z) > op2.z))public ThreeD(int i, int j, int k) { x = i; = j; z = // .public static bool operator >(ThreeD opl, ThreeD op2){if(Math.Sqrt(opl.x*opl.x+opl.*opl.+opl.z*Math.Sqrt(op2.x*op2.x+op2.*op2.y+op2.z*return true; elsereturn false;}// X, Y, Z. public void Show(){Console.WriteLine(x + ", " + + ", " + z) ;}class ThreeDDemo { static void Main() {ThreeD a = new ThreeD(5, 6, 7);ThreeD b = new ThreeD(10, 10, 10); ThreeD c = new ThreeD(l, 2, 3);ThreeD d = new ThreeD(6, 7, 5);Console.Write(" a: ");a. Show();Console.Write(" b: ") ;b. Show();Console.Write(" : ");c. Show();Console.Write(" d: ");d. Show();Console.WriteLine();

if( > ) Console.WriteLine(" > "); if( < ) Console.WriteLine(" < "); if( > b) Console.WriteLine(" > b "); if ( < b)-Console.WriteLine(" < b ");if( > d) Console.WriteLine(" > d "); else if( < d) Console.WriteLine("a < d ");else Console.WriteLine(" and " +" "); . : 5, 6, 7 : 10, 10, 10 : 1, 2, 3 d: , 7, 5 > < b and : . , , . .==I =

=

: == ! =, Object. EqualsO Object. GetHashCode (). 11. true false true false . true false . if, while, for do-while ?. true false , . .public static bool operator true(_ ){

# I. C#

# I. #

9. #// true false.}public static bool operator false(_ ){// true false.} , bool. , true false ThreeD. : ThreeD , , , . .// true false ThreeD. using System;// , class ThreeD {int , , z; // public ThreeD(){ = = z = 0;}public ThreeD(int i, int j, int 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; // elsereturn false;}// false.public static bool operator false(ThreeD op) { if((op.x == 0) && (op. == 0) && (op.z == 0)) return true; // elsereturn false;// . public static ThreeD operator (ThreeD op){ThreeD result = new ThreeD();// , result.x = op.x - 1; result. = op. - 1; result.z = op.z - 1;return result;}// X, Y, Z. public void Show (){Console.WriteLine( + ", + + "," + z);}class TrueFalseDemo { static void Main() {ThreeD a = new ThreeD(5, 6, 7);ThreeD b = new ThreeD(10, 10, 10);ThreeD c = new ThreeD(0, 0, 0);Console.Write(" a: ") ;a. Show();Console.Write(" b: ");b. Show();Console.Write(" : ");c. Show() ;Console.WriteLine();if(a) Console.WriteLine(" ."); else Console.WriteLine(" .");if(b) Console.WriteLine(" b ."); else Console.WriteLine(" b .");if(c) Console.WriteLine(" ."); else Console.WriteLine(" .");Console.WriteLine();Console.WriteLine(" ThreeD.") ; do {b. Show(); b;} while(b); . : 5, 6, 7 : 10, 10, 10 : 0, 0, 0 b ThreeD.10, 10, 109, 9, 9 8, 8, 8 7, 1, 7 , 6, 5, 5, 5 4, 4, 4 3, 3, 3 2, 2, 2 1, 1, 1 , ThreeD if do-while. , if ThreeD true. , i f . do-while b . , , b , .. . , b , true . , # : &, |, !, & & | |. , , &, [ !. , , & & | |. . . , & | , bool. , , !. , !, & | ThreeD. , ThreeD , . , .// // !,| & ThreeD.using System;// . class ThreeD {int , , z; // 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) M (opl. != 0) M (opl.z != 0)) | ( (op2 .-x != 0) || (op2. != 0) || (op2.z != 0)) ) return true; elsereturn false;// &.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; elsereturn 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 { static void Main() {ThreeD a = new ThreeD(5, 6, 7);ThreeD b = new ThreeD(10, 10, 10); ThreeD c = new ThreeD (0, 0, 0) ;Console.Write(" a: ");a. Show ();Console.Write(" b: ");b. Show();Console.Write(" : ");c. Show();Console.WriteLine();if(!a) Console.WriteLine(" ."); if(!b) Console.WriteLine(" b ."); if(!c) Console.WriteLine(" .");

# I. C#

# !. #

9. #Console.WriteLine();if( & b) Console.WriteLine(" & b ."); else Console.WriteLine(" & b .");if( & ) Console.WriteLine(" & ."); else Console.WriteLine(" & .");if( | b) Console.WriteLine(" | b ."); else Console.WriteLine(" | b .");if( | ) Console.WriteLine(" | ."); else Console.WriteLine(" | .");}} . : 5, 6, 1 : 10, 10, 10 : 0, 0, 0 . & b , & . | b , | . &, | ! bool. , , .. , bool. , C# bool. bool . , , , . & & | | , . -, & |. -, & | , , . -, , . -, true false. , . , & | ThreeD, & & | |./* !,| & ThreeD. && || . */using System;// , class ThreeD {int , , z; // 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)) |( (op2.x != 0) || (op2. != 0) || (op2.z != 0)) ) return new ThreeD(1,1, 1) ;elsereturn new ThreeD(0, 0, 0) ;// & , 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); elsereturn new ThreeD(0, 0, 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; // elsereturn false;}// false.public static bool operator false(ThreeD op) { if((op.x == 0) && (op. == 0) && (op.z == 0)) return true; // elsereturn false;}// X, Y, Z. public void Show(){Console.WriteLine(x + ", " + + ", " + z) ;}class TrueFalseDemo { static void Main() {ThreeD a = new ThreeD(5, 6, 7);ThreeD b = new ThreeD(10, 10, 10);ThreeD c = new ThreeD(0, 0, 0) ;Console.Write(" a: ");a. Show();Console.Write(" b: ");b. Show();Console.Write(" : ");c. Show();Console.WriteLine() ;if (a) Console.WriteLine(" ."); if(b) Console.WriteLine(" b ."); if(c) Console.WriteLine(" .");if(!a) Console.WriteLine(" ."); if(!b) Console.WriteLine(" b ."); if(!c) Console.WriteLine(" .");Console.WriteLine ();Console.WriteLine(" & |") if( & b) Console.WriteLine(" & b ."); else Console.WriteLine(" & b .");if( & ) Console.WriteLine(" & ."); else Console.WriteLine(" & .");if( | b) Console.WriteLine(" | b ."); else Console.WriteLine(" | b .");if( | ) Console.WriteLine(" | ."); else Console.WriteLine(" | .");Console.WriteLine();// . Console.WriteLine(" " +" && "); if( && b) Console.WriteLine(" && b ."); else Console.WriteLine(" && b .");if( && ) Console.WriteLine(" && ."); else Console.WriteLine(" && .");if( b) Console.WriteLine (" || b ."); else- Console.WriteLine(" || b .");if ( | | ) Console.WriteLine(" | | ."); else Console.WriteLine(" || ."); .:5,6, 7

:,,

:0,0, 0

b . & | & b , & , | b , | . && || && b , && , , . , & |. .// | , public static ThreeD operator | (ThreeD opl, ThreeD op2){if (( (opl.x!=0)M(opl.!=0)||(opl.z!=0))|( (op2.x!=0)||(op2.!=0)||(op2.z!=0)))return new ThreeD(1,1, 1) ;elsereturn new ThreeD(0, 0, 0);}// & , 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); elsereturn new ThreeD (0, 0, 0);

# I. C#

# !. #

9. #} , ThreeD. , . , ThreeD, . , . , & b :if( & b) Console.WriteLine(" & b ."); else Console.WriteLine(" & b ."); ThreeD, . true false , ThreeD true bool. true, , if . , ThreeD. . operator true ( | |) operator false ( &&). , (& |) . (& | ) . , & & | |, & | , . .if( ) Console.WriteLine(" || ."); true . , | . :if( a) Console.WriteLine (" || ."); true , . , |. , , , , . true false , . . , & | , , , . , . , . C# , . . #, , , . : . :public static explicit operator _{_ v) {return ;} public static implicit operator _(_ v) {return ;} _ , ; - _ , ; , . , _, . (implicit), , , , . (explicit), , . . ThreeD, . , ThreeD , . , , . .public static implicit operator int(ThreeD opl){return opl.x * opl. * opl.z; , .// , using System;// , class ThreeD {int , , z; // 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.y + op2.y; result.z = opl.z + op2.z;return result;// ThreeD int. public static implicit operator int(ThreeD opl){return opl.x * opl.y * opl.z;// X, Y, Z. public void Show(){Console.WriteLine(x + ", " + + "," + z) ;class ThreeDDemo { static void Main() {ThreeD a = new ThreeD(1, 2, 3);ThreeD b = new ThreeD(10, 10, 10);ThreeD c = new ThreeD(); int i;Console.Write(" a: ") ;a. Show();Console.WriteLine() ;Console.Write(" b: ");b. Show();Console.WriteLine();c = a + b; // b Console.Write(" a + b: ");c. Show ();Console.WriteLine(); i = a; // intConsole.WriteLine(" i = a: " + i) ;Console.WriteLine();i=a*2-b; // intConsole.WriteLine(" a*2-b: " + i}} . : 1, 2, 3 : 10, 10, 10 +: 11, 12, 13 i = : 6 * 2 - : -988 , ThreeD , i = , . 6, , . int , . operator int() = + . . , ThreeD double . . : ; ; . , , , . . , int.// , using System;// , class ThreeD {int , , z; // 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;}// , public static explicit operator int(ThreeD opl){return opl.x * opl. * opl.z;// X, Y, Z. public void Show (){Console.WriteLine( + "," + + "," + z) ;}class ThreeDDemo { static void Main() {ThreeD a = new ThreeD(l, 2, 3) ;ThreeD b = new ThreeD(10, 10, 10);ThreeD c = new ThreeD(); int i;Console.Write(" a: ");a. Show();Console.WriteLine() ;Console.Write(" b: ");b. Show() ;Console.WriteLine() ;c = a + b; // b Console.Write(" a + b: ");c. Show();Console.WriteLine() ;i = (int) a; // int ,// Console.WriteLine(" i = : " + i) ;Console.WriteLine();i = (int)a * 2 - (int)b; // Console.WriteLine(" * 2 - b: " + i);} , int. , , .i = (int) ; // int ,// . , . , int, double. object . . . ( 11.) . ( 12.) , , . , , . , . -, , , , . - , . , . , , C# . , , . , + ThreeD . , , + , /, . : , , . . , , , . , , . , , , , , +=. , . , .&&09

? ?[]11=

=>->aschecked

defaultisnewsizeof

typeofunchecked

# I. #

# I. C#

9. # () , , ., , , +=, , . , , . , += opera tor + (). , operator+ () ThreeD, b 11,12,13.ThreeD = new ThreeD(1, 2, 3) ;ThreeD b = new ThreeD(10, 10, 10);b += a; // b : , [ ] , , . , , ThreeD, . , . , , , . 4- . , , , 4- , , , , ! , - #. . Nybble. int, 0 15. Nybble . Nybble. int Nybble. Nybble int. : (>) (.public static bool operator >(Nybble opl, Nybble op2){if(opl.val > op2.val) return true; else return false;}// = 0 & index < Length) return true; return false;// , class FSDemo {static void Main() {FailSoftArray fs = new FailSoftArray(5);// fs. for(int i=0; i < fs.Length; i++) fs[i] = i;// // int double .Console.WriteLine("fs[1]: " + fs[1]);Console.WriteLine("fs[2]: " + fs[2]);Console.WriteLine("fs[1.1]: " + fs[l.l]);Console.WriteLine("fs[1.6]: " + fs[1.6]);} .fs [1] : 1 fs [2] : 2 fs[1.1 ] : 1 f s [ 1.6 ] : 2 , double . , 1.1 1, 1.6 2.

10. #

# I. C#

10. # , . , , , - .

public int Length; // , . , . , , 2 0 15. , . .// .using System;class PwrOfTwo {/* , 2 0 15. */ public int this[int index] {// 2. get {if((index >= 0)&& (index < 16)) return pwr(index);else return -1;// set .}int pwr(int p) { int result = 1;for(int i=0; i < p; i++) result *= 2;return result;}class UsePwrOfTwo { static void Main() {PwrOfTwo pwr = new PwrOfTwo();Console.Write(" 8 2: "); for(int i=0; i < 8; i++)Console.Write(pwr[i] + " ");Console.WriteLine();Console.Write(" : "); Console.Write(pwr[-1] + " " + pwr[17]);

# I. #

# I. C#

10. #Console.WriteLine();

. 8 2: 1 2 4 8 16 32 64 128 : -1 -1 , PwrOf Two - get, set. , . , PwrOf Two , . , .pwr[0] =11; // , set. . -, , , ref out, . -, static. . . .// .using System;class FailSoftArray2D {int[,] a; // int rows, cols; // public int Length; // public bool ErrFlag; // // , public FailSoftArray2D(int , int ) { rows = ; cols = ; = new int[rows, cols];Length = rows * cols;// FailSoftArray2D. public int this[int indexl, int index2] {// get.get {if(ok(indexl, index2)) { ErrFlag = false; return a[indexl, index2]; } else {ErrFlag = true; return 0;// set. set {if(ok(indexl, index2)){a[indexl, index2] = value; ErrFlag = false;}else ErrFlag = true;// true, // , private bool ok(int indexl, int index2) {if (indexl >= 0 & indexl < rows & index2 >= 0 & index2 < cols) return true;return false;}// , class TwoDIndexerDemo { static void Main() {FailSoftArray2D fs = new FailSoftArray2D(3, 5); int x;// .Console.WriteLine(" ."); for (int i=0; i < 6; i++) fs[i, i]=i*10;for(int i=0; i < 6; i++){x = f s [ i, i ] ;if(x != -1) Console.Write (x + " ") ;}Console.WriteLine ();// .Console.WriteLine("\ ."); for(int i=0; i < 6; i++){f s [ i, i ] = i * 10 ; if(fs.ErrFlag)Console.WriteLine ("fs[" + i + ", " + i + "] ")for(int i=0; i < 6; i++){x = f s [ i, i ] ;if(!fs.ErrFlag) Console.Write(x + " ");elseConsole.WriteLine("fs[" + i + ", " + i + "] "); : .0 10 20 0 0 0 .fs[3, 3] fs[4, 4] fs[5, 5] 0 10 20 fs[3, 3] fs[4, 4] fs[5, 5] . , . , , , , , , , . , , , , . . , get set. . , , get set. get set . : {*get {// }set {// , int, . , . , set value, , ., , , , . , , . ( , .) , Prop, prop. .// , using System; class SimpProp {int prop; // , MyProp public SimpProp(){ prop =0; }/* prop. . */ public int MyProp { get {return prop;}set {if(value >= 0) prop = value;}// , class PropertyDemo { static void Main() {SimpProp ob = new SimpProp();Console.WriteLine(" ob.MyProp: " + ob.MyProp);ob.MyProp = 100; // Console.WriteLine(" ob.MyProp: " + ob.MyProp);// prop .Console.WriteLine(" " +"-10 ob.MyProp");ob.Prop = -10;Console.WriteLine(" ob.MyProp: " + ob.MyProp); . ob.MyProp: 0 ob.MyProp: 100 -10 ob.MyProp ob.MyProp: 100 . prop , prop. , , . , prop , , . public, , . , prop, . get prop, set prop , . , , prop. , , . , , . . , , , get. , , set. . , (Length). FailSoftArray Length. , , . (, , , .) , Length " ", , FailSoftArray.// Length FailSoftArray.using System;class FailSoftArray {int[] a; // int len; // Lengthpublic bool ErrFlag; // // , public FailSoftArray(int size) { a = new int[size]; len = size;// Length , public int Length { get {return len;}}

// FailSoftArray. public int this[int index] {// get. get {if(ok(index)){ErrFlag = false; return a[index];} else {ErrFlag = true; return 0;}}// set. set {if(ok(index)){a[index] = value;ErrFlag = false;}else ErrFlag = true;}}// true, // , private bool ok(int index) {if(index >= 0 & index < Length) return true;return false;// // , class ImprovedFSDemo { static void Main() {FailSoftArray fs =newFailSoftArray(5);int x;// Length, for (int i=0; i < fs.Length; i++) fs[i] = i*10;for (int i=0; i < fs.Length; i++){x = f s [ i ] ;if(x != -1) Console.Write(x + " ");}Console.WriteLine ();// fs.Length = 10; // , !}

Length , 1. -}

get, . , Length , . , .// fs.Length =10; // , ! , , Length , . Length Fail Soft Array . , , ErrFlag, . Fail So f tArray, Error, ErrFlag, .// ErrFlag .using System;class FailSoftArray {int[] a; // int len; // bool ErrFlag; // ,// // , public FailSoftArray(int size) { a = new int[size]; len = size;}// Length , public int Length { get {return len;// Error , public bool Error { get {return ErrFlag;// FailSoftArray. public int this[int index] {// get.get {if(ok(index)){ErrFlag = false; return a[index];} else {ErrFlag = true; return 0;}}// set. set {if(ok(index)){a[index] = value; ErrFlag = false;}else ErrFlag = true;// true, // , private bool ok(int index) {if(index >= 0 & index < Length) return true; return false;}// , class FinalFSDemo { static void Main() {FailSoftArray fs = new FailSoftArray(5);// Error. for(int i=0; i < fs.Length + 1; i++){fs[i] = i * 10; if(fs.Error)Console.WriteLine(" " + i);} Error FailSoftArray. -, ErrFlag , Error, , . -, Error " ". Error , . Main (), , Error.

# I. #

# I. C#

10. # C# 3.0, _ , , . . : { get; set; } , . , get set , . , , . . . , UserCount.public int UserCount { get; set; } , . , , . UserCount . . get set. (.. - ) , private ( " "). , , . , . , , , . , , . , , . 8, . , . , . 8, . , 8 , , Count Str . .// .using System;class MyClass {// , public int Count { get; set; } public string Str { get; set; }class ObjlnitDemo { static void Main() {// MyClass .MyClass obj =new MyClass { Count = 100, Str = "" };Console.WriteLine(obj.Cougt + " " + obj.Str);}} , Count Str . , 8, :100 8, , LINQ-. ., . -, , ref out. -, . , , , , . , get. , . get . set get , , . , public, set get (public). set get , private. , , , , . , . , , . private. Prop, set private.// . using System; class PropAccess {int prop; // , MyProp public PropAccess(){ prop = 0; }/* prop. prop , . */ public int MyProp { get {return prop;private set { // prop = value;// MyProp. public void IncrPropO {MyProp++; // . .}// , class PropAccessDemo { static void Main() {PropAccess ob = new PropAccess();Console.WriteLine(" ob.MyProp: " + ob.MyProp);// ob.MyProp = 100; // ob.IncrProp();Console.WriteLine(" ob.MyProp : " + ob.MyProp); PropAccess set private. , , IncrProp (), PropAccess. ob. Prop PropAccessDemo ., . , , , get set, . , private. Length Fail So ft Array, .public int Length { get; private set; } Length , set private. Length . , , , . Error, .public bool Error { get; private set; } Error , Fail So ft Array. Length Error FailSof tArray 1 ErrFlag, , 1 ErrFlag Length Error FailSof tArray. FailSof tArray Main(), .// ' // Length Error.using System;class FailSoftArray {int[] a; // // , public FailSoftArray(int size) { a = new int [size];Length = size;// Length, public int Length { get; private set; }// Error, public bool Error { get; private set; }// FailSoftArray. public int this[int index] {// aKceccop get. get {if(ok(index) ){Error = false; return a[index];} else {Error "= true; return 0;}}// set.set {if(ok(index)){a[index] = value; Error = false;}else Error = true;// true, // , private bool ok(int index) {if(index >= 0 & index < Length) return true; return false;}// // , class FinalFSDemo { static void Main() {FailSoftArray fs = new FailSoftArray(5);// Error. for(int i=0; i < fs.Length + 1; i++){fs[i] = i * 10; if(fs.Error)Console.WriteLine(" " + i);}} FailSoftArray , , , .

# !. #

10. # . -, : set get, . -, , . , , . ( 12.) , . RangeArray, , . , C# . : 1 , -5 5. RangeArray , . RangeArray, .RangeArray = new RangeArray(-5, 10); // -5 10 for(int i=-5; i csc SeriesDemo.cs ISeries.cs ByTwos.cs Visual Studio #. , ( ) . .2

4

6

8

10

.

2

4

6

8

10

100. 102 104 106 108 110 , , . ByTwos, GetPrevious (), .// ISeries // ByTwos GetPrevious().class ByTwos : ISeries { int start; int val; int prev;public ByTwos(){start = 0; val = 0; prev = -2;public int GetNextO { prev = val; val += 2; return val;}public void Reset(){val = start; prev = start - 2;public void SetStart(int x) { start = x; val = start; prev = val - 2;}// , ISeries.public int GetPrevious(){return prev;}

# I. C#

# I. #

12. , # , GetPrevious (), , ISeries. , . . , . Primes, . , ISeries , ByTwos.// ISeries // , class Primes : ISeries { int start; int val;public Primes(){start = 2; val = 2;public int GetNextO { int i, j; bool isprime;val++;for(i = val; i < 1000000; i++){isprime = true; for(j = 2; j , Console . In Console . Out . , .using System;class Test {static void Main() {Console.WriteLine(" ."); Test > log " ." log. . , "". < >, , #, . -, , , Windows, , . , . Setln (), SetOut () SetError (), Console.static void Setln(TextReader __) static void SetOut(TextWriter __)static void SetError(TextWriter ____) , Setln () . , , TextReader. SetOut () , TextReader. , FileStream, StreamWriter. .// Console.Out.using System; using System.10;class Redirect {static void Main() {StreamWriter log_out = null;try {log_out = new StreamWriter("logfile.txt");// logfile.txt. Console.SetOut(log_out);Console.WriteLine(" .");for(int i=0; i count + 2;// - incr.Console.WriteLine(" - incr: "); int x = -10; while(x n % 2 == 0;// - isEven.Console.WriteLine(" - isEven: "); for (int i=l; i count +2;IsEven isEven = n => n % 2 == 0; incr -, 2 count. Incr, . , incr, count, -. isEven , true, , false. , - IsEven. : , - , , int count -, incr? : . , - . , -. . incr.Incr incr = (int count) => count + 2; , count int. . . ( , , .) - , - , . - , . - , .(low, high, val) => val >= low && val val >= low && val {int = 1;for(int i=l; i {Console.WriteLine(" ."); return s.Replace('};%// .StrMod RemoveSpaces = s => { string temp = ""; int i;Console.WriteLine(" ."); for(i=0; i < s.Length; i++) if(s[i] != '') temp += s[i];return temp;};// .StrMod Reverse = s => {string temp = ""; int i, j;Console.WriteLine(" ."); for(j=0, i=s.Length-1; i >= 0; i, j++) temp += s[i];return temp;lustring str;// - . StrMod strOp = ReplaceSpaces;str = strOp(" .");Console.WriteLine(" : " + str); Console.WriteLine() ;strOp = RemoveSpaces;str = strOp(" .");Console.WriteLine(" : " + str); Console.WriteLine();strOp = Reverse;str = strOp(" .");Console.WriteLine(" : " + str);}} , . . : --. . : . . :. #, , . , , , . : , , . , . . event. :event _ _; _ , , _ . .// , , using System;// , delegate void MyEventHandler();// , , class MyEvent {public event MyEventHandler SomeEvent;// , public void OnSomeEvent(){if (SomeEvent != null)SomeEvent();class EventDemo {// , static void Handler (){Console.WriteLine(" ");}static void Main() {MyEvent evt = new MyEvent ();// Handler() , evt.SomeEvent += Handler;// , evt.OnSomeEvent();} . , , . , .delegate void MyEventHandler(); . . , . MyEvent. SomeEvent .public event MyEventHandler SomeEvent; . event , . , MyEvent OnSomeEvent (), . , , . OnSomeEvent () SomeEvent.if(SomeEvent != null)SomeEvent();

# I. #

# I. #

15. , - # , , SomeEvent . , , OnSomeEvent () .

} , , . Event Demo Handler (). , . Main () MyEvent, a Handler () , .MyEvent evt = new MyEvent ();// Handler() , evt.SomeEvent += Handler; , +=. += -=. Handler () , . , , .// , evt.OnSomeEvent(); OnSomeEvent () , . , , . , . . .// , using System;// , delegate void MyEventHandler() ;// , , class MyEvent {public event MyEventHandler SomeEvent;// , public void OnSomeEvent(){if(SomeEvent != null)SomeEvent();}class X {public void XhandlerO {Console.WriteLine(" X);}class Y {public void YhandlerO {Console.WriteLine(" Y");}class EventDemo2 {static void Handler (){Console.WriteLine(" EventDemo");static void Main() {MyEvent evt = new MyEvent ();X xOb = new X();Y yOb = new Y();// .evt.SomeEvent += Handler;evt.SomeEvent += xOb.Xhandler;evt.SomeEvent += yOb.Yhandler;// , evt.OnSomeEvent() ;Console.WriteLine() ;// .evt.SomeEvent -= xOb.Xhandler;evt.OnSomeEvent() ; . EventDemo X Y EventDemo Y , X Y, , MyEventHandler. . , X Y . , , , . . , . , . , . , , , . , , , . . X, . , X , . X./* , . */using System;// , delegate void MyEventHandler() ;// , , class MyEvent {public event MyEventHandler SomeEvent;// , public void OnSomeEvent(){if(SomeEvent != null)SomeEvent() ;}class X { int id;public X(int x) { id = x; }// , public void Xhandler() {Console.WriteLine(" " + id);}class EventDemo3 { static void Main() {MyEvent evt = new MyEvent();X ol = new X(1);X o2 = new X(2);

# !. #

# I. C#

15. , - #X o3 = new X(3); evt.SomeEvent += ol.Xhandler; evt.SomeEvent += o2.Xhandler; evt.SomeEvent += o3.Xhandler;

}// , evt.OnSomeEvent() ;} . 1 2 3 , , . , , - , ./* , . */using System;// , delegate void MyEventHandler();// , , class MyEvent {public event MyEventHandler SomeEvent;// , public void OnSomeEvent(){if (SomeEvent != null)SomeEvent() ;class X {/* . */ public static void Xhandler() {Console.WriteLine(" .");class EventDemo4 { static void Main() {MyEvent evt = new MyEvent ();evt.SomeEvent += X.Xhandler;// , evt.OnSomeEvent(); . . , X . Xhandler () X, SomeEvent OnSomeEvent(). , , . . . , , , . event, . .event _ _ { add {// .remove {// .} : add remove. add , +=. remove , -=. add remove, . , value. add remove, . , , . , . . , .// // .using System;// .delegate void MyEventHandler();,

15. , - #

# I. C#

15. , - #

}

// , class MyEvent {MyEventHandler[] evnt = new MyEventHandler[3];public event MyEventHandler SomeEvent {// , add { int i;for(i=0; i < 3; i++) if(evnt[i] == null) { evnt[i] = value; break;}if (i == 3) Console.WriteLine(" .");// , remove { int i;for(i=0; i < 3; i++) if(evnt[i] == value) { evnt[i] = null; break;}if (i == 3) Console.WriteLine(" .");// . , public void OnSomeEvent(){for(int i=0; i < 3; i++)if(evnt[i] != null) evnt[i]();// , MyEventHandler. class W {public void Whandler() {Console.WriteLine(" W");class X {public void Xhandler() {Console.WriteLine(" X);class Y {public void Yhandler() {Console.WriteLine(" Y");}class Z {public void Zhandler() {Console.WriteLine(" Z");}}class EventDemo5 { static void Main() {MyEvent evt = new MyEvent();W wOb = new W () ;X xOb = new X();Y yOb = new Y();Z zOb = new Z();// .Console.WriteLine(" . ") ; evt.SomeEvent += wOb.Whandler; evt.SomeEvent += xOb.Xhandler; evt.SomeEvent += yOb.Yhandler;// - , evt.SomeEvent += zOb.Zhandler;Console.WriteLine();// , evt.OnSomeEvent();Console.WriteLine();// .Console.WriteLine(" xOb.Xhandler.") ; evt.SomeEvent -= xOb.Xhandler; evt.OnSomeEvent();Console.WriteLine();// .Console.WriteLine(" " +"xOb.Xhandler ."); evt.SomeEvent -= xOb.Xhandler; evt.OnSomeEvent();Console.WriteLine();// Zhandler.Console.WriteLine(" zOb.Zhandler.") evt.SomeEvent += zOb.Zhandler; evt.OnSomeEvent(); : . . W X Y xOb.Xhandler. W Y xOb.Xhandler . . W Y zOb.Zhandler. W X Y . MyEventHandler. MyEvent. evnt, .MyEventHandler[] evnt = new MyEventHandler[3]; , . evnt (null). SomeEvent. event.public event MyEventHandler SomeEvent {// , add { int i;for(i=0; i < 3; i++) if(evnt[i] == null) { evnt[i] = value; break;}if (i == 3) Console.WriteLine(" .");// , remove { int i;for(i=0; i < 3; i++) if(evnt[i] == value) { evnt[i] = null; break;}if (i == 3) Console.WriteLine(" .");

# I. #

# I. C#

15. , - #

, add, (.. ) evnt , value. , . (, .) evnt , . , remove evnt , value. , (null), , . OnSomeEvent (). evnt . , , . , event . event . , , , , . - . 23. . , . (abstract). . . , (sealed). , , .. . - - , , . , , , . - . , - , . , - .// - , using System;// , delegate void MyEventHandler(int n);// , , class MyEvent {public event MyEventHandler SomeEvent;// , public void OnSomeEvent(int n) { if(SomeEvent != null)SomeEvent(n);}class LambdaEventDemo {static void Main() {MyEvent evt = new MyEvent();// - , evt.SomeEvent += (n) =>Console.WriteLine(" . " + );// , evt.OnSomeEvent(1); evt.OnSomeEvent(2);} . . 1 . 2 , - .evt.SomeEvent += (n) =>Console.WriteLine(" . " + ); - , . -, - . , .// , evt.SomeEvent += delegate(int n) {Console.WriteLine(" . " + n); , , . .NET Framework C# . .NET Framework , Microsoft. , , : . , , EventArgs, , . , .NET- .void (object , EventArgs ) {// ...} , , this. EventArgs , . EventArgs , . , EventArgs , , . EventArgs Empty static, EventArgs . , .NET- .// .NET- , using System;// , EventArgs. class MyEventArgs : EventArgs { public int EventNum;}// .delegate void MyEventHandler(object source, MyEventArgs arg);/ , , class MyEvent {static int count = 0;public event MyEventHandler SomeEvent;// SomeEvent. public void OnSomeEvent(){MyEventArgs arg = new MyEventArgs();if(SomeEvent != null) { arg.EventNum = count++;SomeEvent(this, arg);}class X {public void Handler(object source, MyEventArgs arg) { Console.WriteLine(" " + arg.EventNum +" X."); Console.WriteLine(": " + source);Console.WriteLine();class Y {public void Handler(object source, MyEventArgs arg) { Console.WriteLine(" " + arg.EventNum +" Y."); Console.WriteLine(": " + source);, Console.WriteLine();class EventDemo6 { static void Main() {X obi = new X ();Y ob2 = new Y ();MyEvent evt - new* MyEvent ();// Handler() , evt ..SomeEvent += obi. Handler;' evt.SomeEvent += ob2.Handler;// , evt.OnSomeEvent(); evt.OnSomeEvent(); . 0 X : MyEvent 0 Y : MyEvent 1 X : MyEvent 1 Y : MyEvent MyEventArgs, EventArgs. MyEventArgs : EventNum. MyEventHandler, , .NET Framework. , , , EventArgs . Handler (), X Y, . MyEvent SomeEvent MyEventHandler. On SomeEvent () SomeEvent, this, MyEventArgs. , MyEventHandler .NET. EventHandler EventHandler . , , .NET Framework EventHandler. ( 18.) TEventArgs , EventArgs . , SomeEvent MyEvent .public event EventHandler SomeEvent; , , . EventArgs . .NET Framework EventHandler. , . EventHandler.// EventHandler. using System;// , , class MyEvent {public event EventHandler SomeEvent; // EventHandler// .public void OnSomeEvent(){if(SomeEvent != null)SomeEvent(this, EventArgs.Empty);}}class EventDemo7 {static void Handler(object source, EventArgs arg) { Console.WriteLine(" ");Console.WriteLine(": " + source);static void Main() {MyEvent evt = new MyEvent();// Handler() , evt.SomeEvent += Handler;// , evt.OnSomeEvent() ; EventArgs , - EventArgs . Empty. . : MyEvent , Windows. , , . #, . , LButtonClick. OnLButtonClick (), . Windows, , , , , , . , . , , KeyPress OnKeyPress (). , .NET- -.// , , using System;// , EventArgs // .class KeyEventArgs : EventArgs { public char ch;}// , , class KeyEvent {public event EventHandler KeyPress;// , public void OnKeyPress(char key) {KeyEventArgs k = new KeyEventArgs();if(KeyPress != null) { k.ch = key;KeyPress(this, k) ;// KeyEvent. class KeyEventDemo { static void Main() {KeyEvent kevt = new KeyEvent();ConsoleKeylnfo key; int count = 0;// - , kevt.KeyPress += (sender, ) =>Console.WriteLine(" : " + e.ch);// - .kevt.KeyPress += (sender, ) =>count++; // count Console.WriteLine(" . " +" .");do {key = Console.ReadKey(); kevt.OnKeyPress(key.KeyChar);} while(key.K