Post on 01-Dec-2015
. .
-
- -
2 0 0 9
#.
: . .
. . . ,
, ; . . -
,
,
32.973-018.1 004.43 . .
12 #. . . .: , 2009. 432 : .
ISBN 978-5-91180-174-8 , #,
. # 2.0 (2005) , ,
, , .
,
, ,
.
,
.
# . , , ,
.
,
.
, 2009
.
.
ISBN 978-5-91180-174-8
, 1 9 8 2 0 6 , - , , . 7 3 , 2 9
- 0 0 5 - 9 3 , 2 ; 9 5 3 0 0 5 -
0 4 . 0 9 . 0 8 . 7 0 x 1 0 0 / 1 6 . . . . 3 4 , 8 3 . 2 7 0 0 . 1 1 0 2 0 Q P . . . .
1 9 7 1 1 0 , - , ., . 15.
6 7
1. .NET 8 - 11 ^ 13 Visual Studio.NET ' 14 2 1
2. 22 2 2 3 1 3 6
3. , 38 3 8 4 1 42 5 9 6 7
4. 69 , 70 7 0 7 5 8 7 8 9 c h e c k e d u n c h e c k e d 95 9 5
5. : 100 103 : 1 0 4 106 th is 114 1 1 4 120 1 2 4
6. 126 126 f o r e a c h 136 138 139 R a n d o m 148 150
4
7. : 152 152 153 154 M a i n 156 157 161 169 169 170
8. 172 172 178 181 182 o b j e c t 183 186
9. 188 188 190 . is as 1 9 4 195 .NET 198 2 1 2 2 1 5 2 1 9
10. , 2 2 0 2 2 0 2 3 2 2 3 7 2 4 5
1 1 . 2 4 6 2 5 0 - 2 5 3 2 5 5 2 6 0 - 2 6 2 2 6 3 ( ) ' . . . 2 6 7 2 7 0
12. , , , 2 7 2 2 7 2 2 7 5 2 7 8 2 7 9 2 8 3 2 8 5 2 8 7 2 9 0
5
13. , - 291 2 9 1 System.Col lect ions 2 9 5 - 2 9 9 3 0 8 3 0 9 3 1 0
14. W i n d o w s 311 - 3 1 2 W i n d o w s - 3 1 4 C o n t r o l 3 2 3 3 2 5 3 3 7 F o r m 3 3 8 3 3 9 A p p l i c a t i o n 3 4 2 3 4 4 3 4 6
15. # 3 4 7 3 4 7 3 5 5 X M L 3 6 5 , 3 6 6 3 6 9
3 7 0 1 . 3 7 0 2 . . . . . . . . . . 3 7 1 3 . 3 7 9 4 . 3 8 1 5 . 3 8 5 6 . 3 8 9 7 . 3 9 3 8. 3 9 5 9 . 4 0 0 10. 4 0 5 1 1 . 4 1 1 12. W i n d o w s - 4 1 2
# 4 2 3
4 2 5
4 2 7
, #, .
# 2.0 (2005) , , ,
, .
, , ,
.
,
.
# . , ,
.
.
.NET , ,
. # , BASIC + + .
, # , , ,
.
, # , -.
# : -,
1, -,
, , ,
,
.NET .
, Microsoft # Express 2005, (http://msdn.microsoft.com/vstuclio/express/visualCsharp/),
.
1 , # Express 2005 Windows /2000, Pentium 600 128 .
7
- -.
Microsoft , , ,
,
. ,
.
-. ,
++ --,
!
# (http:// www.ecma-international.org/publications/standards/Ecma-334.htm) , .
, 14 Windows, 15 - -. , ,
, ,
,
.NET .
,
#, , , , - .
. , , ,
, ,
. -
http://ips.ifmo.ru , .
:
,
. , ,
,
.
, , ,
mux@tp2055.spb.edu - .
,
comp@piter.com ( ) . ! - www.piter.com.
1
.NET
, .
, , ,
. ,
, , ,
, , .
:
,
;
, ,
, ;
;
, ;
.
,
. .NET ( ) , Visual Studio.NET, , , ,
.
,
,
:
I riepettocztjvtocTnb ;
;
; ;
1
.NET
, .
, , ,
. ,
, , ,
, , .
:
,
;
, ,
, ;
;
, ;
.
,
. .NET ( ) , Visual Studio.NET, , , ,
.
,
,
:
;
;
; ;
.NET 9
;
.
.NET . , ,
, (Microsoft Intermediate Language, MSIL, I L ) , , , .
, ,
(Common Language Runtime, C L R ) . C L R . C L R JIT-, IL ,
. J IT just in time, , ,
.
1 .
.NET . 1.1.
(. .dll)
(IL ) CLR t
-
( )
1
. 1 . 1 . .NET
dll, IL . ,
, .
,
,
.
,
.
10 1. .NET
.
,
, .
.
C L R , ,
.
.
.NET *, .NET. . 1.2. . ,
: -, ,
, , .
. N E T
Visual Studio.NET
.NET Framework
(FCL) -
XML
(CLR)
.NET Enterprise Servers
.NET Building Block Services
. 1.2. .NET
,
X M L ( X M L ). ,
- Windows-. .
.NET , .
1 . ,
.
- 11
CLR (framework), .
.
. , , Windows, Windows- .
.NET - , #, -
( ) .
-
.
, , .
. ,
, , ,
.
:
, ,
, .
1.
-
. ,
.
, .
.
. , , ,
, , .
. , .
.
, ,
,
, !.
12 1. .NET
.
.
( ) . :
.
, !
, ,
.
, ,
.
:
.
, .
,
.
,
, ,
.
,
.
- ,
,
.
, .
:
;
;
.
,
, ,
.
.
, ,
.
,
, - , .
, ,
, -, , ,
, , .
13
, .
,
.
,
8.
, .
, ;
,
;
;
;
.
. : -
,
,
- .
,
.
, .
, .
#, Java, + + , Delphi . . ,
, , . .
,
, .
, ,
, .
,
1.
.NET, , .NET, object .
,
1 , , -
- , , Windows.
\
14 1. .NET
:
. ,
.
, .NET, System. , ,
, ,
-.
.
, System. Net , , System.Windows.Forms , , . .
, .
, ,
# , Visual Studio.NET.
Visual Studio.NET Visual Studio.NET , , , ,
.NET- . Microsoft : # , VB.NET, + + J#.
.NET . , .
.NET, , Ada, COBOL, Delphi, Eiffel, Fortran, Lisp, Oberon, Perl Python.
Bee .NET- (Common Language Specification, CLS ) , .
.
.NET.
.
: , ,
. Visual Studio.NET , :
Windows- Windows, , , .;
,
;
,
;
Visual Studio.NET 15
- ,
(, Internet Explorer) ;
- , .
(solution). .
Visual Studio.NET Windows Windows- -, .
,
- .
, - .
,
,
.
,
# . : # . , Visual Studio.NET, [8], [16].
, ,
# 7.1 ( .NET Framework 1.1), . , , 2.0, Visual # 2005 Express Edition ( .NET Framework 2.0).
.
Visual Studio.NET1 File New Project.... Visual # Projects, Console Application. Name , Location , .
. . 1.3.
( File, Edit, View . . ) (toolbars). , (View Toolbars...), .
1 .
16 1. .NET
" 3
. 1.3.
. Solution Explorer ( , View Solution Explorer ). , : (System, System.Data, System.XML), (App.ico), (Classl.cs) 1 (Assemblylnfo.cs). ,
Class View, . Class View , , .
,
. Windows , ,
. ( csproj), ( sin) (Classl .cs).
Properties ( , View Properties ).
,
.
Visual Studio.NET 17
. ,
, Classl,
FileName ( Enter). ,
, .
, .
() 1 ,
2 -, .
:
, .
, , .
.
( 1.1). , .
, .
1 . 1 .
using System;
namespace ConsoleAppli cati onl
{ /// /// Summary description for Classl. /// class Classl {
/// /// The main entry point for the application. /// [STAThread] static void Main(string[] args) {
// // TODO: Add code to start application here //
} }
} , .
, .
.
1
18 1. .NET
using System System ( ). namespace , ConsoleAppl icationl. , , ,
.
, ,
.
# - , .
,
Classl. class, ( , ).
.
,
. ,
, , .
, .
Main. Main . .
:
[ ] _ ( [ ] ) {
: ,
}
, ,
.
,
, ,
, -,
. :
[ void | int ] _(): , _
,
voi d, i nt, . , ,
, . ,
, .
Visual Studio.NET 19
, , ,
, .
(. . 106).
- .
,
. .
Main :
// TODO: Add code to start application here
: , .
( !) |
Console.WriteLine{"-! -! () ");
Console System. WriteLine . ,
._
,
Console , Console. , ,
. Enter .
. ,
.
, 1.2 ( ,
). , ?
,
* ,
, , , ,
. File Save Save ( ). , .
20 1. .NET
1.2. #
using System: namespace ConsoleApplicationl
{ class Classl {
static void MainO {
Console.WriteLine( "-! -! ( ) " ): }
} } . 1.4 Visual # 2005 Express Edition. , , , 1.1 .
. 1.4. Visual # 2005 Express Edition
F5 ( Debug Start). , -! -! () , . ,
,
Ctrl+FS1 ( Debug Start Without Debugging).
1 Ctrl+F5 , , Ctrl, F5.
21
. ,
.
,
, .
Console.WriteLineL.) . ,
, , (There were build errors. Continue?). No, .
,
, error.
error CS1002 ; e x p e c t e d (, ) .
F1, .
.
Dynamic Help, , Properties ( ). , .
,
( Visual # 2005 Express Edition ).
, .NET, , , # .
.NET. (, [5], [19], [26], [27]) .
Visual Studio.NET , , ,
, . # ,
, .
2
#, .
.
,
,
.
# .
. ,
( ), . # Unicode. , .
,
, ,
() . , (character set). . , Windows ANSI , 1251. (8 ), 256 . , ,
. -
23
.
(, ), . Unicode , .
. ,
. 128 Unicode- ANSI.
# :
( ) (_), ;
;
, +, *, { &; ( ); .
: ,
.
(token1) , . :
{); ;
;
;
(). .
, 128 ( 12), Vasia, goto +. .
# ++ . ,
. ,
(, ) .
# , ++ . (. . 287).
. .
.
. , + b .
.
24 2.
IL : .
, ,
( , , ). . , .
,
. :
int :
.
, ,
, .
.
, .
, , sysop, SySoP SYS0P .
,
. .
.
# . , gg
1. ,
escape- Unicode, \, , \u00F2.
: 21 ate, Big g ig, #; , ( # ) .
, :
, , , , . .
(, . .), . .
:
(. )2;
1 ,
. , , . 182. 2 ,
@. , @i f.
25
,
.
, .
.
.
, ,
, , MaxLength, MyFuzzyShooshpanchik.
( , Microsoft) , , , i MaxLength, IpfnMyFuzzyShooshpanchik.
Camel, , , , , maxLength, myFuzzyShooshpanchik. ,
.
, ,
: max_length, my_fuzzy_shooshpanchik, .
# : Camel. ,
. , -
.
.
,
.
, . # . 2.1.
,
. . ,
+= b += , b . , , , , &&, |
26 2.
2 . 1 . #
abstract as base boo I break
byte case catch char checked
class const continue decimal default
delegate do double else enum
event explicit extern false finally
fixed float for foreach goto
if implicit in int interface
internal is lock long namespace
new null object operator out override params private protected public
readonly ref return sbyte sealed
short sizeof stackalloc static string
struct switch this throw true
try typeof uint ulong unchecked
unsafe ushort using virtual void
volatile while
C# (. . 42). (). (. . 161).
, ,
. : , , .
, #:
{ } [ ] ( ) . . : : + - * / * & | ! - = < > ? + + | | . . > > _ ] = < = > = + = - = * = / = % = &= | = = = = ->
, , . # , , , ,
null. , , .
1.
. 2.2. , ,
( ).
,
.
27
2 . 2 . #
true () false ()
:
(0, 1, 2, 3, 4, 5, 6, 7, 8, 9), (U, u, L, 1, UL, Ul, uL, ul, LU, Lu, 1U, lu) : Ox OX,
(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, , , , D, , F), , , (U, u, L, 1, UL, Ul, uL, ul, LU, Lu, 1U, lu)
1:
[] [.] [] [] F, f, D, d, , m
:
[][.][]{|}[+|-][] [] F, f, D, d, , m
,
,
null ,
true
false
8 8u
199226 OLu 199226L
0x188 0X00FF OxAU 0xlB8LU 0X00FF1
5.7 .001 35 5.7F .OOld 35 5F .OOlf 35m
0.2E6 0.2E6D 5E10
.lle+3
.l le-3 5E-10
'A' V ' * ' '\0' '\n' '\xF' '\x74' '\uA81B'
" Vasia" " = \0xF5 \n" " \u0056\u0061" ": WtempWfilel . txt" @":\temp\filel.txt"
null
. 2.2 . . -.
, -
, ,
,
.
28 2.
: .
.
( ) , .
10 (, 1.32 = 1,3 102 = 130). , , , ,
.
.
, .
, , ,
10.
, ( - ) , : -218, -022, -, -4.8, -0.14.
,
. -
, ,
, . 2.3. , .
2 . 3 .
L, 1 (long) U, (unsigned) F, f ( f loa t ) D, d (double) , m (decimal)
.
Unicode.
,
. 2.2:
, ( ), '', '', ' * ' ;
'\0', '\ ' ;
'VxF', '\74';
escape- Unicode '\uA81B'.
29
, escape-,
, .
:
, (, \ );
,
, , '.
. 2.4 .
, ,
.
2 . 4 . #
\
\
\f ()
\
\
\t
\v
\\
V
V
\0 -
, ,
\0, .
0 216 - 1, .
Escape- Unicode Unicode \ \U, , \u00F2, \U00010011. \U10000 \U10FFFF ; ,
\U10FFFF, .
, . ,
, ,
\:
" , \ "
30 2.
:
,
: ,
, ,
:
" \"\""
,
, # (verbatim strings). @,
, . ,
:
@" ,
"
@" """
,
,
.
:
": WappWbi nWdebugWa. exe" @":\app\bin\debug\a.exe"
( " " ) , .
nul 1 , , .
. .
. # : .
( / / ) , - /* */ , . :
// /* .
I 31
Me , , -! ( / / / ) . X M L .
,
.
15.
/ , , . -
, , ,
: .
.
:
, ,
;
( ). , ,
, ;
, , , .
. ,
, .
.
1, , : (stack) , (heap)1. -
, ,
-
.
# .
, . -
, #, i . .
,
ie ) ( ).
. , , ,
, !
32
() (. 2.1). ,
.
,
.
. 2 . 1 . #
.
, ,
. . ,
,
, .
, ,
.
# . 2.5. .NET, System , ? . ,
,
.
, ,
.
. ( , 1 ). . ! ,
, , / .
, ! .
33 i . #
.NET ,
bool Boolean true, false
[ sbyte SByte -128 127 8
byte Byte 0 255 8
short Intl6 -32 768 32 767 16
ushort UIntl6 0 65 535 16
int Int32 -2 109 2 109 32
uint UInt32 0 4 10 32
long Int64 -9 1018 9 1018
64
ulong UInt64 0 18 1018 64
char Char U+0000 U+ffff
Unicode-
16
1 float Single 1.5 10"45
3.4 1038 7 32
double Double 5.0 10324
1.7 10308 15-16 64
decimal Decimal 1.0 10~28
7.9 1028 28-29 128
string String
Unicode-
object Object
, 2" 1 . , -1 [-2""1,2~1 - 1], - ,
1 [0, 2 - 1] (. . 2.5).
, ,
, .
, . -
, .
: , 0,381 104
.
34 2.
381 4, 560,3 102 5603 5 ( ), 0,012 12 ,
,
. double, . i .
, ,
f 1 oat i nt, - . 1 ong doub
decimal , . . 2.5, float } 7 , double 15-16. , ;
,
! decimal 28-29
decimal , .
.
.
# .N1 System. , ] , . , :
# . , , ,
35
nt, uint, long ulong, .
double.
1, 10 i nt ( [ ), 2147483648 ui nt. , , 1. If, 1UL, 1000m ( - . 2.3). : , .
-
1 # - [ (. 2.2)1. - , value types), , [ . , .
, (, [). .
( , -,
object.
2.3 . -.
. ,
. ,
. 2 . 2 . #
,
.
36 2.
( b , b ). , , .
. 2 . 3 .
, .
, . 9. 5 , #, ,
-. nullable # 2.0 13.
,
. # . -
(boxing), (unboxing).
,
,
: ,
,
.
,
.
, ,
. ,
( !), .
37
, , .
, ,
. ,
, .
.
:
( , ), . ,
, .
.
,
. ,
.
: ,
, , ,
, ,
.
.
- :
;
;
;
;
;
.
, . ,
,
# ,
.
3 ,
, #, -
nporpaj .
, <
.
. , ,
. ] .
int ; float ;
,
. .
# , 1
. , i - , ! quantity , , kolich, , , A, tl7_xz prikol.
, , ! , 1 ( L) I ( i ) .
39
, -
. , -
,
, ,
.
I , ! , :
a, b = 1;
it = 0.1. = 0.If:
:
i nt, ;
b i nt, 1; f 1 oat, -1 0.1. , - double ( , ), 'loat; 0.1 -.
,
, , :
b = 1, = 100: = b * + 25;
,
, , ,
.
1 .
, ,
. 1, Z# , . -, , .
, 0 .
, , -
. .
1
.
40 3. ,
, , ,
, <
, . ,
.
# - : , .
.
, , i ,
, :
class X // X
{ int ; // X int ; // X
void Y( ) // Y {
int ; // , - Y int ; // ( )
{ // ============ 1 ============ int D; // D, -
// int : ! , //
= : // X ( * * ) = this.: // X ( * * * )
} // ============ 1 ===============
{ // ============ 2 ============ int D; // D, -
} // ============ 2 ===============
} // Y
} // X
. X, TJ : , Y. Y . .
,
,
this ( , **4
, X, >
( ).
,
(. , }
41
* * ) . D , , , .
,
, ,
, .
3.1 ,
.
3 . 1 .
using System; namespace ConsoleAppl icationl { class Classl
{ static void MainO {
int i = 3; double = 4.12; decimal d = 600m; string s = "";
Console.Write( "i = " ) Console.WriteC "y = " ) Console.Write( "d = " ) Console .Wri te( "s - " )
} }
} , Write , WriteLine, .
- (. . 59).
.
() . ,
. .
,
const, :
const int b = 1; const float x = 0:1, = 0.If: // const
Consol . Writel_ine( i ) Consol.WriteLine( ) Console.Writel_ine( d ) Console.Writel_ine( s )
42 3. , !-
,
. ,
.
,
.
. 4 n c e j 0 1, ] , .
. ,
, ,
const int b = 1. = 100: const int = b * + 25:
. one
, .
, .
, + 2 , + , 2 -. ,
, .
, . # . 3.11.
3 . 1 . #
. 105
( ) . 108, 221 [ ] . 127 ++ . 47
--
. 47
new . 48
typeof . 280
checked . 46
unchecked . 46
1
.
! 43
+
- . 48 ( )
j
. 48
-
. 48
++ . 47
--
. 47
( ) . 49 ^
*
. 50 )
/ . 50
1 . 50
( + . )
-
. 53
. 54
. 54
< . 54
> . 54
=
. 54
is . 194
as . 194
. 54
[ 1 =
. 54
& ( ) . 55 )
.
. 55
1 () . 55 && . 56
I I . 56
? ; . 56
=
. 56
*=
/ =
1-
+=
&
44 3. ,
3 . 1 () <
&
=
=
# 2.0 ??, : 13 (. , . 309).
] , . . 3.1 ; , ? .
.
, b :
int = 2, b = 5;
+ b 7 i nt, = b , ( 5) , , .
,
, . on
,
.
, + b + ( + ) + , = b = = ( = ) . b = , .
, F ( i ) + G(i++) * H( i ) F , .
,
, i ( 1).
, ! .
45
1 -
[ . , ,
, .
/ , - -
,
. () , .
; , -
(). . -
.
, i nt, . , sbyte, byte, short ushort, i nt. , int.
[ . 3.1. - , , , -
. ,
. , -
, . -
, .
double
Z [ float decimal ]
long ulong
. 3 . 1 .
46 3. ,
, i nt, . , float double decimal .
int, ui nt long float long double , .
.
, ,
, . # ,
. :
(). ,
, () . .
# -, , Excepti on, System.
, () , DivideByZeroException, OutOfMemoryException, OverflowException.
,
Exception.
, catch (), .
, ,
.
, ,
.
, ,
. checked unchecked. checked , unchecked . , , ,
.
, :
= checked (b + ) ; // unchecked { //
= b + ; }
47
, .
, ,
, .
,
.
/checked, . ,
.
(. . 89).
# #, ,
.
(++) ( - - ) , , ,
, .
,
,
, . 3.2 .
3 . 2 .
using System; namespace ConsoleApplicationl { class Classl
{ static void MainO {
int x = 3. = 3; Console.Write( " : " ); Consol.WriteLineC ++ ); Console.Write( " : " ); Console.WriteLineC );
Console.WriteC " : " ); Console.WriteLineC ++ ); Console.WriteC " : " ); Console.WriteLineC );
48 3. ,
:
: 4 : 4 : 3 : 4
, ,
, .
, ( , 5 7) .
new
new . :
new ( [ ] )
,
, :
object z = new objectO: int i = new i n t ( ) : II , int i = 0;
,
,
.
new
( , ), , ,
.
, .
.
,
OutOfMemoryException.
( -) . int, long, float, double decimal. , (. . 3.1). .
. .
, .
49
( ! ) bool. false, true, true,
false.
( ~ ) , , int, uint, long ulong.
3.3.
3 . 3 .
BSing System; namespace ConsoleApplicationl { class Ciassl
{ static void MainO
sbyte a = 3, b = -63, = 126; bool d = true; Console.WriteLineC -a ) Console.WriteLineC -c ) Console.WriteLineC !d ) Console.WriteLineC ~a ) Console.WriteLineC ~b ) Console.WriteLineC ~c )
// -3 // -126 // false // -4 // 62 // -127
, ,
. ,
.
,
1.
:
( )
, ,
, :
long b = 300; int = ( in t ) b; byte d = (byte) a;
// //
.
, .
50 3. ,
,
( * ) . int, ui nt, long, ulong, float, double decimal. , (. . 3.1). , int.
decimal ,
System. OverflowExcepti on1.
. 3.2. , z
.
,
2, , 0. NaN (not a number)
, .
3 . 2 .
* +
-
+0 -0 +00 00 NaN
+ + Z - z +0 -0 +00 - o o NaN
-
- z + Z -0 +0 - o o + o o NaN
+0 +0 -0 +0 -0 NaN NaN NaN
-0 -0 +0 -0 +0 NaN NaN NaN
+00 +00 -00 NaN NaN +00 -00 NaN
00 00 + NaN NaN -00 + o o NaN
NaN NaN NaN NaN NaN NaN NaN NaN
(I) . int, uint, long, ulong, float, double decimal. , .
(. . 3.1), int.
,
. ,
System. Di vi deByZeroExcept i on.
,
, . 3.3. , z
1 . ,
.
2 . 34.
51
.
,
, , 0.
3 . 3 .
/ + - +0 -0 + 0 0 00 NaN
+ + Z - z + - 0 0 +0 -0 NaN
- - z + Z - 0 0 + o o -0 +0 NaN
+0 +0 -0 NaN NaN +0 -0 NaN
-0 -0 +0 NaN NaN -0 +0 NaN
+00 +0 0 - + o o 00 NaN NaN NaN
-
00 +0 0 oo + o o NaN NaN NaN
NaN NaN NaN NaN NaN NaN NaN NaN
( decimal) 0 , 0.
(%) - , . ,
- ( / ) * . , System.DivideByZeroException. , int (. . 3.1). ,
- * , ,
.
. 3.4. , z .
3 . 4 .
/ / + - +0 -0 +00 00 NaN
+ + Z z NaN NaN X X NaN
- X - z - z NaN NaN -x -x NaN
+0 +0 +0 NaN NaN +0 +0 NaN
-0 -0 -0 NaN NaN -0 -0 NaN
+00 NaN NaN NaN NaN NaN NaN NaN
- 0 0 NaN NaN NaN NaN NaN NaN NaN
NaN NaN NaN NaN NaN NaN NaN NaN
( decimal) ,
0. .
52 3. ,
,
3.4.
3 . 4 . ,
using System; namespace Consol eApplicationl { class Classl
{ static void MainO {
int x = 11, = 4; float z = 4; Console.WriteLineC z * ) ; // 16 Console.WriteLineC z * le308 ); // " Console.WriteLineC x / ) ; // 2 Console. WriteLineC x / z ) ; // 2.75 Console.WriteLineC x I ) : // 3 Console.WriteLineC le-324 / le-324 ); // NaN
} }
} ,
. 2 /
, 2
, .
2 --
, ,
, ,
2 / ( ) 2 / / . ,
( + ) . one int, ui nt, long, ulong, float, double decimal
,
(. . 3.1). ] , i nt.
decimal
,
System.OverflowException1.
1 . ,
.
53
. 3.5. , z
.
,
, , 0.
3 . 5 .
+
+0 -0 +00 - o o NaN
X z X X +00 -00 NaN
+0
+0 +0 +00 -00 NaN
-0
+0 -0 +00 - o o NaN
+00 +00 +00 +00 +00 NaN NaN
00
00 00 NaN -00 NaN
NaN NaN NaN NaN NaN NaN NaN
( - ) . int, ui nt, long, ulong, float, double decimal.
,
(. . 3.1). , i nt.
decimal
,
System.OverflowException.
. 3.6. , z .
, .
,
, - , ,
0 , - .
3 . 6 .
-
+0 -0 +00 00 NaN
X X X 00 +00 NaN
+0 -
+0 +0 -00 +00 NaN
-0 -
-0 +0 -00 +00 NaN
+00 +00 +00 +00 NaN + o o NaN
00 00 00 00 00 NaN NaN
NaN NaN NaN NaN NaN NaN NaN
54 3. ,
( ) .
,
1.
( ) . ( ) , ( ), ( ). .
int, ui nt, long ulong.
3.5.
3 . 5 .
using System; namespace ConsoleApplicationl { class Classl
{ static void MainO
(=, ==, ! = ) .
, true false. . 3.7
3 . 7 .
== true, , fal se
! = true, , fal se
< true, , fal se
> true, , fal se
= true, , fal se
1 , 5 , int ui nt, 6 , long ulong.
byte = 3, b = 9; sbyte = 9, d = -9; Console.WriteLineC a 1 ) Console.WriteLineC a 2 ) Console.WriteLineC b 1 ) Console.WriteLineC 1 ) Console.WriteLineC d 1 )
// 6 // 12 // 4 // 4 // -5
55
,
, .
. , NaN, , !=, false ( != true). , ! = ! ( == ) , NaN, , = . ,
NaN, < false, ! ( >= ) true. :
+0 -0 ;
- - ;
+ + .
(&, |, ) .
( , . . ) . int, ui nt, long ulong.
, ( &), 1 , 1.
, ( | ) , 1 , 1.
( ) 1 , 1.
3.6.
3 . 6 .
using System: namespace ConsoleApplicationl { class Classl
{ static void MainO
Console.WriteLineC 6 & 5) Console.WriteLineC 6 | 5 ) Console.WriteLineC 6 A 5 )
// 4 // 7 // 3
56 3. , !
(&&) (11) one . true
false. .
true, ;
true. true
true.
,
, . , \ false, false , .
3.7.
3 . 7 .
using System; namespace ConsoleApplicationl { class Classl
{ static void MainO {
Console WriteLineC true & & true ) ; // true Console WriteLineC true & & false ) ; // false Console WriteLineC true 1 1 true ); // true Console WriteLineC true J 1 false ) ; // true
} }
}
(? :) , .
_1 ? _2 :
,
. true, TI ,
. , .
.
:
, 1;
, 2 3, , ;
.
57
, 3 2, , 2;
.
if ( ) . 3.8.
3 . 8 .
using System; namespace ConsoleApplicationl { class Classl
{ static void MainO {
int a = 11. b = 4; int max = b > a ? b : a; Console.WriteLineC max ); // 11
}
: ,
1, , 1. : i = ( i < n) ? i + 1: 1; , .
, ? b : ? d : a T b ^ c d ^ ) 1 .
,
(=, +=, -=, *- . . ) 2. .
(=):
=
:
,
, . ,
, , . :
58 3. , ^
:
= b + / 2: b = : = :
= 1; = + 0.5:
: = = !
, , .
.
, { . , >
.
( = + 0.5). , , . 0.5, ./: , ,
, .
.
. ,
,
, :
:= ;
] - (. . 45). , ] . .
( +=, *=, /= . .) , , . ! , j , += b 6oi = + .
,
.
,
i nt. , , , b byte, += b ] : += (byte). .
59
, ,
, ( = = = ( = ) ) . .
,
, .
.
: , .
, ,
# . 11, .
-
.
, , .
,
11.
# , , . .
# Console, System. Write WriteLine . ,
3.1. 3.9.
3 . 9 . using System: namespace ConsoleApplicationl { class Classl
{ static void MainO {
int i = 3; double = 4.12: decimal d = 600m: string s = "";
Console WriteLineC "i - " + i ) ; // 1 Console WriteLineC "y = {0} \nd - { 1 } " . y. d ) ; // 2 Console WriteLineC "s = " + s ) : // 3
} }
}
60 3. , :
:
1 = 3 = 4,12 d = 600 s =
WriteLine . ,
Console Write WriteLine .
,
. , ,
. Consol , ,
3.9 ) .
1 3. . WriteLine , , , , . ) , :
, ,
+.
, .
# ToStringO. , :
Console.WriteLineC "i = " + i.ToStringO );
2 . WriteLine, . , :
, ,
, ( 2) ,
:
( ) , ! ( d) . .
(. . 146]
(\) ( \ t ) .
61
. Console , ,
.
:
1. , , .
2. .
Convert, System, Parse, . 3.10 .
3 . 1 0 .
using System; namespace ConsoleApplicationl
class Classl { static void MainO
Console.WriteLineC " string s = Console.ReadLineO: Console.WriteLineC "s = " + s );
) ; // 1
Console.WriteLineC " " ); char = (char)Console.ReadO; // 2 Console.ReadLineC); // 3 Console.WriteLineC "c = " + ); string buf; // - Console.WriteLineC " " ); buf = Consol.ReadLineC); int i = Convert.ToInt32( buf ); 1/4 Console.WriteLineC i ) ;
Console.WriteLineC " " ); buf = Console.ReadLineO: double x = Convert.ToDouble buf ); // Console.WriteLineC x );
// 5
Console.WriteLineC " " ); buf = Console.ReadLineO; double - double.ParseC buf ); // Console.WriteLineC );
//
Console.WriteLineC " " ); buf - Console.ReadLineO; decimal z = decimal.ParseC buf ); // Console.WriteLineC z );
// 7
} }
62 3. ,
.
1. ,
.
Read, ( 2) . int, , - 1 , (, Enter). int, a char, int char , ,
(. . 49).
2 3, . ,
. ,
, .
Enter . Read, ReadLine, , , .
4 5 Convert, 6 7 Parse Double Decimal .NET, # double decimal.
, . ,
, . ,
, 1,95-8.
,
1.
-
, .
2.
.
, ,
, ,
.
1 , .
,
.
2 !
63
,
.
11,
. 3.11 3.9, , output.txt. ,
, ...\ConsoleApplication1\bin\Debug.
3 . 1 1 .
using System; using System.10: namespace ConsoleApplicationl { class Classl
{ static void MainO {
StreamWriter f = new StreamWriterC "output.txt" ) int i = 3; double = 4.12; decimal d = 600m; string s = "";
f.WriteLineC "i = " + i ); / /3 f.WriteLineC "y = {0} \nd = { 1 } " . y, d ); / / 4 f.WriteLineC "s = " + s ): l i b
f .CloseO; // 6
} }
} , :
1. ,
( 1). 2. ( 2) .
3. - ( 3-5) .
4. ( 6 ) .
, . .
. 3.12 , 3.10, input.txt, D:\C#. , .
// 1
// 2
64 3. ,
,
Visual Studio.NET. File New File... Text File.
3 . 1 2 .
using System; using System.10; namespace ConsoleApplicationl { class Classl
{ static void MainO {
StreamReader f = new StreamReader( "d:\\C#\\input.txt" ); string s = f.ReadLineC); Console.WriteLineC "s = " + s );
char = (char)f.ReadO; f.ReadLineC); Console.WriteLineC "c = " + );
string buf; buf = f.ReadLineC); int i = Convert.ToInt32( buf ); Console.WriteLineC i ) ;
buf = f.ReadLineC); double x = Convert.ToDoubleC buf ); Console.WriteLineC x );
buf = f.ReadLineO; double = double.ParseC buf ); Console.WriteLineC );
buf = f.ReadLineO; decimal z = decimal.ParseC buf ); Console.WriteLineC z ); f.CloseC);
Math ,
. Math, System. :
: Sin, Cos, Tan;
: ASin, ACos, ATan, ATan2;
65
: Tanh, Sinh, Cosh;
: Exp, Log, LoglO;
( ), , : Abs, Sqrt, Sign;
: Ceiling, Floor, Round;
, : Mi n, Max;
, : Pow, IEEEReminder;
: BigMul;
: Di vRem.
, : .
. 3.8.
3 . 8 . Math
Abs
1 |x| Abs(x) Acos 2 double Acos(double x) Asin
double Asin(double x) Atan
double Atan(double x) Atan2
double Atan2(double x, double ) ,
BigMul long BigMul (int , int )
Ceiling
double Ceiling(double )
Cos double Cos(double )
Cosh double CoshCdouble )
DivRem
DivRem(x, , rem) E.
( ) double 2,71828182845905
Exp double ex ()
Floor
double Floor(double )
IEEERemainder double IEEERemainder(double ,
double ) Log double \ogex
Log(x) &
, .
2 .
66 3. ,
3.13 Math. .
3 . 1 3 . Math
u s i n g System; namespace C o n s o l e A p p l i c a t i o n l { c l a s s C l a s s l
{ s t a t i c v o i d M a i n O {
C o n s o l e . W r i t e ( " x: " ); s t r i n g b u f = C o n s o l e . R e a d L i n e ( ) ; d o u b l e x = d o u b l e . P a r s e ( b u f ); C o n s o l e . W r i t e L i n e ( " s i n ; " + M a t h . S i n ( x ) ) ;
C o n s o l e . W r i t e ( " ; " ) ; b u f = C o n s o l e . R e a d L i n e ( ) ; d o u b l e = d o u b l e . P a r s e b u f ); C o n s o l e . W r i t e L i n e ( " : " + M a t h . M a x ( x , y ) ) ;
} }
}
= Vtv*-'2" + 2 tg2a + l,6103 log10x2 .
67
, -
1 . ->i, d o u b l e . 3.14.
1 3 . 1 4 .
i n g System: mespace C o n s o l e A p p l i c a t i o n l
c l a s s C l a s s l { s t a t i c v o i d M a i n ( )
( s t r i n g b u f :
C o n s o l e . W r i t e L i n e C " x" ) : b u f = C o n s o l e . R e a d L i n e O : d o u b l e x = C o n v e r t . T o D o u b l e ( b u f ):
C o n s o l e . W r i t e L i n e C " a i f a " ) ; b u f = C o n s o l e . R e a d L i n e O : d o u b l e a = double.ParseC b u f );
d o u b l e = M a t h . S q r t ( M a t h . P I * x ) -Math.ExpC 0 . 2 * M a t h . S q r t ( a ) ) + 2 * Math.TanC 2 * a ) + 1.6e3 * M a t h . L o g ]0 i ' Math.PowCx. 2) );
Console.WriteLineC " x {0} a!fa - { ! ) " , x. a }; C o n s o l e . W r i t e L i n e C " = " + );
} }
1, , : # , -
. , -
. .
1 , , .
.
, .
.
, , .
1 . , 1 , .
68
: ,
. ( , ). ,
, - i .
, ,
, ,
t r u e f a l s e . , b o o l f l a g , bool empty.
# , , Camel (. , . 24).
,
. ,
, .
.
.
, c o n s t . , ,
. , 0 1.
,
.
( , ). ,
.
.
.
,
,
.
.
, .
,
, ,
.
4
,
. # .
#, .
,
,
.
, 60-70- , ,
,
.
-
,
.
# - ,
, .
.
.
.
,
,
. . , ,
, .
4.
,
, ,
, .
: ( , , ). :
1-; //' * b + ; // fu!) ( i. k ): // w h i l e ( t r u e ) : // ()
, , ,
.
, ,
. .
if switch
. if , switch .
if if . . 4.1.
^ . 4 . 1 .
:
if ( ) _1; [ else ; ]
71
" . t r j t , - , - .
, . e l s e .
1 , ,
.
7. - , - , ,
. ,
( ). , , , .
:
< 0 ) b = 1 : ,7 1 < b && ( > d 11 == 0 ) ) b++: e l s e { b *= : = 0: } // 2 < b ) if ( < ) m = a: e l s e = :
if ( b < ) m = b: e l s e m = : /.' 3 - b > a ) max = b: e l s e max - a: // 4
} 1 e l s e . , , -
.
, -
. , 2 , < b . , ,
. .
3 . , e l s e -i O N i y 1 f .
\, 4 ( ), ) , :
; . = b > a ? b : a ;
. , 0 < < 1, ,
. : i f ( 0 < X && < 1)...
3 , . 4.2.
72 4.
/ \
\
\ \
. 4 . 2 .
, , .
,
,
. 4.1.
4 . 1 .
using System; namespace ConsoleApplicationl { class Classl
{ static void MainO
Console.WriteLineC " x" ); string buf = Console.ReadLineO; double x = Convert.ToDoubleC buf );
Console.WriteLineC " " ); buf = Console.ReadLineO: double = double.ParseC buf );
int kol = 0: i f ( x * x + y * y < l ) k o l = 2 ; else if ( x * x + * < 4 ) kol = 1; Console.WriteLineC " = {0} ", kol );
,
, if:
73
-f ( temp < 1 ) k o l = 2; T o n s o l e . W r i t e L i n e C - " = { 0 } " , k o l ) ;
, i f
,
, .
,
.
:
-""oat . ; ... * == b ) ... // ! -
f ( Math.AbsCa - b) < i e - 6 ) ... // !
, ,
. S i n g l e Double E p s i l o n ( , 1.0 + E p s i l o n != 1.0) .
switch s w i t c h () .
. 4.3.
switch
case 1
case 2
1
2
case
default
>
. 4 . 3 . switch
:
switch ){ case __1: [ __1 ] case __2: [ __2 ]
case : [ ] [ default: ]
74 4. |
.
( c h a r ) 1. ,
, .
] . , ,
d e f a u l t ( ? s w i t c h ).
, b r e a k , g o t o r e t u r n :
break s w i t c h , f o r , w h i l e do (. break, . 84);
g o t o , case s w i t c h (. goto, . 83);
r e t u r n , ( return, . 87).
g o t o ,
, .
4.2 , .
4 . 2 .
u s i n g System; namespace C o n s o l e A p p l i c a t i o n ! : c l a s s C l a s s l
{ s t a t i c v o i d M a i n ( ) i
s t r i n g b u f ; d o u b l e . b. r e s ,
Cor isole. W r i t p i j n e ' " : " ) : buf" = Console ReadLineC): a = d o u b l e . P a r s e f b u f i ;
C o n s o l e . W r i i e L i n e C " " ); c h a r op - ( c h a r ) C o n s o l e . R e a d O : C o n s o l e . R e a c i L m e c ) ;
C o n s o l e . W r i t e L i n e C " :" ) . b u f = C o n s o l e . R e a d L i n e O ; b = d o u b l e . P a r s e C b u f );
,
, .
75
bool ok = t r u e : s w i t c h ( o p ) I i
case ' + 1 : res = a + b; b r e a k : case ' - 1 : r e s = a - b: b r e a k ; case ' * ' : res - a * b: b r e a k : case ' / ' : res = a / b: b r e a k : d e f a u l t : res = d o u b l e . N a N : ok = f a l s e : b r e a k :
i i f ( o k ) C o n s o l e . W r i t e L i n e C ". " + r e s ) : e l s e Console W r i t e L i n e C " "
d e f a u l t , , .
.
switch if ,
, ,
. ,
.
, .
# : whi 1, repeat, for foreach. .
, , .
: ,
(, 4.4). . ,
, .
^ ( , : . 4.4, ), ( , . 4.4, 6). .
, , .
,
, .
76 4.
. 4 . 4 .
,
,
, , .
, .
. .
,
1.
, .
, .
break, continue, return goto (. , . 83). ( ).
while :
while ( ) . ,
.
true, () . , fa 1 se. .
,
.
77
.
false, .
,
.
, ,
,
:
't, < 0 "
= I tx, 0 < 10 2, > 10
, ,
dX t. . , :
.
:
1. .
2. , , .
3. .
4. .
5. , 2-4, .
2-4 , . 4.3. .
.
. , ,
, .
4 . 3 . , while
_5"ng System; -;~iespace Consol eApplicationl
class Classl { static void MainO
{ double Xn = -2, Xk = 12, dX = 2, t = 2. y; Console.WriteLineC "| x | |" ); //
double x = Xn; // 1 while ( x
78 4. /
4 , 3 () i f ( X >= X < 10 ) = t * ; i f ( >= 10 ) = 2 * t ; C o n s o l e . W r i t e L i n e C "| { 0 . 6 } j { 1 . 6 += dX:
{ 1 . 6 } I", x . ) :
// 2 11 2 //' 3 // 4
, ( ( 6 ). . .
, ,
. ,
4. t c k j
,
. ,
Ctrl+Break, :
;
;
.
1
w h i l e ( t r u e ) ; - '
. 4.4 ] .
4 . 4 .
u s i n g System; namespace C o n s o l 1 i c a t i o n l { c l a s s C l a s s !
{ s t a t i c v o i d M a i n O
s t r i n g b u f : w h i l e ( t r u e ) {
C o n s o l e . W r i t e L i n e C "1 - _ 1 . 2 - __2. 3 - " ): b u f = C o n s o l e . R e a d L i n e O : s w i t c h b u f )
case " 1 " " : // 1 C o n s o l e . W r i t e L i n e C " - _ 1 " ) :
79
b r e a k :
case "2 C o n s o l e . W r i t e L i n e C b r e a k ;
// n y i K i a 2 ne( "oi;idaxa - _ 2 " ),
case " 3 " r e t u r n ;
d e f a u l t : C o n s o l e . W r i t e L i n e C " !" b r e a k ;
do , . 4.4, ,
do while ;
, ,
( b o o l ) . , .
, f a l s e - .
,
, ,
. ,
.
, , 4.5.
4 . 5 .
. s i n g System: -amespace C o n s o l I i c a t i o n i
c l a s s C l a s s l { s t a t i c v o i d M a i n O
c h a r answer: do
C o n s o l e . W r i t e L i n e C " , a?" ) : answer = ( c h a r ) Console.Read ; C o n s o l e . R e a d L i n e O ;
w h l i e ( answer != ' );
80 4.
,
cos(x) = 0,0001.
, ,
1. .
. ,
(, ). . , ,
, , .
,
.
( 4.6) , . ,
, ,
( ) ,
2.
4 . 6 .
using System; namespace ConsoleApplicationi { class Classl
{ static void MainO
x = ( lef t + right ) / 2; if ( ( Math.Cos(x) - x ) * ( Math.Cos(left) - lef t ) < 0 )
right = x; else left = x;
} while ( Math.Abs( right - lef t ) < le-4 ); Console.WriteLineC " " + x );
double x, lef t = 0, right = 1; do
.
81
for :
for ( ; ; ) ;
, ,
.
, , :
- ( i n t i - 0 . j 2 0 : . . . nt k. m; "" ( k = 1 , m = 0: . . .
, ,
. .
bool : t r u e , . .
.
, :
-~: ( i n t i = 0, j = 2 0 ; i < 5 && j > 10; i + + , j - - ) ...
.
f o r ( !). 1 100:
" t 5 - 0: - V ( i n t i - 1 , i < - 100; i + + ) s += i :
4.7 ,
4.3.
4 . 7 . , for
. 51n g System: -amespace C o n s o l e A p p l i c a t i o n l
c l a s s C l a s s l { s t a t i c v o i d M a i n O
d o u b l e Xn = - 2 , Xk = 12. dX = 2. t = 2, y; C o n s o l e . W r i t e L i n e C "| x | |"; //
f o r ( d o u b l e x = Xn; x = 0 && x < 10 ) = t * x ;
// 2 // 2
&
82 4.
4 . 7 ()
w h i l e f o r . , :
f o r :
f o r ( b l ; 2: ) ;
whi 1:
:
w h i l e ( 2 ) {
;
}
foreach f o r e a c h , , .
, , 1
foreach (. . 136).
,
.
do w h i l e , , , .
w h i l e , , ,
.
i f ( >= 10 ) = 2 * t ; / / 2 C o n s o l e . W r i t e L i n e C "| { 0 , 6 } | { 1 . 6 } |". , ): // 3
} }
} } ,
. . ,
, ,
4.3, ,
.
83
f o r e a c h .
f o r . ,
,
(, 1). .
, :
, ,
,
( ); , ,
;
( ); , , ,
, .
# ,
:
g o t o ;
b r e a k ;
c o n t i n u e ;
r e t u r n ;
t h r o w .
,
, .
.
, t h r o w . 93.
goto g o t o :
goto ; goto case _; goto default;
: ;
84 4.
goto . , ,
. ,
.
:
;
(, -
).
, goto .
,
, , .
goto switch. goto case _ , goto default default. , # ,
, .
break break , ,
break.
ch ( ) = 10~6
2
4
6
2"
= 1 + + + + . . . + + . . . 2 ! 4 ! 6! 2!
| \ < . , .
.
| \ > . : ,
. ,
.
,
, . ,
- , -
85
( , ,
).
.
break.
,
, ,
, , .
.
,
,
, ,
1.
: ,
,
.
:
Cn+i = C,i ' ^
.
+1,
:
, _ 2\ -2 = 2
~
"
2- (2( + 1 ) ) ! (2 + 1)(2 + 2 ) '
4.8 .
4 . 8 .
j s i n g System: 'aniespace Conso 1 eAppl i c a t i o n l { c l a s s C l a s s !
{ s t a t i c v o i d M a i n O {
d o u b l e e = l e-6 ; c o n s t i n t M a x l t e r = 500; // C o n s o l e . W r i t e L i n e C " : " ) ; s t r i n g b u f = C o n s o l e . R e a d L i n e O ; d o u b l e x = Convert .ToDoubleC b u f );
bool done = t r u e ; // d o u b l e ch = 1, = c h : f o r ( i n t n = 0: M a t h . A b s ( c h ) > e; n++ ) {
, .
4.
4 . 8 () c h * = x * x / ( 2 * n + l ) / ( 2 * n + 2 ) ; += c h ; // if ( n > M a x l t e r ) { done = f a l s e ; b r e a k : }
} if ( done ) C o n s o l e . W r i t e L i n e C " - " + ); e l s e C o n s o l e . W r i t e L i n e C " " ) ;
} }
} ,
,
.
, , d o u b l e ,
- , . -
.
"!:
( );
( ) ; (
); (
). , ,
,
,
! , , .
continue c o n t i n u e , ,
.
4 . 8 c o n t i n u e :
f o r i n t n = 0; M a t h . A b s ( c h ) > e; n++ ) {
ch *= x * x / ( 2 * n + 1 ) / ( 2 * n + 2 ); += c h ; if ( n
87
return return
. :
return [ ];
.
void, .
, ,
. ,
.
11 , , , ,
.
, .
.
- .
# . ,
,
, , .
,
( ). ,
- . .
. 4.5.
. 4 . 5 .
88 4.
,
,
, ,
, (. 4.6).
!
1 J ;__ ! i J j j j j ! ! j _ _ ^nrm '_ !
. 4 . 6 .
. ( , ), .
; # ( ). , .
, ,
. ,
, , ,
.
89
# , ( ), . (. . 46), .
, ,
,
,
.
. # .
# , , Ctrl+C. ,
.
, ,
.
- .
.
. , ,
, , ,
, ,
.
, .
,
, .
,
, , ,
,
.
,
t h r o w . . 4.1 , .
System. E x c e p t i o n , , S y s t e m E x c e p t i o n .
t r y .
90 4.
4 . 1 .
Ari thmeti cExcepti on ( DivideBeZeroException OverFlowException)
ArrayTypeMi smatchExcepti on
Di vi deByZeroExcepti on
FormatException
IndexOutOfRangeException
InvalidCastException
OutOfMemoryExcepti on
OverFlowException
StackOverR owExcepti on
try try :
,
try. . , ,
;
catch, , ;
f inal ly , .
try:
try [ catch ] [ f inally ] catch, finally, .
, .
1. . , , . ,
try, , .
2. , , .
3. final 1, ( , ).
91
4. ,
. .
.
,
, ,
.
t r y . c a t c h , .
. c a t c h , ,
.
. :
catch( ) { ... /* */ } catch( ) {.../* */ } catch { ... /* */ }
,
- ,
.
,
.
.
, ,
( ) . :
-' { . . . //
c a t c h ( O v e r t l o w E x c e p t i o n ) { // O v e r f l o w E x c e p t i o n ()
c a t c h ( D iv ideByZeroExcept on ) { . . . .// D i v i d e B y Z e r o E x c e p t i o n ( 0)
c a t c h { /7
,
.
92
, ,
finally ( ), , try. , ,
, , ,
, .
4.9 , .
, .
, ,
, .
.
4 . 9 .
using System; namespace ConsoleApplicationi { class Program
{ static void MainO {
string buf; double u, i, r; try {
Console.WriteLineC " :" ); buf = Console.ReadLineO; u = double.ParseC buf );
Console.WriteLineC " :" ): buf = Console.ReadLineO; r- = double. ParseC buf );
i = u / r; Console.WriteLineC " - " + i );
} catch ( FormatException ) {
Console.WriteLineC " !" ); } catch //
{ Console.WriteLineC " " );
} }
} }
93
try . , try catch, , . .
. , ,
,
,
. throw.
throw ,
# , . throw , . ,
System.Exception. .
throw , :
throw [ ];
catch . , throw, , :
:hrow new DivideByZeroExceptionO;
throw , 0 new.
.
, , throw, , catch, .
, # :
jsing System; class Test
static void F() { try {
GO; // ,
} catch ( Exception ) {
Console.WriteLineC "Exception in F: " + e.Message ); e = new Exception( "F" ); throw; //
}
4.
} static void GO {
throw new Exception( "G" ); // } static void MainO {
try { FO;
} catch ( Exception e ) {
Console.WriteLineC "Exception in Main: " + e.Message );
F ,
, Message ( Exception ). .
Exception , , , ,
, :
Exception in F: G Exception in Main: G
throw :
throw e;
,
, :
Exception in F: G Exception in Main: F
,
.
Exception Exception , . . 4.2.
4 . 2 . System.Exception
HelpLink U R L
Message . .
Source ,
95
S T T r a c e , .
" r Except i o n ,
~ r . r g e t . S i t e ,
checked unchecked 3, , , checked unchecked, , ,
, , :
- checked (b + ) ; // ( ) , ^ c h e c k e d { // ( )
- b + :
, .
#
, , .
,
. ,
,
.
, .
, ,
. ,
,
.
. , ,
.
.
(, -), .
96 4.
-, ,
(, , , , 0 100 1. .) . :
, ,
, .
1
:
,
. , .
. -
,
, .
, , ,
.
, .
. ,
switch, i f . for foreach. goto , , switch ,
, break return.
. do ,
, , .
,
.
. 82.
( , ),
. .
if , , .
true false:
1 ,
,
.
bool busy:
if ( busy == true ) { ... } if ( busy == false ) { ... }
// ! if ( busy ) // ! if ( !busy )
97
. :
i f ( < b ) - 1 ; else if ( > b ) = 2;
else if ( == b ) = 3:
:
i f ( < b ) = 1 : else if ( > b ) = 2: else = 3;
:
= 3 : i f ( < b ) = 1 ; i f ( > b ) - 2;
i f , else :
if ( i > 0 ) break: // i
98 4.
, ,
, : !. , , <
.
,
.
- ( - ) , .
, ,
, , .
,
.
,
.
, , , -
,
, .
.
, . # , 15, , , .
-
1 (
,
, ). , f . ! , ,
, .
3-5 , .
, , :
s t r i n g b u t = "qwerty"; d o u b l e ex = 3 . 1 2 3 4 ; i n t number = 12; b y t e z = 0 ;
if ( done ) C o n s o l e . W r i t e L i n e ! " - " + ); e l s e C o n s o l e . W r i t e L i n e C " " ) :
i f ( x >= 0 && x < 10 ) = t * x ; e l s e if ( x >= 10 ) = 2 * t: e l s e = x;
1 , .
99
,
, .
:
II , , // // , { /*
*/ }
/ / -
. ,
:
f = a + b ; // ! f - + ;
, :
w h i l e ( t r u e ) {
w h i l e ( < ) {
f o r ( i = 0; i < 10; ++i ) {
f o r ( j = 0; j < 10; + + j ) {
// } // end f o r ( j = 0; j < 10; + + j ) } // end f o r ( i = 0; i < 10; ++i )
} II end w h i l e ( x < ) } // end w h i l e ( t r u e )
C# , , ,
, , ,
. ,
,
1.
,
, [2], [6] .
,
.
5 :
1 (. . 13)
(. . 17). , , - Main. ,
. , , ,
.
, .
, ,
. ,
.
c l a s s , , - , . ,
() , :
[ ] [ ] class [ : ] -
, c l a s s , . # . , .
, .
, :
c l a s s Demo {}
.
, ,
12.
1 , .
: 101
,
.
. 5.1. .
.
.
5 . 1 .
1 new . .
,
8 (. . 175) ") public
3 protected .
L -i internal () ' 5 protected internal
() 6 private .
,
t abstract . , 8 (. . 181)
s sealed . , 8 (. . 182)
9 static . 2.0. (. . 114)
2-6 . , .
,
, .
,
( ). : public internal. , ,
internal.
,
,
, , .
1 . 9.
110 5. :
5 . 3 () s h o r t t l = 3 . t 2 = 4 : i n t = Max( t l , tZ ) : C o n s o l e . W r i t e L i n e C ):
i n t z = Max( a + t l . t l / 2 C o n s o l e . W r i t e L i n e C z );
b ) :
// Max // : 4
// Max // : 5
2
,
. . Main .
, , ,
- .
,
, ,
.
.
:
, .
: .
,
.
. , .
( ) ,
, .
#
:
-;
- ref;
o u t :
- params.
. ,
-. -
, :
p u b l i c i n t C a l c u l a t e ( i n t a . r e f i n t b . o u t i n t c , params i n t f ] d ) ...
200 9.
9 . 1 ()
{ Monster temp = (Monster) obj; if ( this.health > temp.health ) return 1; if ( this.health < temp.health ) return -1; return 0;
} string name; int health, ammo;
} class CI ass 1 { static void MainO
{ const int n = 3; Monster[] stado = new Monster[n];
stado[0] = new Monster( 50, 50, "" ); stado[l] = new Monster( 80, 80, "" ); stado[2] = new Monster( 40, 10, "" ) ' ;
Array.Sort( stado ); // foreach ( Monster el em in stado ) elem.Passport();
} }
} :
Monster health = 40 ammo = 10 Monster health = 50 ammo = 50 Monster health = 80 ammo = 80
,
.
. # I Comparer, .
( IComparer) IComparer System. Col lections. CompareTo, , :
interface IComparer
{ int Compare ( object obi, object ob2 )
.NET 201
,
,
.
( ).
( Name, SortByName) ( Ammo, EortByAmmo) 9.2. , Monster .
9 . 2 .
. s i n g System;
. s i n g System.Col l e c t i o n s ; 'amespace C o n s o l e A p p l i c a t i o n i
c l a s s Monster {
p u b l i c M o n s t e r ( i n t h e a l t h , i n t ammo, s t r i n g name ) {
t h i s . h e a l t h = h e a l t h ; t h i s . a m m o = ammo; t h i s . n a m e = name;
}
p u b l i c i n t Ammo {
g e t { r e t u r n ammo; } s e t
{ i f ( v a l u e > 0) ammo = v a l u e ; e l s e ammo = 0;
} } p u b l i c s t r i n g Name {
g e t { r e t u r n name; } } v i r t u a l p u b l i c v o i d P a s s p o r t O {
C o n s o l e . W r i t e L i n e C "Monster { 0 } \ t h e a l t h = { 1 } ammo = { 2 } " , name, h e a l t h , ammo );
} p u b l i c c l a s s SortByName : IComparer // {
i n t IComparer.Compare( o b j e c t o b i , o b j e c t ob2 ) ^
202 9.
9 . 2 ()
Monster ml = ( M o n s t e r ) o b i : Monster m2 = ( M o n s t e r ) o b 2 ; r e t u r n S t r i n g . C o m p a r e ( m1.Name, m2.Name );
i n t . IComparer.Compare( o b j e c t o b i , o b j e c t ob2 ) {
Monster ml - ( M o n s t e r ) o b i : Monster ni2 - ( M o n s t e r ) o b 2 ; if ( ml.Ammo > m2.Ammo ) r e t u r n 1; i f ( ml.Ammo < m2.Ammo ) r e t u r n - 1 ; r e t u r n 0 .
c o n s t i n t n = 3 . Monster3 s t a d o new M o n s t e r ] ,
s t a d o [ 0 ] = new M o n s t e r ( 50. 5 0 . "" ); s t a d o [ l ] = new M o n s t e r ( 8 0 . 8 0 . "" ); s t a d o [ 2 ] = new M o n s t e r ( 4 0 . 10. "" ):
C o n s o l e . W r i t e L i n e ( " :" ) ; A r r a y . S o r t ( s t a d o , new Monster SortByName j ; f o r e a c h ( Monster el em in s t a d o ) el em. P a s s p o r t ' , ' ) .
C o n s o l e . W r i t e L i n e ( " " %:: A r r a y . S o r t ( s t a d o . new Monster.SortByAmmoO ); f o r e a c h ( M o n s t e r e lem in s t a d o ) e l e m . P a s s p o r t ( ) ;
p u b l i c c l a s s SortByAmmo : IComparer
s t r i n g name: m t h e a l t h , ammo;
Class C lass1 { s t a t i c void Mam()
:
:
Monster h e a l t h 50 ammo Monster h e a l t h = 40 ammo Monster h e a l t h = 80 ammo
50 10 30
.NET 203
:
Monster h e a l t h Monster h e a l t h Monster h e a l t h
h e a l t h = 40 ammo h e a l t h = 50 ammo h e a l t h = 80 ammo
10 50 80
IComparable, .
, .
: < >, =, == ! =. ,
Is.
IComparable, fcqual s GetHashCode. o b j e c t . object* (. . 183).
9.3 Monster .
h e a l t h , ,
9 . 3 .
u s i n g System; namespace C o n s o l e A p p l i c a t i o n i
c l a s s Monster : IComparable {
p u b l i c M o n s t e r ( i n t h e a l t h , i n t ammo, s t r i n g name ) {
t h i s . h e a l t h = h e a l t h ; t h i s . a m m o = ammo; t h i s . n a m e = name;
p u b l i c o v e r r i d e b o o l E q u a l s ( o b j e c t o b j ) {
i f ( o b j = = n u l l I I G e t T y p e O ! = o b j . G e t T y p e ( ) ) r e t u r n f a l s e ;
Monster temp = ( M o n s t e r ) o b j ; r e t u r n h e a l t h == t e m p . h e a l t h &&
ammo == temp.ammo && name == temp.name;
p u b l i c o v e r r i d e i n t GetHashCode() iP
204 9.
9 . 3 ()
{ return name.GetHashCode();
} public static bool operator == ( Monster a. Monster b ) {
return a.Equals( b ); }
// : // public static bool operator == ( Monster a, Monster b ) // { // return ( a.CompareTo( b ) == 0 j; // }
public static bool operator != ( Monster a, Monster b ) {
return ! a.Equals( b ); }
// : // public static bool operator != ( Monster a, Monster b ) // { // return ( a.CompareTo( b ) !- 0 ); // }
public static bool operator < ( Monster a, Monster b )
return ( a.CompareTo( b ) < 0 );
public static bool operator > ( Monster a. Monster b )
return ( a.CompareTo( b ) > 0 );
public static bool operator = 0 );
public int CompareTo( object obj )
.NET 205
Monster temp = ( M o n s t e r ) o b j ; i f ( t h i s . h e a l t h > t e m p . h e a l t h ) r e t u r n 1 ; i f ( t h i s . h e a l t h < t e m p . h e a l t h ) r e t u r n - 1 : r e t u r n 0 ;
} s t r i n g name; i n t h e a l t h , ammo;
} c l a s s C l a s s l { s t a t i c v o i d M a i n ( )
{ Monster = new M o n s t e r ( 7 0 , 8 0 , "" ); Monster = new M o n s t e r ( 8 0 , 8 0 , "" );
if ( > ) C o n s o l e . W r i t e L i n e ( " " ); e l s e if ( = ) C o n s o l e . W r i t e L i n e ( " " ); e l s e C o n s o l e . W r i t e L i n e ( " " );
} }
:
( ICIoneable) .
. ,
, (. 9.1, ). ,
w emberwiseClone, o b j e c t . , ,
, (. 9.1, ). .
. 9 . 1 .
206 9.
, , ,
, . . (. 9.1, ). ,
.
, ,
I C l o n e a b l e 1 9.4 Monster MemberwiseClone, IC loneab ' r .
, MemberwiseClone . ,
o b j e c t ( p r o t e c t e d ) .
9 . 4 .
u s i n g System: namespace C o n s o l e A p p l I c a t i o n i {
c l a s s Monster : I C l o n e a b l e {
p u b l i c M o n s t e r ( i n t h e a l t h , i n t ammo, s t r i n g name ) {
t h i s . h e a l t h = h e a l t h : t h i s . a m m o = ammo: t h i s . n a m e = name;
} p u b l i c Monster S h a l l o w C l o n e ( ) // {
r e t u r n ( M o n s t e r ) t h i s , M e m b e r w i s e C l o n e ( ) ; } p u b l i c o b j e c t C l o n e O / / {
r e t u r n new M o n s t e r ( t h i s . h e a l t h , t h i s . a m m o . " " + t h i s . n a m e ); }
v i r t u a l p u b l i c v o i d P a s s p o r t O {
C o n s o l e . W r i t e L i n e C "Monster { 0 } \t h e a l t h = { 1 } ammo = {2}", name, h e a l t h , ammo );
}
s t r i n g name; i n t h e a l t h , ammo;
}
c l a s s C l a s s l { s t a t i c v o i d M a i n O
.NET
{ Monster = new Monster( 70. 80. "" ); Monster X = ; Monster Y = .ShallowClone(); Monster Z = (51).CIone();
} }
} X , . , , .
Y Z, , .
( I Enumerable)
foreach . .NET , IEnumerable IEnumerator. foreach . ,
.
IEnumerable () GetEnuraerator, IEnumerator (), .
IEnumerator :
Current, ;
MoveNext, ;
Reset, .
foreach , .
, ,
foreach, : GetEnumerator, Current, MoveNext Reset. , , ,
, MoveNext 1 , Current . .
, , 2.0 , .
,
. foreach
207
208 9.
, .
yield.
( 9.5). , Monster,
.
.
9 . 5 .
using System; using System.Col lections; namespace ConsoleApplicationi
{ class Monster { . . . } class Daemon { . . . } class Stado ; IEnumerable // 1
{ private Monster[] mas; private int n;
public StadoO {
mas = new Monster[10]; n = 0;
}
public IEnumerator GetEnumeratoK) {
for ( int i = 0; i < n; ++i ) yield return mas[i]; // 2 }
public void Add( Monster m ) {
if ( n >= 10 ) return; mas[n] = m: ++n;
class Classl { static void MainO
{ Stado s = new StadoO; s.Add( new Monster() ); s.Add( new Monster("") ): s.Add( new Daemon );
foreach ( Monster m in s ) m.Passport();
.NET 209
, 2.0 , , IEnumerable ( 1), ( 2) . MoveNext Current IEnumerator.
, 9.5, . foreach ,
.
, , , ,
, ,
.
9.6 , .
9 . 6 .
using System; using System.Collections; namespace ConsoleApplicationi
{ class Num : IEnumerable
{ public IEnumerator GetEnumerator() {
yield return "one"; yield return "two"; yield return "three"; yield return "oops";
} } class Classl { static void MainO
{ foreach ( string s in new NumO ) Console.WriteLineC s );
} :
one
two
three oops
( 1 5 ) :
using System; using System.Col lections;
111
- , 7 (. , 154), .
-
- :
, - :
. o i d ( i n t ) . .
,
(. . : ,
, ,
. ,
, , , .
. ,
- , , . ,
,
, .
, ,
, , , -
.
*.
,
:
- n t X = 1 ; ; ; y t e = 1 : . s h o r t = 1:
, ,
:
; ) : ( ) : ( ) : ( 200 ) ; ( / 4 + 1 ) ;
-
,
, , ,
,
return. ,
, . ,
- (. . 4 5 ) .
210 9.
namespace ConsoleApplicationi { class Classl
{ public static IEnumerable Count( int from, int to ) {
from = 1; while ( from = 0; --i ) yield return mas[i]:
public IEnumerable MonstersOnlyO //
112
- .
-.
- r e f :
ref
, - :
v o i d ( r e f i n t )
,
, , .
, - ( ) . , , ,
,
.
-
.
r e f .
,
.
- -
( 5.4).
%
5 . 4 . - -
u s i n g System; namespace C o n s o l e A p p l i c a t i o n i { c l a s s C l a s s l
{ s t a t i c v o i d P ( i n t a , r e f i n t b ) {
a = 4 4 ; b = 3 3 ; C o n s o l e . W r i t e L i n e ( " {0} { 1 } " , a, b );
} s t a t i c v o i d M a i n ( ) {
i n t a = 2. b = 4; C o n s o l e . W r i t e L i n e C " {0} { 1 } " , a, b ); P( a, r e f b ) ; C o n s o l e . W r i t e L i n e C " {0} { ! } " , a, b ) :
113
:
:;. 2 4 44 33 2 33
, Main , - , b -, .
,
, , .
, - ,
,
, .
, , .
, .
,
,
.
,
, ,
. -
: .
o u t . , , ,
.
.
5.4 , ( 5.5).
5 . 5 .
, s i n g System; "smespace C o n s o l e A p p l i c a t i o n i
c l a s s C l a s s l {
s t a t i c v o i d P ( i n t a . o u t i n t b ) {
a = 4 4 ; b = 3 3 : C o n s o l e . W r i t e L i n e C " { 0 } { 1 } " . a, b ) ;
} s t a t i c v o i d M a i n O
^ &
114 5. :
5 . 5 () int = 2, ; ( a, out b ) : Console.WriteLineC " {0} { ! } " , a, b );
out.
,
. ,
.
this .
,
, .
this, .
this , , ,
, :
'< class Demo
double ; public Demo TO //
- return this; ** 1 public void Sety( double )
this. = ; //
.
new.
. :
, void.
115
.
-
, ,
n u l l .
"J , , .
| ., , 5.1). , .
, -
;. 5.6 Demo , ( ). .
5 . 6 .
..s-ng System; "=mespace C o n s o l e A p p l i c a t i o n i
c l a s s Demo
{ p u b l i c Demo( i n t a, d o u b l e ) // {
t h i s . = ; t h i s . = :
} p u b l i c d o u b l e G e t y O // {
r e t u r n ;
} i n t ; d o u b l e ;
} c l a s s C l a s s l { s t a t i c v o i d M a i n O
{ Demo a = new Demo( 300, 0.002 ) C o n s o l e . W r i t e L i n e C a . G e t y O ) : Demo b = new Demo( 1, 5 . 7 1 ); C o n s o l e . W r i t e L i n e C b . G e t y O ) ;
} }
}
// // ; 0,002 // // : 5 , 7 1
116 5. :
,
.
,
:
class Demo
{ public DemoC int a ) // 1 {
this.a = a: this. = 0.002;
} public Demo( double ) // 2 {
this.a = 1: this. = ;
}
Demo x = new Demo( 300 ); // 1 Demo = new DemoC 5.71 ); // 2
.
- ,
-,
. this , :
class Demo
{ public DemoC int a ) // 1 {
this.a = a; } public DemoC int a, double ) : thisC a ) // 1 {
this. = ; }
} , , ,
,
.
, # object. , ,
.
118 5. :
5 . 7 () class Class2 { static void MainO
{ D.PrintO;
// D d = new DO; // :
, ( ), ,