Вы находитесь на странице: 1из 31

#

. .

namespace WriteTest4
{ 2
class Program
{
static void Main(string[] args)
{
int a = 10, c;
//

double b = 9.5;
c = a+b;
//
}
}
}

. .


{

}

;

+ - * / %

(,
)

& | ^ ! ~ && ||
true false
+

++ --

<< >>

== != < > <= >=

( )


double = 1, b = 2.5, = -3;
double x = (-b + Math.Sqrt(b * b - 4 *
* c)) / (2 * );
,

:
a=1; b=3;
Math.Sqrt(b * b - 4 * * c)+1;

// error

x=a+2*b;
:
int x, ;
x = = 0;



. 3/2
1.

, /
float double.
int x = 3 ;
double = 2, c;
c = x / y;
Console.WriteLine( double result = + c);
.
//21
int a = int.MaxValue, b;
b = a+1;
Console.WriteLine(MaxValue = + a);
Console.WriteLine(MaxValue + 1 = + b);


% .
//22
int x = 3 , = 2, c;
c = x % y;
Console.WriteLine(x Mod y = + c);
%
.
2.
, 0,
, 1.


float,
double , ..
0 1.

. .
// 23
float tenth = 0.1f;
//
0.1
float one = 1f;
Console.WriteLine (one - tenth * 10f);
//
1.490116E-08
decimal m = 1M / 6M;
//
0.1666666666666666666666666667M
double d = 1.0 / 6.0;
//
0.16666666666666666
Decimal WholeM = m+m+m+m+m+m;
// 1.0000000000000000000000000002M


short x = 1, y = 1;
short z = x + y;
// Compile-time error
short z = (short) (x + y); // OK
int a = 1000000;
int b = 1000000;
int c = checked (a * b);
//
.
checked
//
{
...
c = a * b;
...
}

++

--

()


static void Main(string[] args)
{
int a = 0, b = 5;
a++;
// a=1;
b--;
// b=4
}
+
+ ++.

()
, ,
-
, () :
static void Main(string[] args)
{ //24
int x = 0, y = 0;
Console.WriteLine (x++); // 0; x 1
Console.WriteLine (x);
// x 1
Console.WriteLine (++y);
1
}

// y


static void Main(string[] args)
{ //25
int a = 2, b = 3, c, d = 3;
c = a + ++b;
// c = 6, ,

c = a + d++;
// c = 5, ,

Console.WriteLine("a=" + a +" b=" + b + " c=" + c +
" d=" + d);
}


static void Main(string[] args)
{ //26
int a = 2, b = 3, c, d = 3;
c = a+++b;
// c ???
c = a + ++b;
// ?
}

/ ,
/.

+=
-=

*=
/=
%=
&=
=

^=

<<=
>>=


-
:
static void Main(string[] args)
{
int a = 2, b = 3;
a += b;
//
a -= b;
//
a *= b;
//
a /= b;
//
a %= b;
//
}

a
a
a
a
a

=
=
=
=
=

a
a
a
a
a

+ b;
- b;
* b;
/ b;
% b;

&

<<

>>

&&


!
.
static void Main(string[] args)
{
bool a, b = true, c = false;
a = !b;
// a = false
a = !c;
// a = true
}


|| -
false ,
false,
true;
static void Main(string[] args)
{
bool a, bTrue = true, bFalse = false;
a = bFalse || bFalse;
// a = false
a = bFalse || bTrue;
// a = true
a = bTrue || bFalse;
// a = true
a = bTrue || bTrue;
// a = true
}


&& -
true ,
true,
false;
static void Main(string[] args)
{
bool a, bTrue = true, bFalse = false;
a = bFalse && bFalse;
// a = false
a = bFalse && bTrue;
// a = false
a = bTrue && bFalse;
// a = false
a = bTrue && bTrue;
// a = true
}

==

!=

<

<=

>

>=


static void Main(string[] args)
{
bool a;
int b = 2, c = 3, d = 2;
a = b > c;
// a =
a = b < c;
// a =
a = b >= c;
// a =
a = b >= d;
// a =
a = b == c;
// a =
a = b == d;
// a =
a = b != c;
// a =
}

false
true
false
true
false
true
true

(expr) x.y f(x) a[x] x++ x-- new


sizeof(t) typeof(t) checked(expr)
unchecked(expr)

+ - ! ~ ++x --x (T)x

()

* / %

()

+ -

<< >>

< > <= >= is as

== !=

&


(XOR)

(OR)

10

&&

11

||

12

? :


:
2+2*2=6
(2+2)*2=8

:
class Program
{ //27
static int X(string label, int i)
{
Console.Write(label);
return i;
}
static void Main(string[] args)
{
Console.WriteLine(X("a", 1) + X("b", 1) + X("c", 1) +
X("d", 1));
Console.WriteLine();
Console.WriteLine( X("a", 1) + X("b", (X("c", 1) +
X("d", 1) + X("e", 1))) +
X("f", 1));
}
}
C# .
,


(currency) .

(general) .

static void Main(string[] args)


{
int a = 10;
Console.WriteLine("{0} {0:D5}", a);
double b = 12345.12345;
Console.WriteLine("{0} {0:C} {0:E3}", b);
Console.WriteLine("{0:F6} {0:N4}
{0:#.###}", b);
}

Оценить