1
. Microsoft.NET
C# 2000 Microsoft. ( 1980- Turbo Pascal).
C# C, C++ Java. , , . C# -, ++. ++ C#. # , .NET.
.NET :
;
, .
, , .
, ( ).
.NET Framework (Common Language Runtime), ( ) (Managed Code). , , . , , , , , , , , .
, , .NET, :
-, - , , . : , . , ( - ), . , (: , ).
, (: ), : , , , , , . , , .
|
|
, , .NET-. , .NET Framework,.NET- , .NET Framework Microsoft Windows. Framework Linux ( Mono), Linux Windows, .
.NET : Common Language Runtime .NET Framework Class Library.
CLR (Common Language Runtime, ) , .NET, CIL (Common Intermediate Language ). , , , .
NET Framework Class Library , . , -, , , . -, FCL , . .NET Framework FCL BCL (. Base Class Library ).
, ( ), , , .
.NET Framework : (value-types) (reference-types). , (), .NET Framework managed heap.
C# : char, byte, sbyte, short, ushort, int, uint, long ulong. char , , . . :
:
int param = 170;
int paramBase16 = 0xaa;
. C# : float double. . , float 32 , 5E-45 3,4E+38. double 64 , 5E-324 1,7+308.
float , . double , float, (15 ).
|
|
:
float f = 12.3F;
decimal, . 128 1-28 7,9+28. . decimal, 28 . , , .
:
decimal totalMoney = 2005m, workedHours = 263m;
decimal ratePerHour = totalMoney / workedHours;
// 7,6235741444866920152091254753
.NET Framework char
Unicode. Unicode , 21- , code point ( ), UTF-16, , 16- .
, char 16- .
:
char char1 = 'A';
char char2 = 'V';
bool compare = 'A' > 'V'; //false
.NET Framework object string.
string (.NET Class: System.String) .
String , :
Compare
Concat .
Copy .
Equals .
Remove .
Replace .
Split ,
.
# - , . ѻ . , , 100 ( ).
# - , .
C# . :
L long;
F float;
D double;
M decimal;
U ( U , ).
:
10D // double
10F // float
10M // decimal
10 // int
10L // long
10UL // unsigned long
, , :
\a
\b
\f
\n
\r
\t
\v
\0 - ( )
\ , " ' \
:
"Hello \"World\"!\n" // Hello "World"!
2
, . C# type-safe . , C# (C# compiler) , . C# (strongly typed) , . :
|
|
_ _;
_ _ = ;
(initial value) . , C# .
:
double a, b = 5;
double result = a / b; // "",
#, - ( ). . . :
int SomeVar; //
int somevar; //
int _SomeVar; //
int SomeVar2; //
int 3_SomeVar; //
C# -. SomeVar somevar, . , , , @. .
:
int @int;
.NET . , .NET , , , :
Pascal case convention ( ) . . . () .
:
double SomeVariable;
int EveryWordStartsWithUpperCharacter;
Camel case convention Pascal case, , , , .
:
int someVariable;
double totalCount;
Uppercase convention , . , .
, . ( - , Camel, - Uppercase convertion).
, . , . , .
:
{
//first block
}
if (a > 5) {
//another block
}
int someFunction() {
//one more block
}
.
. :
|
|
int someFunc() {
//block A
int a = 5;
if (a > 2) {
//block B
a += 2;
int b = a;
if (b == 7) {
//block C
int c = 1;
a = a + b + c;
}
//c += 2;
}
//b = 0;
return a;
}
: A, B, C. "". B a, b. C a,b,c.
, .
# :
( ) (value-types)
(referenced types).
, . , , , . : , , , , ( , ).
, X 10 Y X, X, Y , X . :
int x = 10;
int y = x;
x = x 5;
//x = 5, y = 10
. . :
SomeObject a = new SomeObject(); //
SomeObject b = a; // , a,b
//a.changeState(1); , b
SomeObject c = new SomeObject()
c.changeState(2);
b = c;
// a 1
// b,c 2
( ) . :
(implicit) ( ).
(explicit) (, , ).
. .
, , . , . , double , float ( double ). , float double , float. , , double c . ( int) . , , . , . :
float a = 3.5f;
int b = 5;
float c = a + b; // c = 8.5
int d = a + b; //
:
byte: short, ushort, int, uint, long, ulong, float, double, decimal
sbyte: short, int, long, float, double, decimal
short: int, long, float, double, decimal
ushort: int, uint, long, ulong, float, double, decimal
int: long, float, double, decimal
uint: long, ulong, float, double, decimal
long: float, double, decimal
ulong: float, double, decimal
char: ushort, int, uint, long, ulong, float, double, decimal
float:double
. :
int d =(int) a + b; //d = 8
:
float a = 4.9, b = 4.9;
int result1 = (int)a + (int)b; //a->4, b->4; result1 = 4+4 = 8
|
|
int result2 = (int)(a + b); //a+b=9.8; 9.8->9; result2 = 9
:
byte: Sbyte char
sbyte: byte, ushort, uint, ulong, char
short: sbyte, byte, ushort, uint, ulong, char
ushort: sbyte, byte, short, char
int: sbyte, byte, short, ushort, uint, ulong, char
uint: sbyte, byte, short, ushort, int, char
long: sbyte, byte, short, ushort, int, uint, ulong, char
ulong: sbyte, byte, short, ushort, int, uint, long, char
char: sbyte, byte, short
float: sbyte, byte, short, ushort, int, uint, long, ulong, char, decimal
double: sbyte, byte, short, ushort, int, uint, long, ulong, char, float, decimal
decimal: sbyte, byte, short, ushort, int, uint, long, ulong, char, float, double
3
( ), , .
# :
1. .
2. .
3. .
4. .
5. .
, . , , ,
. , , ( # ) .
- .
(-) . , . , .
, :
++ ++ -- .
:
int a = 5, b = -3;
int c = a > b? 14: 12; // c=14
int d = ++a * c; // a=6 d=84
int e = d-- / a; //e=14 d=83
e = d / a; //e=13
int firstDigitOfD = d % 10; //3
int secondDigitOfD = d / 10 % 10; //8
string message1 = "d = " + secondDigitOfD + firstDigitOfD; // "d = 83"
string message2 = " d =" + (secondDigitOfD + firstDigitOfD); //" d = 9"
string message3 = "d - " + (d % 2 == 0? "": "") + " "; //"d - "
a = 7;
b = 2;
c = 4;
d += a / b * c < c * a / b? 1: -1; // a/b*c=12; c*a/b=14; 12<14 d = 84
int zero = 0;
zero = 5 / zero; // DivideByZeroException: .
double infinity = 1.0 / 0.0; //infinity=
--infinity; //infinity=
, , , (True or False), .
= = != , >, <, >=, <= , .
:
2 > 7 //false
2!= 4// true
8 < 10//true
my == my//true
false == true//false
// true > false //!!!
, (true or false). .
() , .
:
int a = 1, b = 3, c;
c = a > b && a++ % 2 == 0? 1: 0; // c=0;
bool even = a % 2 ==0; //false
a = 1;
b = 3;
c = a > b & a++ % 2 == 0? 1: 0; // c=0; a=2
even = a % 2 ==0; //true
. . . .
, .
:
int a = 10;
int b = 1;
int result = a >> b;// 2
//, 1, 2
, .
(=). # , . # = . :
int , b, c;
a = b = c = 58;
, . , .
, . # :
++() - - ()
! ~ + () () ++ () - - ()
* / %
+ -
>> <<
< > <= >=
= =!=
&
^
|
&&
| |
, . . .
- , . :
< 34 Sity! = " "
C - , . :
a > 2 && a < 10 ( 2 10)
a > 5 || b > 5 ( 5 b 5)
!(a < 0) ( , 0)
if
, , .
if . :
.
if:
if ( ) ;
++: