748
Автор бестселлеров по программированию: продано более 3 миллионов его книг! Герберт Шилдт написал книгу, которую должен иметь каждый, кто программирует на C#.” -Прашант Шридхаран, управляющий выпуском C# продуктов, Microsoft БЕСПЛАТНЫЙ В INTERNET Osborne M c Graw Hill

Shildt Gerbert - Polny Spravochnik Po C

Embed Size (px)

Citation preview

  • : 3 !

    , , C#.- , C# , Microsoft

    INTER

    NET

    OsborneMcGrawHill

  • C#

  • 32.973.26-018.2.75 57

    681.3.07

    . ..

    ..

    :

    [email protected], http://www.williamspublishing.com , .

    57 C#. : . . . : , 2004. 752 . : . . . .

    ISBN 5-8459-0563- (.)

    C# ,

    .NET, : , , , , , , , , . C#.

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

    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

  • 18 I. C# 21 1. C# 22 2. C# 30 3. , 53 4. 80 5. 102 6. , 126 7. 154 8. 179 9. 224 10. 256 11. 277 12. , 319 13. 349 14. - 375 15. 409 16. , 431 17. , 449 18. , 484 II. C# 501 19. System 502 20. 541 21. 575 22. 610 23. Internet 645 III. C# 669 24. 670 25. Windows- 689 26. 707 IV. 731 . XML 732 . C# 737 740

  • 6

    17 18 I. C# 21 1. C# 22

    C# 23 C, 23 C++ 24 Internet Java 25 C# 26

    C# .NET Framework 27 .NET Framework 27

    CLR 28 28

    29 2. C# 30

    - 31 32 32 33

    33 csc.exe 34

    34 34 35

    Visual Studio IDE 35 , 38

    40 41 42 44 45

    if 45 for 47

    48 50 50

    C# 51 51 C# 52

    3. , 53 54 C# 54 55 57 decimal 58 60

  • 7

    bool 61 62 65

    65 66 66

    68 68 69

    70 72

    73 74

    76 78

    4. 80 81

    82 84

    87 89

    89 90

    , , 90 96 99

    ? 99 101 101

    5. 102 if 103

    if- 104 if-else-if 105

    switch 106 switch 110

    for 110 for 112

    112 114 115

    116 116 for 117

    while 117 do-while 119 foreach 120 break 120 continue 122 return 123 goto 123

    6. , 126 127

    127

  • 8

    128 132 133 134

    Building 134 137 138 140 Building 142 144

    144 146 Building 146

    new 147 new 148

    149 149

    this 151 7. 154

    155 155 157 158

    159 159 160 161

    162 164 Length 165

    Length 167 foreach 168 172

    172 173 175 177 switch- 178

    8. 179 180

    C# 180 public private 182 : 182

    187 189

    ref- out- 191 ref 191 out 193 ref out 195

    197 199

  • 9

    202 203 208

    this 212 Main() 213

    Main() 213 Main() 213

    215 static 218

    223 9. 224

    225 226 228

    C#- 232 236 true false 237 240 240 , 242 246 250 251

    10. 256 257

    257 260 263 264

    266 271

    271 11. 277

    278 281

    283 285

    286 290

    base 291 293 296 297 301

    305 305

    309 sealed 313 object 313

    315 object 317

  • 10

    12. , 319 320

    321 325 327 328 330 331 331

    332 333

    334 .NET Framework 334 : 335 340

    343 345

    347 347 347

    13. 349 System.Exception 350 350

    try- catch- 351 351 353

    354 356 catch- 357 358 try- 358 360

    360 finally 362 363

    365 367 370 checked unchecked 372

    14. - 375 C#- - 376

    376 376

    377 Stream 377 378 378 380

    - 380 380 382

    FileStream - 383

  • 11

    383 FileStream 385 386 FileStream 387

    - 389 StreamWriter 389 Stream Reader 391

    392 394

    BinaryWriter 394 BinaryReader 395 - 396

    400 MemoryStream 402 StringReader StringWriter 404 405

    15. 409 410

    413 System. Delegate 415 416

    416 418 , 419 421 425

    .NET Framework 426 EventHandler 428

    : 429 16. , 431

    432 432

    434 using 436 using 437 438 440 441 441

    #define 442 #if #endif 442 #else #elif 444 #undef 445 #error 446 #warning 446 #line 446 #region #endregion 447

    internal 447 internal 447

    17. , 449 450

    is 450

  • 12

    as 451 typeof 453

    454 : System.Type 454

    455 455

    GetMethods() 458 459 462 466 471

    474 474

    474 475 475

    477 480

    Attribute Usage 480 Conditional 481 Obsolete 482

    18. , 484 485

    486 486 * & 487

    unsafe 487 fixed 488 489 489 491 492

    492 494 494 495

    496 sizeof 496 lock 496 readonly 496 stackalloc 497 using 498 const volatile 499

    II. C# 501 19. System 502

    System 503 Math 504 509

    510 511

  • 13

    Decimal 514 Char 518 Boolean 523

    Array 523 524 526 527

    BitConverter 532 Random 534 GC 536 Object 537 IComparable 537 IConvertible 538 ICloneable 538 IFormatProvider IFormattable 540

    20. 541 C# 542 String 542

    String 543 , String 543 String 544 String 544

    544 547

    549 552

    555 , 556 557 Substring() 558

    558 559 560

    String.Format() ToString() 561

    String.Format() 562 ToString() 564

    565 - 565

    569 571

    573 21. 575

    576 Thread 577

    577 580 581

    , 583 IsBackground 585 586 588

    592

  • 14

    593 Monitor lock 594

    Wait(), Pulse() PulseAll() 594 Wait() Pulse() 595 598

    MethodImplAttribute 599 , 601

    Abort() 603 Abort() 604

    606 606 608 608

    22. 610 611

    612 ICollection 612 IList 613 IDictionary 614 IEnumerable, IEnumerator IDictionaryEnumerator 615 IComparer 615 IHashCodeProvider 616

    DictionaryEntry 616 616

    ArrayList 617 ArrayList- 621 622

    Hashtable 623 SortedList 625 Stack 629 Queue 631

    BitArray 633 636 636

    637 IDictionaryEnumerator 638

    , 639 IComparable 641 IComparer 642

    644 23. Internet 645

    System.Net 646 647 Internet- 647

    WebRequest 648 WebResponse 650 HttpWebRequest HttpWebResponse 650 650

    653 , Create() 653 , GetResponse() 654

  • 15

    , GetResponseStream() 654 654

    URI 656 HTTP- 657

    658 cookie- 659 LastModified 660

    : MiniCrawler 661 WebClient 665

    III. C# 669 24. 670

    671 671

    C#- 672 672 C#- COM- 672

    IComponent 673 Component 673 674

    CipherLib 675 , CipherComp 676

    Dispose() 677 Dispose(bool) 678 683

    using 684 685

    686 688

    25. Windows- 689 Windows- 690 Windows-, 691 Windows 691 Windows- 692

    Form 692 Windows-, 692

    Windows- 694 694 (IDE) 695

    695 696 696 696

    697 699

    700 702 706

    26. 707 708 : 709

  • 16

    710 711 713

    719 720 728 728

    IV. 731 . XML 732

    XML 733 XML- 734 XML- 734

    . C# 737 740

  • (Herbert Schildt)

    , , C++, Java C#. 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.

  • 18

    , - :

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

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

    , , C# , . , C# , (), , . , ; , COM (Component Object Model Microsoft , , ). . Java C# , . , Java, C# . , C# C++ Java, (type checking) (template class). , C# , ( ).

    , . C# . , C# . , .

  • 19

    C# .

    C# , C# . , .

    I, C#. II, C#. III, C#. I C#.

    , , , , - , .

    II C#. .NET Framework. . .NET Framework , II , System. C#. , , . , C#.

    III C#. 24 , 25 Windows- Windows Forms. 26 (recursive descent parser),

    .

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

    ,

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

    - Web-

    , , Web- : www.osborne.com.

  • 20

    C# ,

    . , .

    C# C#: A Beginner's Guide. , C++, : C++: A 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: A Beginner's Guide Java Java 2: Programmer's Reference ,

    , Teach Yourself

    , , .

    , , . ,

    . , Web- . , , , , .

    , , . . :

    E-mail WWW

    [email protected] http://www.williamspublishing.com

    : 115419, , / 783 03150, , / 152

  • I C#

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

  • 1 C#

  • 1. C# 23

    C# . ,

    . , , C# .

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

    C# , , . C# .NET Framework.

    C#

    . ,

    . , , . . C#.

    C# . (C C++) Java. C#. C# .

    , .

    (Dennis Ritchie) 1970- PDP-11 DEC (Digital Equipment Corporation), UNIX. , Pascal, , C .

    C 1960- . , - (spaghetti code) goto. . , . .

  • 24 I. C#

    , C , , , . , ( ), . , , , , , . 1980- .

    C . .

    , . , , , 5 000.

    C++ 1970- ,

    C . , . - (). , , . , C, , . - C C++.

    C++ (Bjarne Stroustrup) Bell Laboratories ( , -), 1979-. C , 1983 C++. C++ . , C , C++. , , - . , C++ - C. C++ , . , - , - .

    1980- C++ 1990- . , . C++ - .

    , C++ . . ( ) , .

  • 1. C# 25

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

    Java , - (bytecode), .. - , Java-. - Java (Java Virtual Machine JVM) . , Java- , JVM. JVM , .

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

  • 26 I. C#

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

    C# Java ,

    Internet, . (cross-language interoperability) , (mixed-language programming). , , . (), , , .

    , Java Windows. Java- Windows ( Java), Java Windows . Windows , Windows Java.

    , Microsoft C#, C# 1990- .NET- Microsoft. - 2000 . C# (Anders Hejlsberg) , . , 1980- Turbo Pascal, .

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

    C# . 1.1. C# . C C# , . , C# , C++. C C++, C# .

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

  • 1. C# 27

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

    . 1.1. C#

    C# , .

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

    C# .NET Framework

    C# ,

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

    .NET Framework .NET Framework

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

    .NET Framework C# . , Common Language Runtime (CLR), , . CLR .NET Framework, , .

    , .NET-, . , -, , - , .NET-

    C

    C++

    Java C#

  • 28 I. C#

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

    CLR

    CLR .NET-. .

    C#- , , , Microsoft (Microsoft Intermediate Language MSIL). MSIL , . , MSIL . MSIL - Java, .

    CLR- . , , MSIL-, , CLR-. .NET Framework .

    , Microsoft, JI-. JIT . just-in-time, ( ). . .NET- CLR- JIT-, MSIL- , . , C#- , , MSIL-. , , , MSIL-.

    MSIL- (metadata). , , . , MSIL-.

    ,

    (managed code). CLR-. , . , -,

  • 1. C# 29

    , MSIL-, CLR-, , -, .NET Framework. , , , .

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

    ,

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

  • 30 I. C#

    2 C#

  • 2. C# 31

    , , , .

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

    -

    C# -

    (). - C#, C#- - . , , .

    , . , , . , ( ) . , . , , . (, FORTRAN COBOL), . , .

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

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

  • 32 I. C#

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

    - -, C#, : , .

    , ()

    , , . - , . . . , , .

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

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

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

    ( polymorphism, )

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

  • 2. C# 33

    . , , (stack), .. , . , , . , , . , . - , . C# , . , , , .

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

    ,

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

    , . , , () . , .

    C#-.

    /* C#-. Example.cs. */

  • 34 I. C#

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

    C#- Visual Studio .NET. , C#-, : csc.exe (Integrated Development Environment IDE). . ( , , .)

    csc.exe , ,

    Visual Studio, C#- , . C#- .

    1. , . 2. . 3. .

    , , Web-

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

    , C#-, csc.exe, . C:\>csc Example.cs

    csc Example.exe, MSIL- . MSIL- , -. Example. Common Language Runtime JIT-. : Example. ( -, MSIL-) , .NET Framework, CLR.

  • 2. C# 35

    csc. , , vcvars32.bat, //Program Files/Microsoft Visual Studio.NET/Vc7/Bin. . C# Microsoft Visual Studio .NET , Visual Studio .NET Command Prompt , Visual Studio .NET Tools , , .

    .

    C:\>Example :

    C#-. Visual Studio IDE

    7 Visual Studio .NET, Visual Studio IDE C#-. , C#- Visual Studio ( 7), . ( Visual Studio, , .)

    1. () C#-, File , New , Project ( , ,).

    2. ( Project Types ( )) Visual C# Projects ( Visual C#), ( . 2.1) Templates () Empty Project ( ).

    . 2.1. New Project ( )

  • 36 I. C#

    3. , Solution Explorer ( ) , , Add , Add New Item ( , ) , 2.2.

    4. Add New Item ( ) Categories () Local Project Items ( ), Templates Code File ( ) , 2.3.

    2.2. Add , Add New Item

    2.3. Add New Item

  • 2. C# 37

    5. Example.cs. (, , , Web- Osborne : www.osborne.com.) , . 2.4.

    . 2.4. Example.cs

    6. Build , Build Solution ( , ).

    7. Debug , Start Without Debugging ( , ).

    , . 2.5.

    . 2.5.

    , , . C#-. . .

    , ( ) , , , , .

  • 38 I. C#

    , , Example.cs

    , C#-. , .

    (, Java), , C#- . Example.cs, , , Sample.cs, Test.cs X.cs.

    C#- .cs, . , . ( , , ) Example.cs. C#- , . , .

    . /* C#-. Example.cs. */

    . C# , . , , , . Example.cs. , .

    C# . , , . /* , (*/). , , . , , .

    . using System;

    , System. C# (namespace) . , , . , , , , . System, , .NET Framework, C#.

  • 2. C# 39

    using , .

    . class,

    . , C# . Example . ({) (}) . , , , . , , C# . , C#- -.

    . // C#- Main().

    , C#. // . , , () .

    . public static void Main() {

    Main(). , C# (method). , . C#- Main(). ( : /C++- main().) , C#. , .

    public (access specifier). , . public, , , , . ( public private, , () .) Main() public-, ( ) ( ).

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

    static Main()

    . ,

  • 40 I. C#

    Main() . void , Main() . , . Main() , . , Main() ( ) , . ((), Main(). , .

    . , Main(). Console.WriteLine(" C#-.");

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

    , , WriteLine(), , using System. C# . , , , .

    (}) Main(), Example.

    . C# . . , Main main WriteLine writeline, . , C#- , Main(), . , Main (main), . , , Example.exe .

    , (

    ). . , (). C#- - , . , , . ,

  • 2. C# 41

    Main(), IDE , 2.6. (

    csc)

    . 2.6. , ,

    , . ,

    , . , , , . , , . .

    using System; , . , . , C# , . , Console.WriteLine(" C#-."); System.Console.WriteLine(" C#-.");

    , // using System. class Example { // C#- Main(). public static void Main() { // Console.WriteLine . System.Console.WriteLine(" C#-. "); } }

  • 42 I. C#

    System , , C#- using System ( , ). , , , .

    ,

    . , . . , , .

    x y. // . using System; class Example2 { public static void Main() { int x; // . int y; // . x = 100; // x 100. Console.WriteLine("x " + x); y = x / 2; Console.Write("y x / 2: "); Console.WriteLine(y); } }

    : x 100 y x / 2: 50

    ? , int x; // . x . C# . , . . x , .. . C# int. , int x; x int.

    y. int y; // .

    , , . .

    , , :

  • 2. C# 43

    _; ,

    _ . int, C# .

    x 100. x = 100; // x 100.

    C# (=). , , , .

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

    , x. , +, WriteLine() .

    y x, 2. y = x / 2;

    x 2, y. , y 50. x . , C# , :

    + - * /

    : Console.Write("y x / 2: "); Console.WriteLine(y);

    . -, "y x/2: " WriteLine(), Write(). B . , . , Write() WriteLine(), , . -, , WriteLine() y , .. . , WriteLine(), Write() C#-,

    , . . , x y : int x, y; // .

  • 44 I. C#

    int. int . , . , int- 18, 18,3. , int , C#. C# , float double, , . ( double .)

    double , : double result;

    result - double. result double, , 122,23, 0,034 -19,0.

    int double, :

    /* int double. */ using System; class Example3 { public static void Main() { int ivar; // int. double dvar; // double. ivar = 100; // 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); } }

    : ivar: 100 dvar: 100

  • 2. C# 45

    ivar : 33 dvar : 33.3333333333333

    , ivar 3 , 33, .. . dvar 3 .

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

    - : 314.16

    , .

    , ,

    . , C#. , , , .

    if if

    . if C# . C, C++ Java, . : if() ;

  • 46 I. C#

    ( ), , . ( ) . : if(10 < 11) Console.WriteLine("10 11");

    10 11, .. , WriteLine() . : if(10 < 9) Console.WriteLine(" .");

    10 9, WriteLine() .

    C# , . :

    < >= == !=

    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(); c = a - b; // c -1. Console.WriteLine("c -1"); if(c >= 0) Console.WriteLine( " c "); if(c < 0) Console.WriteLine(" c "); Console.WriteLine(); c = b - a; // c 1. Console.WriteLine("c 1"); if(c >= 0) Console.WriteLine( " c "); if(c < 0) Console.WriteLine(" c "); } }

  • 2. C# 47

    : b c -1 c c 1 c

    . int a, b, c; , . , , .

    for ,

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

    for(; ; ) ;

    . , . , for , . , , . , for. // for. using System; class ForDemo { public static void Main() { int count; for(count = 0; count < 5; count = count+1) Console.WriteLine(" : " + count); Console.WriteLine("!"); } }

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

  • 48 I. C#

    count. for . ( ) count < 5. , WriteLine(), . , , , .

    , C#-, , , . , , : count = count + 1;

    , C# , . (++). : count++;

    , for : for(count = 0; count < 5; count++)

    , , , .

    C# (--). , .

    , , C#

    . . . , , . , , if- for-. if-: if(w < h) { v = w * h; w = 0; }

    w h, , w < h, . , , . , , , . .

    , . // .

  • 2. C# 49

    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 j / i 2

    if- , . 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=1; i

  • 50 I. C#

    55 3628800

    ( ) , . for-.

    . . , ({ }) . , , .

    C# , ..

    . , ,

    . , ( ).

    C# ; , . , C# x = y; y = y + 1; Console.WriteLine(x + " " + y); . x = y; y = y + 1; Console.WriteLine(x + " " + y);

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

    .

    , , , ,

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

  • 2. C# 51

    C# C# 77 ,

    . 2.1. ( ) C#. , .

    2.1. C#

    abstract event new structas explicit null switch base extern object this bool false operator throw break finally out true byte fixed override try case float params typeof catch for private uint char foreach protected ulong checked goto public unchecked class if readonly unsafe const implicit ref ushort continue in return using decimal int sbyte virtual default interface sealed volatile delegate internal short void do is sizeof while double lock stackalloc else long static enum namespace string

    C# , , , . . . , . , line_count. C# , .. myvar MyVar . .

    Test x 2 MaxLoad up _top my_var sample23

    , . , 12 . , , , .

  • 52 I. C#

    C# , , @. , @fr C#-. , - for, @ . , @-. // @-. using System; class IdTest { static void Main() { int @if; // if . for(@if = 0; @if < 10; @if++) Console.WriteLine("@if " + @if); } }

    , @if . @if 0 @if 1 @if 2 @if 3 @if 4 @if 5 @if 6 @if 7 @if 8 @if 9

    ( ) @- . , @ ( ), .

    C#

    , ,

    C#- WriteLine() Write(). , Console, System, .NET Framework. , C#- .NET Framework, -, , (GUIs). , C# C# ( ) .NET-. , C#-. C#-, . I .NET-, .NET- II.

  • 3 ,

  • 54 I. C#

    C#: , . , . C#

    , . , C# .

    C# , C#

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

    C#

    C# :

    . , . C# 13 , . 3.1. , C# C#-.

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

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

    3.1. C# bool , , / byte 8- char decimal double C

  • 3. , 55

    3.1. float int long sbyte 8- short uint ulong ushort

    C# : char, byte, sbyte, short,

    ushort, int, uint, long ulong. char ( ). . . 3.2.

    3.2. byte 8 0 255 sbyte 8 -128 127 short 16 -32 768 32 767 ushort 16 0 65 535 int 32 -2 147 483 648 2 147 483 647 uint 32 0 4 294 967 295 long 64 -9 223 372 036 854 775 808 9 223 372 036 854 775 807 ulong 64 0 18 446 744 073 709 551 615

    , C# :

    , . . , , , (sign flag). , , , 1, . . , , , . , .

    , , ( ) . , 32 767 short:

    01111111 11111111

  • 56 I. C#

    1, -1 ( ). , ushort ( ), 65 535.

    , int. int , . , , int, . , uint ( ). , , long, ulong. , , , . , long. // . using System; class Inches { public static void Main() { long inches; long miles; miles = 93000000; // 93 000 000 //5280 , 12 inches = miles*5280* 12; Console.WriteLine(" : " + inches + " ."); } }

    : : 5892480000000 .

    , int uint.

    byte sbyte. byte 0 255. byte , , . sbyte. byte for, 1 100. // byte. using System; class Use_byte { public static void Main() { byte x; int sum;

  • 3. , 57

    sum = 0; for(x = 1; x

  • 58 I. C#

    , Sqrt(): Math. , , , Sqrt() Math. ( WriteLine() Console.) , .

    , C#. double. , ( ) 0,1 1,0. // Math.Sin(), // Math.Cos() Math.Tan(). using System; class Trigonometry { public static void Main() { double theta; // . for(theta = 0.1; theta

  • 3. , 59

    128 . . decimal, 28 ( 29). .

    , decimal . // decimal . 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(" : $" + discounted_price); } }

    : : $16,9575

    , decimal- m. , decimal. decimal (, 10) m. ( .)

    decimal. , . /* decimal . */ using System; class FutVal { public static void Main() { decimal amount; decimal rate_of_return; int years, i; amount = 1000.0M; rate_of_return = 0.07M; years = 10;

  • 60 I. C#

    Console.WriteLine(" : $" + amount); Console.WriteLine(" : " + rate_of_return); Console.WriteLine(" " + years + " "); for(i = 0; i < years; i++) amount = amount + (amount * rate_of_return); Console.WriteLine(" $" + amount); } }

    : : $1000 : 0,07 10 $1967,15135728956532249

    ( ) ! , , .

    C# 8- ,

    (, C++), 16-. C# Unicode (), 16- , . (, , , ) , (, ), , . , 16- . , C# char 16- , 065 535. 8- ASCII Unicode 0127. , ASCII- C#-.

    , . , X ch, : char ch; ch = 'X';

    char-, ch, WriteLine() Console.WriteLine(" ch: " + ch);

    char C# , . , char . , . char ch; ch = 10; // , .

    10 , char. . , .

  • 3. , 61

    bool

    bool /, C#

    true false. , bool . C# bool . , 1 true, 0 false.

    bool : // bool. using System; class BoolDemo { public static void Main() { bool b; b = false; Console.WriteLine("b " + b); b = true; Console.WriteLine("b " + b); // bool if-. if(b) Console.WriteLine(" ."); b = false; if(b) Console.WriteLine( " ."); // bool. Console.WriteLine("10 > 9 " + (10 > 9)); } }

    : b False b True . 10 > 9 True

    , ? -, bool- WriteLine() True False. -, bool- if-, .. if- . if(b == true)...

    -, (, 9 True. 10 > 9, , , + , >.

  • 62 I. C#

    WriteLine() , C#. C# , . , - , . , , . , , C#.

    . : Console.WriteLine( " " + 2 + " $" + 3 + " .");

    , . , , . : Console.WriteLine( " 10/3 : " + 10.0/3.0);

    : 10/3 : 3,33333333333333

    , , . , .

    WriteLine(), . WriteLine("_", arg0, arg1, ... , argN);

    WriteLine() , +. _ : . , , . : {_, : }

    _ ( ). , .

    WriteLine() , ( ) , _. , _ , , . . , {0} arg0, {1} arg1 ..

    .

  • 3. , 63

    Console.WriteLine(" {0} {1} .", 28, 29); : 28 29 .

    , {0} 28, {1} 29. , ( 28 29). , +, .

    , . Console.WriteLine( " {0,10} {1,5} .", 28, 29);

    : 28 29 .

    , , . , . , .

    , , , . , . // . using System; class DisplayOptions { public static void Main() { int i; Console.WriteLine("\t\t"); for(i = 1; i < 10; i++) Console.WriteLine("{0}\t{1}\t{2}", i, i*i, i*i*i); } }

    : 1 1 1 2 4 8 3 9 27 4 16 64 5 25 125 6 36 216 7 49 343 8 64 512 9 81 729

    . , . . , WriteLine(). #, .

  • 64 I. C#

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

    -: 10/3 : 3.33

    #.##, WriteLine() . , , , .

    . Console.WriteLine("{0:###,###.##}", 123456.56); : 123,456.56

    , . : decimal balance; balance = 12323.09m; Console.WriteLine(" {0:C}, balance);

    : $12,323.09

    , . /* C . */ 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:C}", discounted_price); } }

    , , : : 16,96.

  • 3. , 65

    C# ,

    . , 100 . . , . .

    C#- . . , . , , % .

    . , 10 -100 . , . 11.123. C# ( ).

    C# , . : ? , , 12, 123987 0.23? , C# , .

    -, , , , int. , , , int, uint, long ulong. -, double.

    , C#, , . long, l L. , 12 int, 12L long. , u U. , 100 int, 100U uint. ul UL (, 987654UL ulong).

    float, f F (, 10.19F).

    decimal, m (, 9. 95).

    int-, uint-, long- ulong- , byte, sbyte, short ushort, , , .

    , ,

    16, . 0 9 F, 10, 11, 12, 13, 14 15. , 10 16. C#, ,

    . 0x ( x). . count = 0xFF; // 255

  • 66 I. C#

    incr = 0x1a; // 26

    , , (, ), . , , C# , . C# (ESC-), . 3.3. , .

    , ch : ch = \t;

    ch : ch = \;

    3.3.

    ESC- \ () \b \f ( ) \n \r \t \v \0 - \ () \" \\

    C# : .

    , . , " " . ( WriteLine()) .

    , . , , . ESC-, \n, \t \". // ESC- . using System;

  • 3. , 67

    class StrDemo { public static void Main() { Console.WriteLine( " \n \n "); Console.WriteLine("\t\t"); Console.WriteLine("\t\t"); // . Console.WriteLine("\"?\", ."); } }

    : "?", .

    , \n , WriteLine() . , , ESC- \n. , ( ESC- \").

    (verbatim) . @, , . . , , ., . ("). , (""). , . // . using System; class Verbatim { public static void Main() { Console.WriteLine(@" , . "); Console.WriteLine(@" : 1 2 3 4 5 6 7 8 "); Console.WriteLine( @" : "" C#."""); } }

    :

  • 68 I. C#

    , : 1 2 3 4 5 6 7 8 : " C#."

    , , . , . . , .

    . . (, 'X') char. , (, X), - .

    2,

    : _;

    , _ . . . , . , bool . , . , int char.

    C# . , , , , . C# .

    . , , , , .

    .

    . . . : _ = ;

  • 3. , 69

    , , , . .

    : int count = 10; // count // 10. char ch = 'X'; // ch X. float f = 1.2F // f // 1.2.

    ( ) . , int a, b = 8, c = 19, d; // b c // .

    , C# , , . , , , , . // . using System; class DynInit { public static void Main() { double s1 = 4.0, s2 = 5.0; // . // hypot. double hypot = Math.Sqrt((s1 * s1) + (s2 * s2)); Console.Write(" " + s1 + " " + s2 + " "); Console.WriteLine("{0:#.###}.", hypot); } }

    : 4 5 6,403.

    : s1, s2 hypot. (s1 s2) , , hypot, . , Math.Sqrt(). , , . Math.Sqrt() ( ) , hypot. , , , , .

  • 70 I. C#

    , ,

    Main(). C# . , . , (scope) . , , , . .

    C# , . ( , ) , , , .

    , , . , .

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

    . , . . , , , . : , , .

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

  • 3. , 71

    , x Main() . if y. , y . , , // y = 100; //! y . . , , y if-. x if-, (.. ) , .

    , . , , . , , , .

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

    , , . , , : // . using System; class VarInitDemo { public static void Main() { int x; for(x = 0; x < 3; x++) { int y = -1; // y // Console.WriteLine(" y : " + y); // // -1. y = 100; Console.WriteLine(" y : " + y); } } }

    : y : -1 y : 100 y : -1 y : 100 y : -1 y : 100

  • 72 I. C#

    , for y 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 . . C#, , , .

    . , , float int. int i; float f; i = 10; f = i; //float- int-.

    , ( ) . , ,

  • 3. , 73

    int- i, float, f. , C# , . , bool int . , - . .

    , ; (.. ),

    . ,

    . , int , byte, int, byte , .

    , , . , , long double , . // // long double. using System; class LtoD { public static void Main() { long L; 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; // !!!

  • 74 I. C#

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

    decimal float ( double), char ( bool). , char bool.

    , . . . : (_)

    _ . , , x/y int, : double x, y; // ... (int) (x / y);

    , , x y double, int. , x / y, . ( ) int x, . , double int .

    , . , long int , long , int, long-. . , 1,23 1. (0,23) .

    , , . // . using System; class CastDemo { public static void Main() { double x, y; byte b; int i; char ch; uint u;

  • 3. , 75

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

  • 76 I. C#

    : x/y: 3 b 255: 255 -- . b 257: 1 -- . s 32000: 32000 -- . s 64000: -1536 -- . u 64000: 64000 -- . u -12: 4294 967284 -- . ch 88: X

    . (x / y) int , .. .

    , b 255, byte 255. b 257 , 257 byte. , int byte .

    s short 32 000 ( u uint) , short- 32 000. , 64 000 short, . , uint short .

    u ( uint) 64 000 ( l long). , 64 000 uint-. u -12, , , uint . , long uint .

    , byte- char , .. , .

    ,

    . , . , short long, . , .

    . .

  • 3. , 77

    decimal, TO , .. decimal ( float double, ).

    double, TO double.

    float, TO float. ulong, TO

    ulong ( sbyte, short, int long, ).

    long, TO long. uint, sbyte, short int,

    long. uint, TO uint. int.

    . - . , float double decimal. ulong . - , .

    -, . , int, . , char-, sbyte-, byte-, ushort- short- int. int- (integer promotion). , int.

    , , . , byte int, - byte. .

    . , , byte-. . byte- int, , int. , byte- byte- int-. . : // ! using System; class PromDemo { public static void Main() { byte b;

  • 78 I. C#

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

    , b * b b . , b * b b int, .. b * b int-, byte- . , , , , , .

    char. , - char- int . char ch1 = 'a', ch2 = 'b'; ch1 = (char) (ch1 + ch2);

    ch1 ch2 int, int- char-.

    (, ). , int (.. sbyte-, byte-, short- ushort-), int. To char. , uint- long.

    , . . , , . 1 10. . , Math.Sqrt() int. // . using System; class CastExpr { public static void Main() { double n; for(n = 1.0; n

  • 3. , 79

    " : {0}", Math.Sqrt(n) - (int) Math.Sqrt(n) ); Console.WriteLine(); } } }

    : 1 1 : 1 : 0 2 1,4142135623731 : 1 : 0,414213562373095 3 1,73205080756888 : 1 : 0,732050807568877 4 2 : 2 : 0 5 2,23606797749979 : 2 : 0,23606797749979 6 2,44948974278318 : 2 : 0,449489742783178 7 2,64575131106459 : 2 : 0,645751311064591 8 2,82842712474619 : 2 : 0,82842712474619 9 3 : 3 : 0 10 3,16227766016938 : 3 : 0,16227766016838

    , , Math.Sqrt(), int, . ( , ):

    Math.Sqrt(n) - (int) Math.Sqrt(n)

    double. int Math.Sqrt().

  • 4

  • 4. 81

    C# ,

    . C# : , , . ?. C# , , .

    C# .

    + - , * / % -- ++

    C#- +, -, * /

    ( , ). .

    , , . , (/) . , 10/3 3. (%). , : . , 10 % 3 1. C# % , . , 10,0 % 3,0 1. ( C/C++ .) . // %. using System; class ModDemo { public static void Main() { int iresult, irem; double dresult, drem; iresult =10/3; irem = 10 % 3; dresult = 10.0 / 3.0; drem = 10.0 % 3.0;

  • 82 I. C#

    Console.WriteLine( " 10/3: " + iresult + " " + irem); Console.WriteLine( " 10.0 / 3.0: " + dresult + " " + drem); } }

    : 10 / 3: 3 1 10.0 / 3.0: 3,33333333333333 1

    , % , 1, , .

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

    , 1, 1 . , x = x + 1; : x++;

    x = x - 1; : x--;

    ,

    . ,

    x = x + 1; ++x; // . : x++; // .

    ,

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

    x = 10; y = ++x;

  • 4. 83

    y 11. , y 10: x = 10; y = x++;

    x 11. , 11 ( y ).

    . , : /* ++. */ using System; class PrePostDemo { public static void Main() { int x, y; int i; x = 1; Console.WriteLine( ", y = x + x++;"); for(i = 0; i < 10; i++) { y = x + x++; // ++ Console.WriteLine(y + " "); } Console.WriteLine(); x = 1; Console.WriteLine( ", y = x + ++x;"); for(i = 0; i < 10; i++) { y = x + ++x; // ++ Console.WriteLine(y + " "); } Console.WriteLine(); } }

    : , y = x + x++; 2 4 6 8 10 12 14

  • 84 I. C#

    16 18 20 , y = x + ++x; 3 5 7 9 11 13 15 17 19 21

    ,

    y = x + x++; x x, y. x. y = x + ++x; -. ( ) x, , , y. , x++ ++x , , . . x + ++x; , . , . , x x.

    (/)

    , . / - , . .

    , . == != > < >=

  • 4. 85

    . & | ^ && || !

    bool. C# (,

    == !=) . , , =, , . , . bool , true false . , C# true > false .

    , bool, bool. &, |, ^ ! , , .

    p q p & q p | q p ^ q !p

    false false false false false true true false false true true false false true false true true true true true true true false false

    ,

    , . ,

    . // // . using System; class RelLogOps { public static void Main(){ int i, j; bool b1, b2; i = 10; j = 11; if(i < j) Console.WriteLine("i < j"); if(i

  • 86 I. C#

    if(i >= j) Console.WriteLine(" ."); if(i > j) Console.WriteLine(" ."); b1 = true; b2 = false; if(b1 & b2) Console.WriteLine(" ."); if(!(b1 & b2)) Console.WriteLine("! (b1 & b2) -- "); if(b1 | b2) Console.WriteLine("b1 | b2 -- "); if(b1 ^ b2) Console.WriteLine("b1 ^ b2 -- "); } }

    : i < j i

  • 4. 87

    Console.WriteLine( "p " + p + ", q " + q); if(!p | q) Console.WriteLine(" " + p + " " + q + " " + true); Console.WriteLine(); } } } }

    : p True, q True True True True p True, q False p False, q True False True True p False, q False False False True

    C# (short-circuit) , . , , , , . , , . , . , .

    &&, || ( & |, ). , , .

    , . , d n. . d / n , , d n. , . // using System; class SCops { public static void Main() { int n, d; n = 10; d = 2; if(d != 0 && (n % d) == 0)

  • 88 I. C#

    Console.WriteLine(d + " " + n); d = 0; // d . // d , // . if(d != 0 && (n % d) == 0) Console.WriteLine(d + " " + n); /* . ( ). */ if(d != 0 & (n % d) == 0) Console.WriteLine(d + " -- " + n); } }

    , if d . , . , d 2, . ( d ) , , . , , .

    , : C# ?. , , , . , : // . using System; class SideEffects { public static void Main() { int i; i = 0; /* i , , . */ if(false & (++i < 100)) Console.WriteLine(" ."); Console.WriteLine( " if : " + i); // : 1 /* i , . */ if(false && (++i < 100)) Console.WriteLine(" ."); Console.WriteLine( " if : " + i); // - 1 !! } }

  • 4. 89

    , if- i if-. if- i , false. . /, , , , .

    . , .

    2.

    . (=). C# , . . = ;

    . , . , , . int x, y, z; x = y = z = 100;// x, y // z 100.

    x, y z 100 . , . , z = 100 100, y, x. ( ) .

    C# ,

    . . : x = x + 10;

    , : x += 10;

    += x x 10. .

    x = x - 100; : x -= 100;

    x , 100.

  • 90 I. C#

    (.. , ). : op = ;

    op , .

    . += -= *= /= %= &= |= ^=

    , .

    . -, . -, ( ). .

    C# ,

    C#. . bool, float double.

    , (), . , , . . 4.1.

    4.1. & | ^ >>

  • 4. 91

    p q p & q p | q p ^ q ~p 0 0 0 0 0 1 1 0 0 1 1 0 0 1 0 1 1 1 1 1 1 1 0 0

    . , 0 0 . :

    1101 0011 1010 1010 & ________ 1000 0010 &

    . ( ) . , 9 0000 1001. 8 (0000 1000 ). // // "" . using System; class MakeEven { public static void Main() { ushort num; ushort i; for(i = 1; i

  • 92 I. C#

    num : 4 num: 6 num : 6 num: 7 num : 6 num: 8 num : 8 num: 9 num : 8 num: 10 num : 10

    0xFFFE, , 1111 1111 1111 1110. , num & 0xFFFE , . , , , ( ).

    . , , . // // , . using System; class IsOdd { public static void Main() { ushort num; num = 10; if((num & 1) == 1) Console.WriteLine(" ."); num = 11; if((num & 1) == 1) Console.WriteLine(num + " -- ."); } }

    : 11 -- .

    if num 1. num (.. ), num & 1 . . if , .

    , &, , byte . .

  • 4. 93

    // , . using System; class ShowBits { public static void Main() { int t; byte val; val = 123; for(t = 128; t > 0; t = t/2) { if((val & t) != 0) Console.Write("1 "); if((val & t) == 0) Console.Write("0 "); } } }

    : 01111011

    for val. , , 1, 0.

    , , . , 1, , . :

    1101 0011 1010 1010 | ________ 1111 1011

    . // // "" . using System; class MakeOdd { public static void Main() { ushort num; ushort i; for(i = 1; i

  • 94 I. C#

    : num: 1 num : 1 num: 2 num : 3 num: 3 num : 3 num: 4 num : 5 num: 5 num : 5 num: num : 7 num: 7 num : 7 num: 8 num : 9 num: 9 num : 9 num: 10 num : 11

    , for, 1, 0000 0001. , 1 , . , , ( ). , , , , .. .

    (XOR) , , .. . :

    0111 1111 1011 1001 ^ ________ 1100 0110 XOR ,

    . XOR X Y, XOR Y, X. ,

    R1 = X ^ Y; R2 = R1 ^ Y; R2 X. ,

    XOR, (Y), (X).

  • 4. 95

    , , . , . : // XOR // . using System; class Encode { public static void Main() { char ch1 = 'H'; char ch2 = 'i'; char ch3 = '!'; int key = 88; Console.WriteLine(" : " + ch1 + ch2 + ch3); // . ch1 = (char) (ch1 ^ key); ch2 = (char) (ch2 ^ key); ch3 = (char) (ch3 ^ key); Console.WriteLine(" : " + ch1 + ch2 + ch3); // . ch1 = (char) (ch1 ^ key); ch2 = (char) (ch2 ^ key); ch3 = (char) (ch3 ^ key); Console.WriteLine(" : " + ch1 + ch2 + ch3); } }

    : : Hi! : >1 : Hi!

    , XOR, , () .

    ( 1) . , ( ), 1001 0110, ~ 0110 1001.

    1 . // . using System; class NotDemo { public static void Main() {

  • 96 I. C#

    sbyte b = -34; int t; for(t = 128; t > 0; t = t/2) { if((b & t) != 0) Console.Write("1 "); if((b & t) == 0) Console.Write("0 "); } Console.WriteLine(); // . b = (sbyte) ~b; for(t = 128; t > 0; t = t/2) { if((b & t) != 0) Console.Write("1 "); if((b & t) == 0) Console.Write("0 "); } } }

    : 11011110 00100001

    C# . C# :

    > . :

    > _.

    , _ , .

    , , , . , , . , . , . : . , , , .

    , . , , .

    , . , , , .. 1, 0. . , . val 1, 128, 1000 0000. , , , .

  • 4. 97

    // >. using System; class ShiftDemo { public static void Main() { int val = 1; int t; int i; for(i = 0; i < 8; i++) { for(t = 128; t > 0; t = t/2) { if((val & t) != 0) Console.Write("1 "); if((val & t) == 0) Console.Write("0 "); } Console.WriteLine(); val = val 0; t = t/2) { if((val & t) != 0) Console.Write("1 "); if((val & t) == 0) Console.Write("0 "); } Console.WriteLine(); val = val >> 1; // . } } }

    : 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1

    2, 2. . . , , ( ) . : // // 2.

  • 98 I. C#

    using System; class MultDiv { public static void Main() { int n; n = 10; Console.WriteLine(" n: " + n); // 2. n = n 1; Console.WriteLine( " n n = n / 2: " + n); // 4. n = n >> 2; Console.WriteLine( " n n = n / 4: " + n); Console.WriteLine(); // n . n = 10; Console.WriteLine(" n: " + n); // 2, 30 . n = n

  • 4. 99

    , int . . , 2. ( , , 3.)

    , . , x (XOR) x 127. x = x ^ 127; x ^= 127;

    ?

    C# ?.

    ? if-then-else. ? , . : 1 ? 2 : 3;

    1 bool. 2 3 . .

    ?- . 1. , 2, ?-. 1 , ?- 3. , absval val. absval = val < 0 ? -val : val; // // val.

    absval val, . val , absval , .

    ?. 100 , . // ? using System; class NoZeroDiv { public static void Main() { int result; int i;

  • 100 I. C#

    for(i = -5; i < 6; i++) { result = i != 0 ? 100 / i : 0; if(i != 0) Console.WriteLine("100 / " + i + " " + result); } } }

    . 100 / -5 -20 100 / -4 -25 100 / -3 -33 100 / -2 -50 100 / -1 -100 100 / 1 100 100 / 2 50 100 / 3 33 100 / 4 25 100 / 5 20

    : result - i != 0 ? 100 / i : 0;

    result 100 i. , i . ( i = 0) result .

    , ?. , , . . , ?, bool, if. , , , ( ). // ?-. using System; class NoZeroDiv2 { public static void Main() { int i; for(i = -5; i < 6; i++) if(i != 0 ? true : false) Console.WriteLine("100 / " + i + " " + 100 / i); } }

    if. i , if- false, , , . ( ) .

  • 4. 101

    C# (

    ). , , : =10/y*(127/x); x = 10 / y * (127/x);

    ( , ) , . . , , ! ( , , ) . , , ? x = y/3-34*temp+127; X = (y/3) - (34*temp) + 127;

    . 4.2 C#- (

    ). , .

    4.2. C#- ( ) [ ] . ++()--() checked new sizeof typeof unchecked ! ~ +() -() ++() --() * / % + - > < >= is == != & ^ | && || ?: = op=

  • 5

  • 5. 103

    C#-. :

    (if, switch), ( for-, while-, do-while- foreach-) (break, continue, goto, return throw). throw, C#- ( 13), .

    if

    if 2,

    . : if() ; else ;

    C#. else . . if- : if() { } else { }

    , , , if-; else- ( ). . , if-, bool.

    , if-else- , . // , // . using System; class PosNeg { public static void Main() { int i; for(i=-5; i

  • 104 I. C#

    : -5: -4: -3: -2: -1: 0: 1: 2: 3: 4: 5:

    , i , if- ( : ); else- ( : ). .

    if- if- ,

    (. ) if-. if- . , else- if-, , else-. : if(i == 10) { if(j < 20) a = b; if(k > 100) c = d; else a = c; // else- // if(k > 100). } else a = d; // else- if(i == 10).

    , else- if(j < 20), ( if- , else-). else- if(k > 100), .

    . . , else-, : . // , , // . using System; class PosNegZero { public static void Main() { int i; for(i=-5; i

  • 5. 105

    Console.Write(" " + i + ": "); if(i < 0) Console.WriteLine(" "); else if(i == 0) Console.WriteLine(" "); else Console.WriteLine(" "); } } }

    : -5: -4: -3: -2: -1: 0: 1: 2: 3: 4: 5: if-else-if

    , if-, if-else-if. : if() ; else if() ; else if() ; . . . else ;

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

    if-else-if- , { ). // , // . using System;

  • 106 I. C#

    class Ladder { public static void Main() { int num; for(num = 2; num < 12; num++) { if((num % 2) == 0) Console.WriteLine(" " + num + " 2."); else if((num % 3) == 0) Console.WriteLine(" " + num + " 3."); else if((num % 5) == 0) Console.WriteLine(" " + num + " 5."); else if((num % 7) ==0) Console.WriteLine(" " + num + " 7."); else Console.WriteLine(num + " 2, 3, 5 7."); } } }

    : 2 2. 3 3. 4 2. 5 5. 6 2. 7 7. 8 2. 9 3. 10 2. 11 2, 3, 5 7.

    , else- , if-.

    switch

    switch. switch . . if-, switch . . . , . switch : switch{) { case 1: break; case 2:

  • 5. 107

    break; case 3: break; default: break; }

    switch (, char, byte, short int) string ( ). , , . switch- ; case- , . case- switch- .

    default- , case- switch-. default . , case- . - , , case- , break.

    switch- . // switch. using System; class SwitchDemo { public static void Main() { int i; for(i=0; i

  • 108 I. C#

    } } }

    : i . i . i . i . i . i . i . i . i . i .

    , , case-, x. . i , default-.

    switch- int. , , switch- , char. case- char- char-. // char // switch-. using System; class SwitchDemo2 { public static void Main() { char ch; for(ch='A'; ch

  • 5. 109

    ch C ch D ch E

    , default- . , . , .

    C# , , case-, , . , , . case- break. ( , goto, , break .) break, case-, switch , switch. default- break.

    C# , C++ Java. case- , case-, .. . C# . -, case-, , case- . -, case- , .

    case- , , , , case . // "" case- "". using System; class EmptyCasesCanFall { public static void Main() { int i; for(i=1; i < 5; i++) switch(i) { case 1: case 2: case 3: Console.WriteLine("i 1, 2 3"); break; case 4: Console.WriteLine("i 4"); break; } } }

  • 110 I. C#

    : i 1, 2 3 i 1, 2 3 i 1, 2 3 i 4

    , i 1, 2 3, WriteLine(), i 4, . case- , case- .

    case- case-, .

    switch switch case-

    switch. switch. , case- switch , . , : switch(ch1) { case 'A': Console.WriteLine( " - switch."); switch(ch2) { case 'A': Console.WriteLine( " - switch."); break; case '': // ... } // switch. break; case '': // ...

    for

    2, for.

    , , . . , for : for(; ; ) ;

    for , : for(; ; ) { }

    , .

  • 5. 111

    , . bool, . , for . , , . , for . for , . , , for.

    for , , . , 100 -100 , 5. // for // . using System; class DecrFor { public static void Main() { int x; for(x = 100; x > -100; x -= 5) Console.WriteLine(x); } }

    , for. , , . : for(count=10; count < 5; count++) x += count; // .

    , count . (count < 5) . .

    for , . , for 2 20. , . /* . , . */ using System; class FindPrimes { public static void Main() { int num; int i; int factor; bool isprime; for(num = 2; num < 20; num++) {

  • 112 I. C#

    isprime = true; factor = 0; // , num i . for(i=2; i

  • 5. 113

    public static void Main() { int i, j; for(i=0, j=10; i < j; i++, j--) Console.WriteLine("i j: " + i + " " + j); } }

    : i j: 0 10 i j: 1 9 i j: 2 8 i j: 3 7 i j: 4 6

    . i j. i , j . . for , .

    for. , ( 100). : . /* for . */ using System; class Comma { public 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); } }

    :

  • 114 I. C#

    : 50 : 2

    for , . i. 2 , ( num). j. , num, , . , i j . .

    , for,

    , bool. , done. // bool. using System; class forDemo { public 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

    for , bool- done true. true , i j .

  • 5. 115

    C# (, ,

    ) . . : // for . using System; class Empty { public 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 { public static void Main() { int i; i = 0; // . for(; i < 10; ) { Console.WriteLine(" " + i); i++; // . } } }

    i for, . for.

  • 116 I. C#

    , , , , .

    for,

    (, ). , , C#- . for(;;) // . { // ... }

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

    C# , for ( - ), . , . . , 1 5. // . using System; class Empty3 { public static void Main() { int i; int sum = 0; // 1 5. for(i =1; i

  • 5. 117

    i. , : sum = sum + i; for

    , for, . . , , 1 5. i for. // for. using System; class ForVar { public static void Main() { int sum = 0; int fact = 1; // 1 5. for(int i = 1; i

  • 118 I. C#

    , while . // . using System; class WhileDemo { public static void Main() { int num; int mag; num = 435679; mag = 0; Console.WriteLine(": " + num); while(num > 0) { mag++; num = num / 10; }; Console.WriteLine(": " + mag); } }

    : : 435679 : 6

    while . num. , mag , num 10. , num . num , , mag .

    for, while, a , . ( ) . // 2. using System; class Power { public static void Main() { int e; int result; for(int i=0; i < 10; i++) { result = 1; e = i; while(e > 0) { result *= 2; e--; } Console.WriteLine("2 " + i + " " + result);

  • 5. 119

    } } }

    : 2 0 1 2 1 2 2 2 3 2 3 4 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. for while,

    , do-while . , do-while . : do { ; } while();

    , , do-while, while. do-while , , .

    do-while , . // . using System; class DoWhileDemo { public static void Main() { int num; int nextdigit; num = 198; Console.WriteLine(": " + num); Console.Write(" : "); do { nextdigit = num % 10; Console.Write(nextdigit);

  • 120 I. C#

    num = num / 10; } while(num > 0); Console.WriteLine(); } }

    : : 198 : 891

    . 10. . num. , . , num .

    foreach

    foreach .

    . C# , . foreach 7, .

    break

    break ,

    , , . break , , . .

    // break . using System; class BreakDemo { public static void Main() { // break . for(int i=-10; i 0) break; // i > 0. Console.Write(i + " "); } Console.WriteLine("!"); } }

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

  • 5. 121

    , , for i 10 10, break , i .

    break C#-, . , , do-while-, : // break // do-while. using System; class BreakDemo2 { public static void Main() { int i; i = -10; do { if(i > 0) break; Console.Write(i + " "); i++; } while(i

  • 122 I. C#

    // break . using System; class BreakNested { public static void Main() { for(int i=0; i

  • 5. 123

    class ContDemo { public static void Main() { // 0 100. for(int i = 0; i

  • 124 I. C#

    goto case- default- switch. , , case- default- . , , goto. goto switch. , - goto switch. , goto switch. // goto switch. using System; class SwitchGoto { public static void Main() { for(int i=1; i < 5; i++) { switch(i) { case 1: Console.WriteLine(" case 1"); goto case 3; case 2: Console.WriteLine(" case 2"); goto case 1; case 3: Console.WriteLine(" case 3"); goto default; default: Console.WriteLine(" default"); break; } Console.WriteLine(); } // goto case 1; // ! // switch. } }

    : case 1 case 3 default case 2 case 1 case 3 default case 3 default default

    , goto case default switch. , case- break. goto case- case-,

  • 5. 125

    (break) . , goto switch-. // goto case 1; // !... . goto switch , , goto . : // goto. using System; class Use_goto { public static void Main() { int i=0, j=0, k=0; for(i=0; i < 10; i++) { for(j=0; j < 10; j++) { for(k=0; k < 10; k++) { Console.WriteLine("i, j, k: " + i + " " + j + " " + k); if(k == 3) goto stop; } } } stop: Console.WriteLine(", ! i, j, k: " + i + ", " + j + ", " + k); } }

    : i, j, k: 0 0 0 i, j, k; 0 0 1 i, j, k: 0 0 2 i, j, k: 0 0 3 , ! i, j, k: 0, 0, 3

    , goto, if break. goto . , , , , goto .

  • 6 ,

  • 6. , 127

    . C#, . , C#.

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

    C#- , . , , . , , .

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

    . , , , .

    , , , ,

    . , , .

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

    class. , , : class _ { // . 1; 2; //... N; // . _ 1() { // }

  • 128 I. C#

    _ 2() { // } _ N() { // } }

    , . (, public), , . 2, . , . , , , , (private). ( ) . public-, , , , . 8.

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

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

    ,

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

    Building. : floors, area occupants. , Building . . ( .) class Building { public int floors; // public int area; // public int occupants; // }

    , Building, . : _;

  • 6. , 129

    , , _ . , , , . Building public, , , , Building.

    class . Building. Building. , class ; . , Building.

    Building, , , : Building house = new Building(); // // Building.

    house Building, .. . .

    , , . , Building floors, area occupants. (.). . : .

    , , . , floors 2, . house.floors = 2;

    , .

    , Building. // , Building. using System; class Building { public int floors; // public int area; // public int occupants; // } // Building. class BuildingDemo { public static void Main() { Building house = new Building(); // // Building. int areaPP; // , . // house. house.occupants = 4; house.area = 2500; house.floors = 2;

  • 130 I. C#

    // , . areaPP = house.area / house.occupants; Console.WriteLine( " :\n " + house.floors + " \n " + house.occupants + " \n " + house.area + " , \n " + areaPP + " "); } }

    : Building BuildingDemo. BuildingDemo Main() Building house, house, . , Building BuildingDemo . , . , BuildingDemo Building, , .. public-. public, Building, BuildingDemo .

    UseBuilding.cs, UseBuilding.exe. Building BuildingDemo . : : 2 4 2500 , 625

    Building BuildingDemo . Building.cs BuildingDemo.cs, . .

    : csc Building.cs BuildingDemo.cs

    Visual Studio IDE, .

    , : , . , . , , . , Building floors, area occupants, () . . // Building. using System;

  • 6. , 131

    class Building { public int floors; // public int area; // public int occupants; // } // Building. class BuildingDemo { public static void Main() { Building house = new Building(); Building office = new Building(); int areaPP; // , . // house. house.occupants = 4; house.area = 2500; house.floors = 2; // office. office.occupants = 25; office.area = 4200; office.floors = 3; // , . areaPP = house.area / house.occupants; Console.WriteLine(" :\n " + house.floors + " \n " + house.occupants + " \n " + house.area + " , \n " + areaPP + " "); Console.WriteLine(); // , // . areaPP = office.area / office.occupants; Console.WriteLine(" :\n " + office.floors + " \n " + office.occupants + " \n " + office.area + " , \n " + areaPP + " "); } }

    : : 2 4 2500 , 625 : 3 25 4200 , 168

  • 132 I. C#

    , ( house) ( office). . 6.1.

    . 6.1.

    Building. Building house = new Building(); . -,

    house Building. , . -, house . new. , house Building.

    new (.. ) . ( ) , new. , C# .

    , , .

    Building house; // . house = new Building(); // // Building.

    house

    Building. house , , . ( ) house null, , . Building, house. house .

    , , , .

    floorsarea

    occupants

    22500

    4

    house

    floorsarea

    occupants

    34200

    25

    office

  • 6. , 133

    , . . , int x; x = 10; x 10, x int, .. . Building house = new Building(); house , .

    ,

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

    Building house1 = new Building(); Building house2 = house1;

    , house1 house2 , . , house1 house2, . house1 house2 house2 , house1, , house1, house2. , house1.area = 2600; , Console.WriteLine(house1.area); Console.WriteLine(house2.area); 2600.

    , house1 house2, , . , house2 , . Building house1 = new Building(); Building house2 = house1; Building house3 = new Building(); Building house2 = house3; // house2 // house3 // . house2 , house3. , house1, .

  • 134 I. C#

    ,

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

    . C#- . , . . , Main() , . , C#.

    . , getval, getval(). .

    : _ (_) { // }

    , , . , , , , , (private) , . public-, , , .

    _ , . , , . , void. , , . , , . _ ( ), . , , . , _ .

    Building , , , ,

    , . , , Main() , , .

    , . , , Building, area occupants, Building. , Building

  • 6. , 135

    . , , , , . , . , Building , , , - , , . Building , . , Building areaPerPerson(), , . // Building. using System; class Building { public int floors; // public int area; // public int occupants; // // , // . public void areaPerPerson() { Console.WriteLine(" " + area / occupants + " "); } } // areaPerPerson(). class BuildingDemo { public static void Main() { Building house = new Building(); Building office = new Building(); // house. house.occupants = 4; house.area = 2500; house.floors = 2; // office. office.occupants = 25; office.area = 4200; office.floors = 3; Console.WriteLine(" :\n " + house.floors + " \n " + house.occupants + " \n " + house.area + " , "); house.areaPerPerson(); Console.WriteLine(); Console.WriteLine(" :\n " +

  • 136 I. C#

    office.floors + " \n " + office.occupants + " \n " + office.area + " , "); office.areaPerPerson(); } }

    , : : 2 4 2500 , 625 : 3 25 4200 , 168

    ,

    areaPerPerson(). : public void areaPerPerson() {

    areaPerPerson(), . public, . areaPerPerson() void, .. . , .

    . areaPerPerson() : Console.WriteLine(" " + area / occupants + " ");

    , , area occupants. Building area occupants, areaPerPerson() , , , .

    areaPerPerson() , .. .

    Main(): house.areaPerPerson();

    areaPerPerson() house. , , . , , , .

    house.areaPerPerson() , , , house. office.areaPerPerson() , , ,

  • 6. , 137

    office. , , areaPerPerson(), , , , .

    . area occupants areaPerPerson() , .. , . : , , , . , . , , , . , . , area occupants areaPerPerson() , , areaPerPerson().

    . , ( areaPerPerson()), return. return: void- ( ), . , .

    void- return: return;

    , . , , : public void myMeth() { int i; for(i=0; i

  • 138 I. C#

    , : void- : return.

    void- ,

    . , . 3, Math.Sqrt() .

    , , -. ( Math.Sqrt()) , , , , . , , , C#-.

    , return: return ;

    , .

    areaPerPerson(). , , areaPerPerson() , . areaPerPerson(), , , ( ). // areaPerPerson(). using System; class Building { public int floors; // public int area; // public int occupants; // // , // . public int areaPerPerson() { return area / occupants; } } // areaPerPerson(). class BuildingDemo { public static void Main() { Building house = new Building(); Building office = new Building(); int areaPP; // , // . // house. house.occupants = 4; house.area = 2500; house.floors = 2;

  • 6. , 139

    // office. office.occupants = 25; office.area = 4200; office.floors = 3; // house , // . areaPP = house.areaPerPerson(); Console.WriteLine(" :\n " + house.floors + " \n " + house.occupants + " \n " + house.area + " , \n " + areaPP + " "); Console.WriteLine(); // office, // . areaPP = office.areaPerPerson(); Console.WriteLine(" :\n " + office.floors + " \n " + office.occupants + " \n " + office.area + " , \n " + areaPP + " "); } }

    . areaPerPerson():

    . , , areaPerPerson(). , areaPP = house.areaPerPerson(); , house, areaPP.

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

    , . , areaPP. areaPerPerson() WriteLine(); Console.WriteLine(" :\n " + house.floors + " \n " +

  • 140 I. C#

    house.occupants + " \n " + house.area + " , \n " + house.areaPerPerson() + " ");

    WriteLine() house.areaPerPerson(), WriteLine(). , house.areaPerPerson() , Building, . , . if(b1.areaPerPerson() > b2.areaPerPerson()) Console.WriteLine( " b1 .");

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

    . ChkNum isPrime, true, , false . , isPrime bool. // . using System; class ChkNum { // true, x - . public bool isPrime(int x) { for(int i=2; i < x/2 + 1; i++) if((x %i) == 0) return false; return true; } } class ParmDemo { public static void Main() { ChkNum ob = new ChkNum(); for(int i=1; i < 10; i++) if(ob.isPrime(i)) Console.WriteLine(i + " ."); else Console.WriteLine(i + " ."); } }

    :

  • 6. , 141

    1 . 2 . 3 . 4 . 5 . 6 . 7 . 8 . 9 .

    isPrime , . . -, , isPrime. . isPrime 1. x 1. 2, , 2 .. , , isPrime, , x.

    . , . , , ChkNum, lcd(), (least common denominator) . // , . using System; class ChkNum { // true, x - . public bool isPrime(int x) { for(int i=2; i < x/2 + 1; i++) if((x %i) == 0) return false; return true; } // . public int lcd(int a, int b) { int max; if(isPrime(a) | isPrime(b)) return 1; max = a < b ? a : b; for(int i=2; i < max/2 + 1; i++) if(((a%i) == 0) & ((b%i) == 0)) return i; return 1; } } class ParmDemo { public static void Main() { ChkNum ob = new ChkNum(); int a, b; for(int i=1; i < 10; i++)

  • 142 I. C#

    if(ob.isPrime (i)) Console.WriteLine(i + " ."); else Console.WriteLine(i + " ."); a = 7; b = 8; Console.WriteLine(" " + a + " " + b + " " + ob.lcd(a, b)); a = 100; b = 8; Console.WriteLine(" " + a + " " + b + " " + ob.lcd(a, b)); a = 100; b = 75; Console.WriteLine(" " +