Герберт Шилдт - c# 4.0. Полное Руководство - 2011
description
Transcript of Герберт Шилдт - c# 4.0. Полное Руководство - 2011
-
# 4.0
-
TheCompleteReference
HERBERT SCHILDT
Learn more. Do more.MHPROFESSIONAL.COM
C# 4.0
-
C# 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 2010
All 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, . 1
ISBN 978-5-8459-1684-6 (.) ISBN 0-07-174116- (.)
"", 2011 by The McGraw-Hill Companies, 2010
mailto:[email protected]://www.williamspublishing.com
-
23
25
I. # 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. LINQ 637
20. , , 681
II. C# 717
21. System 719
22. 783
23. . : 833
24. . : TPL 885
25. , 923
26. 1011
. 1039
1044
-
22 22
23
25 27 27 27, 27 28 28
I. C# 29
1. C# 31 C# 32
32 C++ 33 Java 33 C# 34 C# 36
# .NET Framework 37 NET Framework 37
CLR 38 38
39
2. C# 41- 41
42 43 44 44 csc.exe 45 Visual Studio 46 50
53 54 54
-
8
57 58
58 60 61
63 C# 64 65 .NET Framework 66
3. , 67 67 C# 68 69 71 73 74 75 76 79
80 80 81
83 83 84 85
86 89
90 91
93 95
4. 97 97
98 101
104 106
107 107
, , 108 114
-
9
117 ? 117 119 119
5. 121 if 121
if 122 if-else-if - 124
switch 125 switch 129
for 129 for 131
while 137 do-while 138 foreach 139 break 139 continue 142
return 143 goto 143
6. , 147 147
148 149
153 154 155
Building 156 158 159 162 Building 164 166
166 168 Building 169
new 170 new 170
" " 171 172 this 174
7. 177 177
-
10
178 182
182 183 184
185 187 Length 189
Length 191
192 foreach 194 198
198 199 203 205 switch 206
8. 209 209
210 212 212
218 220
ref out 222 ref 223 out 224 ref out 227
229 231
234 235 241
this 245 246 247
249 250 251
252 Main() 254
Main() 254
-
11
Main() 255 257 static 260
265 266
9. 269 270
270 273
C# 277 281 true false 283 286
286 288
292 297 298
10. 303 303
304 307 310 311
313 318 319, 320
320 324
11. 329 329 333
336 337
339 343
base 344 346 349 351
-
355 359 360
363 sealed 367 object 368
370 object 372
12. , 375 375
377 381 383 385 387 388 388 391 .NET Framework 391 391
395 397
399 399 399
13. 403 System.Exception 404 404
try catch 404 405 407
408 "" 410 catch 411 412 try 413 414
415 finally 416 Exception 418
420
12
-
422 426 checked unchecked 428
14. - 431 - C# 431
432 432
432 Stream 432 434- 434 436
- 436 436 ReadKey() 438 440
FileStream - 441 441 - 444 446 FileStream 448
- 449 StreamWriter 449 StreamReader 451
452 454
BinaryWriter 454 BinaryReader 455 - 457
461 MemoryStream 463 StringReader StringWriter 465 File 467
() 467 Exists() GetLastAccessTime() 468
469
15. , - 473 473
476 477 478 481
13
-
System.Delegate 483 483
483 484
484 485 486
- 488- 488 - 489 - 492
494 496 497 500 504
- 504 .NET Framework 506
EventHandler EventHandler 508 509
16. , 513 513
514 516 using 518 using 520 521 523 524 :: 524
528 #define 529 #if #endif 529 #else #elif 531 #undef 533 #error 533 #warning 534 #line 534 #region #endregion 534 #pragma 534
internal 535
14
-
internal 536
17. , 537 537
is 538 as 539 typeof 540
541 System.Type 541
543 544 548 550 555 560
562 563 566
570 AttributeUsage 570 Conditional 571 Obsolete 572
18. 575 576 576
580 580
583 585
585 586 594 new() 598 599 602 603
, 604 606 607
609 610
610
15
-
16
612 615 620
620 622
623 625 626
626 630 633
635 , 636 636
19. LINQ 637 LINQ 638
639 641 642 643
where 644 orderby 646 select 649 from 653 group 655 into 657 let 659 join 660 663 666 669
669 670 673 , 673
: 675 676 678PLINQ 680
-
17
20. , , 681 681
682 unsafe 684 fixed 685 686 686 688 688 690 691 692 693
695 695 697 ?? 698 , 699
700 701 703 707 708 708
lock 708 readonly 709 const volatile 710 using 711 extern 712
II. C# 717
21. System 719 System 720 Math 721 .NET, 727
728 730 Decimal 735 Char 741 Boolean 748
Array 750 763
-
18
766 767 768 Action 769
BitConverter 771 Random 773 GC 774 object 776 Tuple 777 IComparable IComparable 778 IEquatable 778 IConvertible 779 ICloneable 779 IFormatProvider IFormattable 781 IObservable IObserver 781
22. 783 C# 783 String 784
String 784, String 785 String 786 808, 810 811 Substring() 811 String 812
812 812 814 815
String.Format() ToString() 816
String.Format() 816 ToString() 819
820- 820
824 827
829 830
23. . : 833 834
-
Thread 835 836 838 839
841 844 IsBackground 846 847 849
853 Monitor 855
Wait(),Pulse() PulseAll() 855
Wait() Pulse() 856 860 MethodImplAttribute 860 862
863 867
870 Interlocked 873 , .NET Framework 4.0 874 875
Abort() 876 Abort() 878
880 880 880 , .NET Framework 4.0 882 882 882
24. . : TPL 885 886 Task 887
887 890
891 Dispose() 895
TaskFactory 895 - 896 897
19
-
899 AggregateException 901 905 Parallel 906
Invoke() 906 For() 909 ForEach() 915
PLINQ 917 ParallelEnumerable 917 AsParallel() 918 AsOrdered() 919 920 PLINQ 922 PLINQ 922
25. , 923 924
925 926 DictionaryEntry 931 931
BitArray 950 953 954
954 KeyValuePair 960 960
983 , , 988 IComparable 990
IComparable 990 IComparable 992
IComparer 994 IComparer 994 IComparer 996
StringComparer 997 998
999 IDictionaryEnumerator 1000
IEnumerable IEnumerator 1001 1003
1005 yield 1006
20
-
21
1006 1008
1009
26. 1011 System.Net 1012 1013 1014
WebRequest 1015 WebResponse 1017 HttpWebRequest HttpWebResponse 1018 1018
1021, Create() 1021, GetResponse() 1022, GetResponseStream() 1022
1022 Uri 1024 , HTTP 1025
1026 cookie- 1027 LastModified 1029
MiniCrawler 1030 WebClient 1034
. 1039
XML- 1039 1041 XML 1041
1044
-
(Herbert Schildt)
#, C++, Java. . , Java, C++, ( "" 2007 2008 .). , , , . . . , - www.HerbSchildt.com.
(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, , , . .
http://www.HerbSchildt.com
-
. , , .
-
, , . . , . , , . , , , , . , , : , , . #.
C# Microsoft .NET Framework . (Anders Hejlsberg) . C# : C++. , , C++ . , C# Java .
, , C# Java , .
-
26
, . , , , . 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 ). ,
-
27
. , 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.
-
28
"" ,
. , .
Java .
Java ( " ", 2007 .)Java: ( " ", 2008 .)SWING: ( "", 2007 .) Java ( "", 2005 .)Java. ( "", 2008 .)
C++ .
C++ ( "", 2007 .)C++. ( "", 2005 .)STL Programming From the Ground Up ++++. ( "", 2009 .)
, ,
( "", 2007 .)
, , .
, , . , .
. , Web- . , , , , .
, , . . :E-mail: [email protected]: http://www.williamspublishing.com
:: 127055, . , . , . 43, . 1: 03150, , / 152
http://www.williamspublishing.com
-
I C#
I #, , . , #, - .
1 C# 2 C# 3 , 4 5 6 , 7 8 9 10 11 12 , 13 14 - 15 , - 16 , 17 , 18 19 LINQ 20 , ,
-
C#
.NET Microsoft.
, . , , XXI .
C# , , , . , # .NET Framework. , C# .NET Framework .
1
C#
-
32 I. C#
C# . ,
, . , , , . . C# .
C# " ". : C++. : Java. #. , C# .
- .
(Dennis Ritchie) 1970- - DEC PDP-11 Unix. , Pascal, , , .
1960- . , "" , , . , . , .
, , . , , , , . , " ", . 1980- .
, , . . , , . , , , "", .
-
1. C# 33
C++ 1970- ,
. - (). , "" . , , , . - C++.
C++ 1979 (Bjarne Stroustrup), Bell Laboratories, -, . - . " ", 1983 C++. C++, , , C++. , , . , , .
1980- C++ , , 1990- , , . 1990- - .
, C++ . , . , , , .
Java
Java. Java, Oak (), 1991 Sun Microsystems. " " Java (James Gosling), (Patrick Naughton), (Chris Warth), (Ed Frank) (Mike Sheridan).
Java , - , C++. Java , , . , . , , , , , . ,
-
34 I. C#
, . , 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
, . , , , . , ,
-
1. C# 35
, . , . , , .
, 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++, #.
. 1.1. C#
C
C++
Java C#
-
36 I. 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, . #, . , . ,
-
1. C# 37
. . 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 #.
NET Framework .NET Framework
. , , Windows. #, .NET Framework . (Common Language Runtime CLR). , . CLR .NET Framework , .
-
38 I. C#
.NET Framework . . , -, - , .NET. , , . , , - , . , .NET, , .NET. C# .NET , # .NET Framework.
CLR CLR .NET.
. C# , , , Microsoft Intermediate Language, MSIL ( Microsoft). MSIL , . , MSIL . , , , , MSIL - Java, .
CLR . , , MSIL, , CLR. .NET Framework.
MSIL JIT-. JIT . . CLR JIT-, MSIL . , C# , , MSIL. , , , , MSIL.
MSIL, # , , , . , MSIL.
, C#
. , CLR, , . :
-
1. C# 39
MSIL, CLR, .NET, #. , , , , , .
, CLR. , .NET Framework Windows . , , , # , - .
, CLR ,
, , (Common Language Specification CLS), .NET- . CLS , . CLS (Common Type System CTS), , . , C# CLS, CTS.
-
C#
, . , . C# . #, #, . , #. , , I.
-
C# - (). #, C# - . , #.
.
2
-
42 I. #
, , , . , , , . , . , , . , , , FORTRAN COBOL, , - . , , .
"" . , . . ( ) , . , , .
, . . : (.. , ) (.. , ). , , , . " , ".
- . , : " ". - , . , , .
- , #, : , . .
,
, , , . - . . , . , , .
-
2. C# 43
, . . , , . , , . , .
C# , . , , . C# , . , , , .
, , . , , , . , , -, . C# . ( - , .) , , , .
, - " ", ,
. , . , : , . , , . , , , , .
. , .. , " ". , : , , . , , , , . , -, . C# . , , .
: " ". , . , . (.. ) . . .
-
44 I. 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, -, csc.. .
-
2. C# 45
csc. , ,
Visual Studio, , , , . , . , , . , (Command Prompt) Windows, , .
, , Visual Studio. C# - , , .
C# .
1. , .2. csc..3. .
, ,
www.mhprofessional.com. . , Notepad. , , , , , , #. , Example.cs.
C#
csc., .:\>csc Example.cs
csc Example., MSIL . MSIL , .. CLR JIT- Example.. , , , Example. (
-
46 I. C#
, MSIL) , .NET Framework , , CLR.
csc., , Visual Studio. Visual Studio Visual Studio Visual Studio (Visual StudioVisual Studio ToolsVisual Studio Command Prompt) (StartAII Programs) Windows. , , vsvars32.bat, Visual Studio.
.
:\>ml .
#.
Visual StudioVisual Studio ,
Microsoft. , , #, . Visual Studio , . , , , , .
, C# Visual Studio 2010. , Visual Studio 2010 Professional. Visual Studio .
1. () #, (FileNewProject). Windows (Installed Templates) (Empty Project), .
, .
-
2. C# 47
, .2. Visual Studio ,
.
- (Solution Explorer) , (ViewSolution Explorer).
Project1 - Microsoft Visual Studio
Debug Create
"Project1" (: 1)Project1
...
-
4. (New Item), (Add New Item). (Code) , (Code File) Example.cs, .
3. , #. ( Project1) , (Add) . , .
48 I. C#
-
2. C# 49
5. , . , .
6. Example.cs, . ( , , www.mhprofessional.com, .) , .
-
7. , (BuildBuild Solution).
8. , (DebugStartWithout Debugging). , .
, . . #. . . . .
, , Visual Studio , . , . . , Visual Studio.
Example.cs ,
, #. , .
, Java, , C# . Example.cs, , C# . , Sample.cs, Test.cs X.cs.
50 I. #
-
2. C# 51
C# .cs, . , , . Example.cs. C# , , . .
, .
/* C#. Example.cs.*/
. , C# . . , , , . , Example.cs., .
C# . . /* */. , , . , .
.
using System; , System.
C# . , . . , , , , . System, , .NET Framework, #. using , . ( , , .)
.
class Example { class
. , #, a Example . ({) (}). , , .
-
52 I. #
, , 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# . , {
-
2. C# 53
}. . .
} Main(), Example.
, C# . . , main Main writeline WriteLine, . , C# , Main(), . Main, , , Example. .
,
( ) , . . , . , - , . , , .
, , . C# - , . , , . Main() csc. ( Visual Studio.)
EX1.CS(12, 21) : CS1002: ; 1.CS(13, 22) : CS1519: '(' , EX1.CS(15, 1) : CS1022:
, , , . .
, , , .
-
54 I. C#
. , , , . , .
, , .
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 , , . , , , , , .
, , ,
. , . , . , .
.
-
2. C# 55
// .
using System;
class Example2 {static void Main() {
int x; // int ; //
= 100; // 100
Console.WriteLine(" " + );
= / 2;
Console.Write(" / 2: ");Console.WriteLine(y);
}}
. 100 / 2: 50
. , int ; // . C# . , , . . , .. . C# int. , int.
.int ; //
, , , , .
, :
_;
, _ . int, C# .
100. = 100; // 100
C# (=). , , , .
-
56 I. C#
" " .Console.WriteLine(" " + );
+ , . , + WriteLine().
, 2. = / 2;
2, . , 50. . , C# , .
+ - * /
.
Console.Write(" / 2: ");Console.WriteLine();
. -, " / 2: " Write(). . , . Write() WriteLine(), , . -, , WriteLine() . , Write() WriteLine(), C# .
, . . . , .
int , ; //
C# , . , . 3.
-
2. C# 57
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 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
-
58 I. C#
ivar : 33 dvar : 33.3333333333333
, ivar int 3 33, . dvar double 3 .
, . , 100 # , 100,0 .
. WriteLine() .
, . , , 3,1416 "".
// .
using System;
class Circle {static void Main() {double radius;double area;
radius = 10.0;area = radius * radius * 3.1416;
Console.WriteLine(" " + area);}
} .
314.16,
, .
(.. )
, .. . , #. , , .
if C#
. if C# , IF . ,
-
2. C# 59
if , C++ Java. .
if() ; , .. , ,
: "" "". , . , , . .
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 {static void Main() {int a, b, c;
a = 2;b = 3;
if(a < b) Console.WriteLine(" b");
// He .if(a == b) Console.WriteLine(" ");
Console.WriteLine();
c = a - b; // -1
-
60 I. #
Console.WriteLine(" -1");if( >= 0) Console.WriteLine(" ");if( < 0) Console.WriteLine(" ");
Console.WriteLine();
= b - ; // 1Console.WriteLine(" 1");if( >= 0) Console.WriteLine(" ");if( < 0) Console.WriteLine(" ");
}}
.
b
-1
1
.
int , b, ; , , b , , . , , , .
. C# . for. if, for C# , C++ Java. .
for(; ; ) ;
. , . , . , . , . , for.// for.
using System;
class ForDemo {
-
2. C# 61
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# ,
.
-
62 I. #
. , , , . , 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 , . , if, , . i, , .
, 1 10.
// 1 10.
using System;
-
2. C# 63
class ProdSum {static void Main() {int prod;int sum;int i;
sum = 0;prod = 1;
for(i=1; i
-
64 I. C#
, . , C# .Console.WriteLine(" " +
+ + z +" ");
, . .
, , . C# . , . , . , . ( ) . , .
C# ,
, . C# : . , . . . , . 4.0 # 77 (. 2.1).
2.1. , C#
abstract as base bool breakbyte case catch char checkedclass const continue decimal defaultdelegate do double else enumevent explicit extern false finallyfixed float for foreach gotoif implicit in int interfaceinternal is lock long namespacenew null object operator outoverride params private protected publicreadonly ref return sbyte sealedshort sizeof stackalloc static stringstruct switch this throw truetry typeof uint ulong uncheckedunsafe ushort using virtual volatilevoid while
-
2. C# 65
, C# 4.0 18 , . , , . , . , , , . - , . . 2.2.
2.2. C#
add dynamic from get globalgroup into join let orderbypartial remove select set valuevar where yield
C# , ,
. . . , . , , , line_count. , , , max__value, . C# . , myvar MyVar . .
Test x 2 MaxLoadup _top my_var sample23
, . , 12 . , .
, C# @ . , @for . for, @ . , @.// @.
using System;
class IdTest {static void Main() {int @if; // if
-
66 I. C#
//
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
, @ , . , @ , .
.NET Framework , ,
: WriteLine() Write(). , Console, System, .NET Framework. , C# , .NET Framework, -, , . , , C# C# .NET. , , #. #, , . .NET Framework I , II .
-
#: , . , , , , . , C# , . , C# .
#,
. , , . , . , . , "" , . , , . , , .
,
3
-
68 I. C#
C# 4.0 , dynamic , . dynamic . dynamic 17.
C# C# :
. . , , 3,1416 212. , . , . .
C# 13 , . 3.1. , . ( , .) #, , , . .
. 3.1. C#
bool , : byte 8- char decimal ( )double float int long sbyte 8- short uint ulong ushort
C# . , C# . , int .
-
3. , 69
. , .
, C# : ,
. .
C# : char, byte, sbyte, short, ushort,
int, uint, long ulong. char , , . . .
byte 8 0-255sbyte 8 -128-127short 16 -32 768-32 767ushort 16 0-65 535int 32 -2 147 483 648-2 147 483 647uint 32 0-4 294 967 295long 64 -9 223 372 036 854 775 808-9 223 372 036 854 775 807ulong 64 0-18 446 744 073 709 551 615
, C# : . . , , C# , . , 0, , 1. , , 1.
, . , , 32 767 short .0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1, , -1, . ushort, 1 65 535.
-
70 I. C#
, int. int , . , int, . , , uint, long, ulong. , . long.// .
using System;
class Inches {static void Main() {long inches;long miles;
miles = 93000000; // 93 000 000
// 5 280 , 12 ,inches = miles * 5280 * 12;
Console.WriteLine(" : " +inches + " .");
}}
.
: 5892480000000 ., int
uint. byte sbyte. byte
0 255. byte , , . sbyte. , byte , 1 100.
// byte.
using System;
class Use_byte {static void Main() {byte x;int sum;sum = 0;
-
3. , 71
for(x = 1;
-
72 I. C#
.
1.78412203012729
Sqrt(). , Sqrt() Math, Math . Console WriteLine() . , , , .
, Math #. double. , , 0,1 1,0 .
// .using System;class Trigonometry {static void Main() {Double theta; // for(theta = 0.1; theta
-
3. , 73
, C#
decimal, . 128 1-28 7,9+28. , , , . decimal, 28 ( 29) . , , .
, decimal . .
// decimal .
using System;
class UseDecimal {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. , m , decimal. decimal m, 10. ( .)
decimal. .
/* decimal .
*/
-
74 I. #
using System;
class FutVal {static void Main() {decimal amount;decimal rate_of_return;int years, i;amount = 1000.0M;rate_of_return = 0.07M;years = 10;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.151357289565322490000
, , .. , ! , "" .
C# 8- ,
, C++, 16- , (Unicode). , . , , , , , , , 8- . C# char, 16- 0 65 535. 8- ASCII 0 127. , ASCII - #.
, (.. ) . , ch X.
-
3. , 75
char ch;ch = 'X';
char WriteLine()., ch.Console.WriteLine(" ch : " + ch);
char C# , . , C# . , .
char ch;ch = 88; // ,
, 88 , . . , , .
bool : "" "".
C# true false . , bool . , C# . , 1 true, 0 false.
bool.// bool.
using System;
class BoolDemo {static void Main() {bool b;b = false;
Console.WriteLine("b " + b);b = true;Console.WriteLine("b " + b);
// if.if(b) Console.WriteLine(".");
b = false;if (b) Console.WriteLine("He .");
-
76 I. C#
// // .Console.WriteLine("10 > 9 " + (10 > 9));
}}
.
b Falseb True.10 > 9 True
. -, bool WriteLine() "True" "False". -, bool if. , , if .if(b == true) ...
-, . 10 > 9 "True." , 10 > 9 , + , >.
WriteLine()
, . .NET Framework , . - , . , WriteLine(). . , , , , , .
+, .
Console.WriteLine(" " + 2 +" $" + 3 + " .");
, , . , .
Console.WriteLine(" 10/3 : " + 10.0/3.0); .
10/3 : 3.33333333333333
-
3. , 77
, . , .
WriteLine(), , .
WriteLine(" ", arg0, arg1, ... , argN); WriteLine() , +.
: , , . :
{argnum, width: fmt} argnum , ; width ; fmt . width fmt .
, , argnum. , . width fmt . , . , {0} arg0, {1} arg1 ..
.
Console.WriteLine(" {0} {1} .", 28, 29); .
28 29 , 28 {0}, 29
{1}. , , ( 28 29). , , , +.
, .
Console.WriteLine(" {0,10} {1,5} .", 28, 29); .
28 29 . , . ,
width. , .
, , , . , . .
-
78 I. C#
// .
using System;
class DisplayOptions {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 12 4 83 9 274 16 645 25 1256 36 2167 49 3438 64 5129 81 729
. . : . , WriteLine(). #, . , , . 10 3.
Console.WriteLine(" 10/3 : (0:#.##)", 10.0/3.0); .
10/3 : 3.33 #.## WriteLine()
. , , , WriteLine() , .
.
Console.WriteLine("{0:###,###.##}", 123456.56); .
123,456.56 , ,
.
-
3. , 79
decimal balance;
balance = 12323.09m;Console.WriteLine(" {0:}" , balance);
, .
$12,323.09 ,
.
// // .
using System;class UseDecimal {static void Main() {decimal price;decimal discount;decimal discounted_price;// ,price = 19.95m;discount = 0.15m; // 15%discounted_price = price - ( price * discount);Console.WriteLine(" : {0:}", discounted_price);
}}
.
: 16,96 .
C# ,
. , 100 . , . .
# . . , . , '' '%' .
. , 10 -100 . . , 11.123 . C# .
-
80 I. C#
, C# . : , 2,123987 0.23? , C# .
-, , , int. , : int, uint, long ulong . -, double.
, . , long l L. , 12 int, a 12L long. u U. , 100 int, a 100U uint. ul UL. , 984375UL ulong.
, float F f., 10.19F float. double, d D, . , , double.
, decimal m . , 9.95 decimal.
int, uint, long ulong, byte,sbyte, short ushort, , .
, , ,
16, 10. 16 . 0 9, F, 10, 11, 12, 13, 14 15. , 16 10. , C# . 0,.. "". .
count = 0xFF; // 255 incr = 0x1; // 26
,
, , . , , , #,
-
3. , 81
. C# , (. 3.2). , .
3.2.
\a ()\b \f ( )\n ( )\r \t \v \0 \' \" \\
, ch .
ch = '\t'; ch
.
ch = '\'';
# .
, . :
" " . .
, , . , \n \t.// // .
using System;
class StrDemo {static void Main() {
-
82 I. C#
Console.WriteLine(" \n \n ");Console.WriteLine("\t\t");Console.WriteLine("\t\t");
// .Console.WriteLine("\"3?\", .");
}}
.
"?", .
, \n. WriteLine() \n ( ), . , .
, . @, . . , , , . ("), (""). .
// .
using System;
class Verbatim {static void Main() {Console.WriteLine(@"
, .");
Console.WriteLine(@" :1 2 3 45 6 7 8");
Console.WriteLine(@" : "" #.""");}
} .
-
3. , 83
, . :1 2 3 45 6 7 8 : " #."
, , .
, , . , . , , . , .
: . , 'X', char. , , "X", - .
:
_;
, ; _ . , . , . , bool . , . , int char.
C# . , , , , . #.
# . , , , .
, ,
, . , . (=) . :
-
84 I. C#
_ = ; , . .
.
int count = 10; // 10 count.char ch = 'X'; // ch X.float f = 1.2F // f 1,2.
, , , , .
int , b = 8 , = 19, d; // b b .
, C# , . .
// .
using System;
class DynInit {static void Main() {// ,double s1 = 4.0;double 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() ( ) ,
-
3. , 85
hypot. , , , , .
, C# .
, , int bool, . C# 3.0, , , . .
var . , .. , . .
var = 2.7183;
, double, double. :var = 2.7183F; float.
. , , .
// .using System;class ImplicitlyTypedVar {static void Main() {// . // double, // double.var s1 = 4.0;var s2 = 5.0;
// , hypot // double, ,// Sqrt(), double.var hypot = Math.Sqrt( (s1 * s1) + (s2 * s2) );
Console.Write(" " +s1 + " by " + s2 + " ");
Console.WriteLine("{0:#.###}.", hypot);
// ,
-
86 I. #
// s1 double // .// s1 = 12.2; // !
}}
, . , -
. .
// s1 = 12.2; // ! , s1
double. , . , , . , . , , , s1 .
C# , . , , (LINQ), 19. , , .
: .
var s1 = 4.0, s2 = 5.0; // ! . , s1 s2, .
, ,
Main(). C# . 2, . . , , , . , , . .
C# , . ( ) , . , .
-
3. , 87
, , . , , .
, , , . , , . , .
. , , , , . . , , , . : , , .
, .
// .
using System;
class ScopeDemo {static void Main() {int x; // Main().
= 10;if(x == 10) { //
int = 20; // .
// , .
Console.WriteLine(" : " + + " " + ); = * 2;
}// = 100; // ! .
// - .Console.WriteLine(" " + );
}}
, Main(), . if . , . line = 100;, , . (//), , . if, ,
-
88 I. C#
, , .
, . , , . , , , , .
, , . .
// .
using System;
class VarInitDemo {static void Main() {int x;
for(x = 0; x < 3; x++) {int = -1; // .Console.WriteLine(" : " + ); // -1
= 100;Console.WriteLine(" : " + );
}}
} .
: -1 : 100 : -1 : 100 : -1 : 100
, -1 for. , 100, .
C# : , , , . , .
/* , , .
-
3. , 89
*** . ****/
using System;
class NestVar {static void Main() {int count;
for (count = 0; count < 10; count = count+1) {Console.WriteLine(" : " + count);
int count; // !!!for(count = 0; count < 2; count++)Console.WriteLine(" !");
}}
} C++,
, , , . , C++ count , for, , . C++ . C# , , .
. , int float.int i;float f;
i = 10;f = i; // float
, , . i float, f. # , , . , bool int ., - . , , . , .
-
90 I. #
,
:
; , .
, . , int , byte, , , int byte, , .
, , , . , , long double .// long double.using System;class LtoD {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 {static void Main() {long L;double D;D = 100123285.0;L = D; // !!!Console.WriteLine("L D: " + L + " " + D);
}}
, decimal float double, char bool. , char bool .
-
3. , 91
,
, . . . . .
(_) _ ,
. .
double , ; / int,
.
(int) ( / ) double,
/ int . / , int , . , double int .
, . , long int , long int, . , . , 1,23 , (1), (0,23) .
, . , .
// .
using System;
class CastDemo {static void Main() {double x, y;byte b;int i;char ch;uint u;short s;long 1;
x = 10.0;
-
92 I. C#
= 3.0;// double int, .i = (int) ( / );Console.WriteLine(" / : " + 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(" -12: " + u +
" -- .");Console.WriteLine();// int char,b = 88; // ASCII Xch = (char) b;Console.WriteLine("ch 88: " + ch);
}}
.
/ : 3
b 255: 255 -- .
-
3. , 93
b 257: 1 -- .
s 32000: 32000 -- .s 64000: -1536 -- .
u 64000: 64000 -- .u -12: 4294967284 -- .
ch 88: X
. / int , , .
b 255, , byte. b 257, , byte. , int byte .
s short 32 000 uint, , short. 64 000, short, . , uint short .
u 64 000 l long. , 64 000 uint. u -12, , uint. , long uint .
, char byte, , .
,
. , . , short long, . , .
C# . , .
decimal, decimal ( float double, ).
-
94 I. C#
double, double. float, float. ulong, ulong ( sbyte, short, int long, ).
long, long. uint, sbyte, short int, long. uint, uint. int.
. -, . , float double decimal , , , ulong . .
-, . : , int. , char, sbyte, byte, ushort short int . . , int.
, , . , byte int , - byte. .
. , , byte, . byte int. , int. , , byte, int. , . .
// !
using System;
class PromDemo {static void Main() {byte b;
b = 10;
-
3. , 95
b = (byte) (b * b); // !!Console.WriteLine("b: "+ b);
}}
, b*b b, byte! , b*b b int byte . , , , , .
. , char, ch1 ch2 int.char ch1 = 'a', ch2 = 'b';ch1 = (char) (ch1 + ch2);
ch1 ch2 int, char.
, . , int(byte,sbyte, short ushort), .. , int. char. , uint, long.
.
. , 1 10 . , , Math.Sqrt(), int.// .using System;class CastExpr {static void Main() {double n;for(n = 1.0; n
-
96 I. C#
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.16227766016838 : 3 : 0.16227766016838
, , Math.Sqrt(), int . , Math.Sqrt(n) - (int) Math.Sqrt(n) int , , . , double. int , Math.Sqrt().
-
C# , . # : , , . . , C# , , -. , .
,
#, .
+ - , * / % ++
4
-
98 I. C#
+, -, * / , . .
, . , , / , ; , 10/3 3. (%), . . , 10 % 3 1. C# % , . 10.0 % 3.0 1. C# C++, . .
// %.
using System;
class ModDemo {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;
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.
(++) (--) 2.
, . .
1, 1. ,
++;
-
4. 99
= x + 1;
--;
= - 1;, , ,
, . .
( ) ( ). ,
= + 1; :
++; // :
++; // ( )
. , . , . , . .
= 10; = ++;
11, 1, . = 10; = ++; 10, , 1. 11. , 11: .
. , .
// // (++).
-
100 I. #
using System;
class PrePostDemo {static void Main() {int x, y;int i;
x = 1; = 0;Console.WriteLine(" , " +
" = + ++;");for(i = 0; i < 10; i++) { = + ++; // ++
Console.WriteLine( + " ");}Console.WriteLine();
x = 1; = 0;Console.WriteLine(" , " +
" = + ++;");for(i = 0; i < 10; i++) { = + ++; // ++
Console.WriteLine( + " ");}Console.WriteLine();
}}
.
, = + ++1361015142128364555
, = + ++;2591420
-
4. 101
2735445465
,
= + ++; , . 1.
= + ++; 1, , . , ++ ++ .
: , :
+ ++x
, . , 1 .
, , "" "". , . .
.
== != > < >=
-
102 I. #
bool.
, , == !=. , = , . , . bool , (true) (false) . , true > false C# .
bool, bool. &, |, ^ ! , , .
p q p & q p | q p ^ q !Pfalse false false false false truetrue false false true true falsefalse true false true true truetrue true true true false false
, (true), true.
, .
// // .
using System;
class RelLogOps {static void Main() {int i, j;bool b1, b2;
i = 10;j = 11;if(i < j) Console.WriteLine("i < j");if(i
-
4. 103
if(i == j) Console.WriteLine(" ");if(i >= j) Console.WriteLine(" ");if(i > j) Console.WriteLine(" ");
b1 = truerb2 = false;if(b1 & b2) Console.WriteLine(" ");if(!(b1 & b2)) Console.WriteLine("!(b1 & b2) true");if(b1 | b2) Console.WriteLine("b1 | b2 - true");if(b1 ^ b2) Console.WriteLine("bl ^ b2 true");
}}
.
i < ji
-
104 I. C#
int i, j;
for(i = 0; i < 2; i++) {for(j = 0; j < 2; j++) {if(i==0) p = true;if(i==1) p = false;if(j==0) q = true;if(j==1) q = false;
Console.WriteLine("p " + p + ", q " + q);if(!p | q)
Console.WriteLine(" " + p +" " + q + " " + true);
Console.WriteLine();}
}}
} .
True, q True True True True
True, q False
False, q False False True True
False, q False False False True
C# , ,
, . . (false), . (true), . , .
&&, ||. & |. , .
. : d n . n/d , n d .
-
4. 105
, .
// .
using System;
class SCops {static void Main() {int n, d;
n = 10;d = 2;if (d ! = 0 && (n % d) == 0)Console.WriteLine(n + " " + d);
d = 0; // d
// d , if (d != 0 && (n % d) == 0)Console.WriteLine(n + " " + d);
// // , - .if (d != 0 & (n % d) == 0)Console.WriteLine(n + " " + d);
}}
- if : d. , , . , d 2, . , , , . , , . , .
, . ? , - . .
// .
using System;
class SideEffects {static void Main() {int i;bool someCondition = false;i = 0;
-
106 I. C#
// i ,// , if .if(someCondition & (++i < 100))Console.WriteLine("He ");
Console.WriteLine(" if : " + i); // 1// i ,// .if(someCondition && (++i < 100))
Console.WriteLine("He ");Console.WriteLine(" if : " + i); // - 1 !!
}}
, someCondition bool false. if. , if i , someCondition false. &, if, . if . i , ( someCondition) false,, . : , , #.
: , .
(=). #
, . ._ =
_ . ,
: . , , .int , , z; = = z = 100; // 100 , z
100 , z . z, , , . " " .
-
4. 107
C# ,
. .
= + 10; , .
+= 10; += ,
, 10. .
= - 100;
x -= 100; . , 100.
, .. , , . ;
_ =
, .
.
+= -= *= /=%= &= |= ^=
, . .
. - , , "" . -, , . , #.
C# , ,
#. () . , bool, float double.
-
108 I. C#
, , , . , , , . C# . 4.1.
4.1.
&
| ^ >>
-
4. 109
class MakeEven {static void Main() {ushort num;ushort i;
for(i = 1; i
-
110 I. #
, 1 .
. , .
// , ,// .
using System;
class IsOdd {static void Main() {ushort num;
num = 10;
if((num & 1) == 1)Console.WriteLine("He .");
num = 11;
if((num & 1) == 1)Console.WriteLine(num + " .");
}}
.
11 . if
num 1. num , .. 1, num & 1 1. . if , .
& , byte . .
// , .
using System;
class ShowBits {static void Main() {int t;byte val;
val = 123;for (t=l28; t > 0; t = t/2) {
-
4. 111
if((val & t) != 0) Console.Write("1 ");if((val & t) == 0) Console.Write("0 ");
}}
} .
0 1 1 1 1 0 1 1 for
val , , . , 1, , 0.
. 1 - , 1 . :
1101 00111010 1010
| __________1111 1011
, , , , .
// , .
using System;
class MakeOdd {static void Main() {ushort num;ushort i;
for(i = 1; i
-
112 I. #
num : 3
num: 3num : 3
num: 4num : 5
num: 5num : 5
num: 6num : 7
num: 7num : 7
num: 8num : 9
num: 9num : 9
num: 10num : 11
num 1, 1 , . 1 , . , , .
, , .
0111 11111011 1001
^ __________1100 0110
, . , X Y, Y, X. ,
R1 = X ^ Y;R2 = R1 ^ Y; R2 , X.,
-
4. 113
, , . , . , . , , . , .
// .using System;
class Encode {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! : 1y : Hi!
, . ( , , , , .)
( 1) . , 1001 0110, ~ 0110 1001.
-
114 I. C#
1 .
// .
using System;
class NotDemo {static void Main() {sbyte b = -34;
for(int 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(int t=128; t > 0; t = t/2) {if((b & t) != 0) Console.Write("1 ");if((b & t) == 0) Console.Write("0 ");
}}
} .
1 1 0 1 1 1 1 00 0 1 0 0 0 0 1
C# ,
, . C# .
>
:
> _
_ , .
, . . , . , . , 1. , ,
-
4. 115
1. , .
. , .
, . , 1. , . . . , 1 , , 128, .
// .
using System;
class ShiftDemo {static void Main() {int val = 1;
for(int i = 0; i < 8; i++) {for(int 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 10 0 0 0 0 0 1 00 0 0 0 0 1 0 00 0 0 0 1 0 0 00 0 0 1 0 0 0 00 0 1 0 0 0 0 00 1 0 0 0 0 0 01 0 0 0 0 0 0 0
-
116 I. C#
1 0 0 0 0 0 0 00 1 0 0 0 0 0 00 0 1 0 0 0 0 00 0 0 1 0 0 0 00 0 0 0 1 0 0 00 0 0 0 0 1 0 00 0 0 0 0 0 1 00 0 0 0 0 0 0 1
2, 2. , , . , , . .
// 2.
using System;
class MultDiv {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);
-
4. 117
// 2 .n = n
-
118 I. C#
, 3, ?. , absval val.absval = val < 0 ? -val : val; // val
absval val, . val , absval , .
?. , .
// , ?.using System;class NoZeroDiv {static void Main() {int result;for(int i = -5; i < 6; i++) {result = i != 0 ? 100 / i : 0;if (i ! = 0)Console.WriteLine("100 / " + i + " " + result);
}}
} .
100 / -5 -20100 / -4 -25100 / -3 -33100 / -2 -50100 / -1 -100100 / 1 100100 / 2 50100 / 3 33100 / 4 25100 / 5 20
.
result = i != 0 ? 100 / i : 0; result 100
i. , i . , result , .
? . , , ?, . ? bool,
-
4. 119
if. 100 , .
// , .
using System;
class NoZeroDiv2 {static void Main() {
for(int i = -5; i < 6; i++)if(i != 0 ? (i%2 == 0) : false)Console.WriteLine("100 / " + i + " " + 100 / i);
}}
if . i , if . i , if , i , , . . , .
C# ,
. , , , , .
=10/*(127+); = 10 / * (127 + );
, , , . . , , , . , ?
= /3-34*temp+127; = (/3) - (34*temp) + 127;
. 4.2 #:
. , .
-
, #. : , if switch, , for, while, do-while foreach, : break, continue, goto, return throw. throw, C# , 13, .
if if 2,
. :
if() ;else ; , a e- if else. else . , if else, . if, .
if(){
}
5
-
122 I. C#
else{
} ,
if. else, . . , if, bool.
, if else , , .
// , .using System;class PosNeg {static void Main() {int i;
for(i=-5; i
-
5. 123
if, , else, else. .if (i == 10) {if(j < 20) a = b;if(k > 100) = d;else a = ; // else if(k > 100)
}else a = d; // else if(i == 10)
, else if(j < 20), , , if else. , else if(i == 10). else if(k > 100), if .
if. . , , , . , , .
// , // , .using System;class PosNegZero {static void Main() {int i;
for(i=-5; i
-
124 I. C#
if-else-if if-else-
if, if. .if();
else if ();
else if ();
else;
. , , .
, else, , . else , , .
if-else-if. , .
// // , .using System;class Ladder {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.");
elseConsole.WriteLine(num + " 2, 3, 5 7.");}
}}
.
2 2 3 3
.
.
.
-
5. 125
4 2 5 5 6 2 7 7 8 2 9 3 10 211 2, 3, 5 7.
, else , .
switch C# switch,
. , . if, switch. . . , , . switch.switch() {case 1: break;
case 2: break;
case 3: break;
default: break;
} switch (char,
byte, short int), . ( string .) , , switch . , switch, . , , . switch .
-
126 I. C#
default , . default . , . , , , break.
, switch.// switch.using System;class SwitchDemo {static void Main() {int i;for(i=0; i
-
5. 127
i , , , default.
switch i int. , switch , char. char switch.// char switch.
using System;
class SwitchDemo2 {static void Main() {char ch;
for(ch='A'; ch
-
128 I. C#
case break, , switch, , switch. default "", , , break.
"" #, , C++ Java. case (.. "") . C# case . -, case . , case . -, case , case .
"" case , case , .
// "" case.
using System;
class EmptyCasesCanFall {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;
}}
} .
i 1, 2 3i 1, 2 3i 1, 2 3i 4
i 1, 2 3, , WriteLine(). case ""; .
case , . .
-
5. 129
switch switch
, switch. switch . switch , . , .
switch(ch1) {case 'A': Console.WriteLine(" " +
" switch.");switch(ch2) {case 'A':Console.WriteLine(" A " +
" switch");break;
case '': // ...} // switchbreak;
case '': // ...
for for 2,
. . , for.
for .
for(; ; ) ; :
for(; ; ){ ;
} , , , , ; , ; , , , , . , for . for , . , , , for.
for , , . 100 -100 5.
-
130 I. C#
// for .
using System;
class DecrFor {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, , 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
-
5. 131
}if(isprime)Console.WriteLine(num + " .");
elseConsole.WriteLine(" " + num +
" " + factor);}
}}
.
2 3 4 25 6 37 8 4 9 3 10 511 12 613 14 7 15 5 16 817 18 919 -
for for #,
. for .
for . . .
// for.using System;class Comma {static void Main() {int i, j;for(i=0, j=10; i < j; i++, j--)Console.WriteLine("i j: " + i + " " + j);
}}
-
132 I. C#
.i j: 0 10i j: 1 9i j: 2 8i j: 3 7i j: 4 6
. , , i j. , , i , j . , . for , , .
for. for ( 100). . .
// for // .
using System;
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);
}}
.
-
5. 133
: 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 100i, j: 1 99i, j: 2 98i, j: 3 97i, j: 4 96i, j: 5 95i, j: 6 94i, j: 7 93i, j: 8 92i, j: 9 91i, j: 10 90
for , done (true). done , i j.
-
134 I. C#
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);
-
5. 135
i++; // }
}}
i , for. , for. , , for.
for, , .. , . , # .
for(;;) // , {//...
} .
, , "" . ( " break " .)
C# for , . . , 1 5.
// .using System;
class Empty3 {static void Main() {int i;int sum = 0;// 1 5for(i = 1; i
-
136 I. C#
, for, . .
sum += i++ .
, #, , . : sum sum i, i. , .
sum = sum + i;i++;
for , for,
. for. , 1 5, i, for, .// for.using System;
class ForVar {static void Main() {int sum = 0;int fact = 1;// 1 5for (int i = 1; i
-
5. 137
while C# while.
:
while () ; , . , . , , .
, while .
// .
using System;class WhileDemo {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 . , , mag .
for, while , . , , . while , 2 0 9.
-
138 I. C#
// 2.
using System;
class Power {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);}
}}
.
2 0 12 1 22 2 42 3 82 4 162 5 322 6 642 7 1282 8 2562 9 512
, while , . , for, while .
do-while C# do-while.
for while, , do-while . , do-while . do-while.do {
;} while ();
. ,
-
5. 139
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 .
foreach foreach
, . C# , . foreach 7, .
break break
, ,
-
140 I. #
. 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
-
5. 141
class FindSmallestFactor {static void Main() {int factor = 1;int num = -1000;for (int i=2; i
-
142 I. C#
: 0 1 2 3 4 5 6 1 8 9 : 2
: 0 1 2 3 4 5 6 1 8 9
, break , .
break . -, break, , break . -, break, switch, , .
continue continue
. continue , , . , continue break. continue 0 100.
// continue.
using System;
class ContDemo {static void Main() {
// 0 100.for (int i = 0; i
-
return return .
. 6.
goto C# goto
. goto, , . " " , "" . goto . goto . ,, , , . , . goto . goto , . goto , , , .
goto # . , goto, . 1 100 goto . = 1;loop1:++;if(x < 100) goto loop1; , goto
case default switch. case default switch . goto. goto switch. , switch. goto switch.// goto switch.
using System;
class SwitchGoto {static void Main() {
for(int i=1; i < 5; i++) {switch(i) {case 1:
5. 143
-
144 I. C#
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 switch
case default. , case break. goto case , "" , , break . , goto switch. , :
// goto case 1; // ! switch . . , goto switch, , , .
goto .
-
5. 145
// goto.
using System;
class Use_goto {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 0i, j, k: 0 0 1i, j, k: 0 0 2i, j, k: 0 0 3! i, j, k: 0, 0 3
goto, if break, . goto . goto, , .
: , , .
-
. #, . , - (). . #, . .
. , , . , .
. , . , . # , . , , , . , . , .
,
6
-
148 I. C#
, , ,
. , .
, , , -. , C# -. , , , , - , , , , , . : . . .
class. , .
class _ {// . 1; 2;//... N;
// . _ 1() {
// } _ 2() {
// }
// ... _ () {
// }
} ,
. , public, . 2, (private) , (public), .e. . . , , . . , , , Main(), (public). , , . 8.
-
6. , 149
,
. .
#, . , , ... , , , , . , . , .
, , : Main(). Main() . , .
,
, , , , .. ( Building) : , .
Building. : Floors, Area Occupants. , Building . , . ( .)
class Building {public int Floors; // public int Area; // public int Occupants; //
} , Building,
. :
_;
; , _- , . , , , . Building public. , Building.
class . Building.
-
150 I. C#
Building. , , , class , . , Building .
Building, .
Building house = new Building(); // Building house
Building, .. "" . .
, , , , . , Building Floors, Area Occupants. , - . - . -.
.
, . , 2 Floors house .
house.Floors = 2; , -
. , Building.// , Building.
using System;
class Building {public int Floors; // public int Area; // public int Occupants; //
}
// Building.class BuildingDemo {static void Main() {Building house = new Building(); // Buildingint areaPP; //
// house.house.Occupants = 4;house.Area = 2500;house.Floors = 2;
// .areaPP = house.Area / house.Occupants;
-
6. , 151
Console.WriteLine(" :\n " +house.Floors + " \n " +house.Occupants + " \n " +house.Area +" . , \n " + + " ");
}}
: Building BuildingDemo. BuildingDemo house Building Main(), Main() 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, C# , . , , .
csc Building.cs BuildingDemo.cs Visual Studio,
. ,
: , . , . , , . , Building, Floors, Area Occupants, . .
-
152 I. #
// Building.using System;class Building {public int Floors; // public int Area; // public int Occupants; //
}// Building.class BuildingDemo {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 + " ");
// .areaPP = office.Area / office.Occupants;Console.WriteLine(" :\n " +
office.Floors + " \n " +office.Occupants + " \n " +office.Area +" . , \n " +areaPP + " ");
}}
.
:2 4 2500 . , 625
-
6. , 153
:3 25 4200 . , 168 , house ,
office. . 6.1.
. 6.1.
Building
.
Building house = new Building!); . -,
house, Building. , , . -, , , . new. , house . , house Building.
new (.. ) , . , C# .
, house , , .
Building house; // house = new Building(); // Building
house Building. , house , , . Building, house. house .
house
office
Floors
Area
Occupants
2
2500
4
Floors
Area
Occupants
3
4200
25
-
154 I. C#
, , , . , . , . ,
int ; = 10; 10, 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; WriteLine()Console.WriteLine(house1.Area);Console.WriteLine(house2.Area); : 2600.
, house1 house2, , . , , house2.
-
6. , 155
Building house1 = new Building();Building house2 = house1;Building house3 = new Building();house2 = house3; // , house2 house3,
// .
house2 , house3. house1 .
,
. Building, , , . , , , . , , , . , .
. C# . , . , . , , , Main() , . , #.
, #. , . , GetVal, : GetVal(). .
:
_ (_) {//
} , , . , . , (private) , . (public), . _ , . , . , void. , . , . , _ , . , , . , .
-
156 I. C#
Building , , ,
. , Main() . , . Building, , . Area Occupants, Building. , Building. , Building, , Building, . . , Building , , - , , , 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 {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;
-
6. , 157
office.Area = 4200;office.Floors = 3;Console.WriteLine(" :\n " +
house.Floors + " \n " +house.Occupants + " \n " +house.Area +". , ");
house.AreaPerPerson();Console.WriteLine();Console.WriteLine(" :\n " +
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() , .
-
158 I. C#
AreaPerPerson() . , .
Main().house.AreaPerPerson();
AreaPerPerson() house. , AreaPerPerson() , house, -. AreaPerPerson() , . , , .
house.AreaPerPerson() , house. , office.AreaPerPerson() , office. , AreaPerPerson() .
AreaPerPerson() : Area Occupants , .. -. , , , -. , . . , . . , Area Occupants AreaPerPerson() , AreaPerPerson().
, Occupants AreaPerPerson() ( , ). Occupants , - . 13, , , C# , .
, . -,
, , AreaPerPerson() . -, return. return: void, .. , , , . , , .
void return.return;
-
6. , 159
, , . .
public void MyMeth() {int i;
for(i=0; i
-
160 I. C#
, 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 {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.AreaPerPerson();
Console.WriteLine(" :\n " +house.Floors + " \n " +house.Occupants + " \n " +house.Area +" . , \n " +areaPP + " ");
-
6. , 161
Console.WriteLine();
// .areaPP = office.AreaPerPerson();
Console.WriteLine(" :\n " +office.Floors + " \n " +office.Occupants + " \n " +office.Area +" . , \n " +areaPP + " ");
}}
, . :
AreaPerPerson() , . , , AreaPerPerson(). ,
areaPP = house.AreaPerPerson(); areaPP ( house).
, AreaPerPerson() int. , . , , , , , , . , double, double.
, , , . , areaPP, AreaPerPerson() , WriteLine(), .Console.WriteLine(" :\n