c#. Спецификация языка. Версия 3.0. 2007. microsoft.doc

675
C # Спецификация языка Версия 3.03.0 Корпорация Майкрософт (Microsoft Corp.), 1999-2007. Все права защищены. Присылайте нам свои исправления, комментарии, отзывы и предложения по адресу [email protected]

Upload: iko1402

Post on 15-Apr-2016

238 views

Category:

Documents


2 download

TRANSCRIPT

C# 3.0

DOCVARIABLE Version \* MERGEFORMAT (Microsoft Corp.), 1999-2007. .Microsoft, Windows, Visual Basic, Visual C# Visual C++ . .11.

1.1 Hello world11.2 21.3 41.4 81.5 101.6 141.6.1 141.6.2 151.6.3 151.6.4 161.6.5 161.6.6 171.6.6.1 171.6.6.2 191.6.6.3 191.6.6.4 , 201.6.6.5 221.6.7 -231.6.7.1 251.6.7.2 261.6.7.3 261.6.7.4 271.6.7.5 271.6.7.6 281.7 281.8 291.9 311.10 321.11 331.12 342. 372.1 372.2 372.2.1 372.2.2 382.2.3 382.3 382.3.1 392.3.2 402.3.3 412.4 412.4.1 412.4.2 422.4.3 442.4.4 442.4.4.1 442.4.4.2 452.4.4.3 462.4.4.4 461.1.1.1 472.4.4.5 null492.4.5 492.5 502.5.1 512.5.2 512.5.3 522.5.4 532.5.5 552.5.6 562.5.7 562.5.8 pragma572.5.8.1 pragma warning573. 593.1 .593.2 .603.3 .603.4 .623.4.1 .633.4.2 .633.4.3 .633.4.4 .633.4.5 .643.4.6 .643.4.7 .643.5 .643.5.1 .643.5.2 .653.5.3 .683.5.4 .693.6 .693.7 .713.7.1 .733.7.1.1 .743.7.1.2 .743.8 .763.8.1 .783.9 .793.10 814. 834.1 834.1.1 System.ValueType844.1.2 844.1.3 854.1.4 854.1.5 864.1.6 884.1.7 decimal894.1.8 bool894.1.9 904.1.10 904.2 914.2.1 914.2.2 924.2.3 924.2.4 924.2.5 924.2.6 924.3 934.3.1 934.3.2 944.4 954.4.1 964.4.2 964.4.3 974.4.4 974.5 984.6 995. 1015.1 1015.1.1 1015.1.2 1015.1.2.1 1025.1.2.2 1025.1.3 1025.1.4 1025.1.5 1025.1.6 1035.1.7 1035.2 1045.3 1045.3.1 1055.3.2 1065.3.3 1065.3.3.1 1065.3.3.2 , 1075.3.3.3 1075.3.3.4 1075.3.3.5 If1075.3.3.6 switch1085.3.3.7 while1085.3.3.8 do1085.3.3.9 for1095.3.3.10 break, continue goto1095.3.3.11 throw1095.3.3.12 return1095.3.3.13 try-catch1105.3.3.14 try-finally1105.3.3.15 try-catch-finally1105.3.3.16 foreach1115.3.3.17 using1115.3.3.18 lock1115.3.3.19 yield1125.3.3.20 1125.3.3.21 1125.3.3.22 1125.3.3.23 1135.3.3.24 &&1135.3.3.25 ||1145.3.3.26 !1155.3.3.27 ?? ??1155.3.3.28 ?:1165.3.3.29 1165.4 1175.5 1176. 1196.1 1196.1.1 1196.1.2 1206.1.3 1206.1.4 1206.1.5 null1216.1.6 1216.1.7 1216.1.8 1226.1.9 , 1226.1.10 1226.1.11 1236.2 1236.2.1 1236.2.2 1256.2.3 1256.2.4 1266.2.5 1266.2.6 , 1276.2.7 1286.3 1286.3.1 1286.3.2 1286.4 1286.4.1 1286.4.2 1296.4.3 1296.4.4 1306.4.5 1316.5 1326.5.1 1336.5.2 1346.5.3 1346.6 1377. 1417.1 1417.1.1 1427.2 1427.2.1 1437.2.2 1447.2.3 1457.2.4 1457.2.5 -1467.2.6 1467.2.6.1 1467.2.6.2 1467.2.7 1477.3 1487.3.1 1497.4 -1507.4.1 1537.4.2 1557.4.2.1 1567.4.2.2 1567.4.2.3 1567.4.2.4 1567.4.2.5 1577.4.2.6 1577.4.2.7 1577.4.2.8 1577.4.2.9 1577.4.2.10 1587.4.2.11 1587.4.2.12 1597.4.2.13 1597.4.3 1607.4.3.1 -1607.4.3.2 -1617.4.3.3 1627.4.3.4 1627.4.3.5 1637.4.4 -1637.4.4.1 1657.5 1657.5.1 1667.5.2 1667.5.2.1 1677.5.3 1687.5.4 1687.5.4.1 1707.5.4.2 1717.5.5 1717.5.5.1 1727.5.5.2 1737.5.5.3 1757.5.6 1767.5.6.1 1767.5.6.2 1777.5.7 this1777.5.8 base1787.5.9 1797.5.10 new1807.5.10.1 1807.5.10.2 1817.5.10.3 1837.5.10.4 1847.5.10.5 1867.5.10.6 1887.5.11 typeof1897.5.12 checked unchecked1917.5.13 1937.5.14 1947.6 1947.6.1 1947.6.2 1947.6.3 1957.6.4 1957.6.5 1957.6.6 1967.7 1977.7.1 1977.7.2 1987.7.3 1997.7.4 2007.7.5 2027.8 2047.9 2057.9.1 2067.9.2 2067.9.3 2077.9.4 2077.9.5 2087.9.6 2087.9.7 2097.9.8 2107.9.9 null2107.9.10 is2107.9.11 as2117.10 2127.10.1 2127.10.2 2137.10.3 2137.10.4 2137.11 2147.11.1 2147.11.2 2147.12 null2157.13 2167.14 2177.14.1 2187.14.2 2197.14.3 2197.14.4 2207.14.4.1 2207.14.4.2 2217.14.5 2237.15 2237.15.1 2247.15.2 2257.15.2.1 select groupby 2257.15.2.2 2267.15.2.3 2267.15.2.4 from, let, where, join orderby2277.15.2.5 select2307.15.2.6 groupby2307.15.2.7 2317.15.3 2327.16 2337.16.1 2347.16.2 2367.16.3 2377.17 2377.18 2377.19 2398. 2408.1 2408.2 2428.2.1 2428.3 2438.4 2438.5 2448.5.1 2448.5.2 2468.6 -2468.7 2468.7.1 if2478.7.2 switch2478.8 2518.8.1 while2518.8.2 do2528.8.3 for2528.8.4 foreach2538.9 2568.9.1 break2578.9.2 continue2588.9.3 goto2588.9.4 return2598.9.5 throw2608.10 try2618.11 checked unchecked2648.12 lock2648.13 using2658.14 yield2679. 2699.1 2699.2 2699.3 2719.4 using2719.4.1 using alias2729.4.2 using namespace2749.5 2769.6 2769.7 2779.7.1 27810. Classes28110.1 .28110.1.1 .28110.1.1.1 .28210.1.1.2 .28210.1.1.3 .28310.1.2 partial.28410.1.3 .28410.1.4 .28410.1.4.1 .28410.1.4.2 .28610.1.5 .28610.1.6 .29010.2 .29010.2.1 .29110.2.2 .29110.2.3 .29210.2.4 .29210.2.5 .29210.2.6 .29310.2.7 .29310.2.8 .29610.3 .29610.3.1 .29810.3.2 .29810.3.3 .29910.3.4 new.30010.3.5 .30010.3.6 .30010.3.7 .30010.3.8 .30110.3.8.1 .30210.3.8.2 30210.3.8.3 30210.3.8.4 this30310.3.8.5 -30310.3.8.6 30410.3.9 30510.3.9.1 , 30510.3.9.2 , 30610.3.9.3 , 30610.3.9.4 , 30610.4 30710.5 30810.5.1 30910.5.2 31010.5.2.1 31010.5.2.2 31110.5.3 volatile31110.5.4 31310.5.5 31310.5.5.1 31410.5.5.2 31510.6 31510.6.1 31710.6.1.1 31810.6.1.2 31910.6.1.3 32010.6.1.4 32010.6.2 32310.6.3 32310.6.4 32510.6.5 32710.6.6 32810.6.7 32910.6.8 33010.6.9 33010.6.10 33110.6.11 33110.7 33110.7.1 33310.7.2 33310.7.3 33810.7.4 33910.7.5 , , 34010.8 34210.8.1 , 34410.8.2 34510.8.3 34610.8.4 , , 34710.9 34710.9.1 35110.10 35110.10.1 35210.10.2 35310.10.3 35310.11 35610.11.1 35710.11.2 35810.11.3 35810.11.4 36010.11.5 36010.11.6 36110.12 36110.13 36310.14 36510.14.1 36510.14.2 36510.14.3 yield36510.14.4 36510.14.4.1 MoveNext36610.14.4.2 Current36710.14.4.3 Dispose36710.14.5 36810.14.5.1 GetEnumerator36810.14.6 36811. 37511.1 37511.1.1 37511.1.2 partial37611.1.3 37611.1.4 37611.2 37611.3 37611.3.1 37711.3.2 37811.3.3 37811.3.4 37811.3.5 37911.3.6 this38111.3.7 38111.3.8 38111.3.9 38211.3.10 38211.4 38211.4.1 38211.4.2 38412. 38712.1 38712.1.1 System.Array38812.1.2 IList38812.2 38912.3 38912.4 38912.5 38912.6 39013. 39313.1 39313.1.1 39313.1.2 partial39313.1.3 39413.1.4 39413.2 39513.2.1 39613.2.2 39613.2.3 39613.2.4 39613.2.5 39713.3 39913.4 39913.4.1 40013.4.2 40213.4.3 40313.4.4 40413.4.5 40713.4.6 40813.4.7 40914. 41114.1 41114.2 41214.3 41214.4 System.Enum41414.5 41415. 41515.1 41515.2 41715.3 41715.4 41816. 42116.1 42116.2 System.Exception42116.3 42116.4 42217. 42517.1 42517.1.1 42517.1.2 42617.1.3 42717.2 42717.3 43317.3.1 43317.3.2 43317.4 43317.4.1 AttributeUsage43417.4.2 Conditional43417.4.2.1 43517.4.2.2 43717.4.3 Obsolete43717.5 43817.5.1 COM Win3243817.5.1.1 .NET IndexerName43918. 44118.1 44118.2 44318.3 44618.4 44618.5 44718.5.1 44818.5.2 44818.5.3 44918.5.4 45018.5.5 45118.5.6 45118.5.7 45218.5.8 sizeof45218.6 fixed45318.7 45718.7.1 45718.7.2 45818.7.3 45918.8 45918.9 460A. 463A.1 463A.2 464A.2.1 465A.2.2 465A.2.3 466A.2.4 466A.2.5 467A.2.6 467A.2.7 468A.2.8 469A.2.9 469A.2.10 469A.2.11 470A.2.12 470A.2.13 471A.2.14 471A.2.15 471A.2.16 472A.2.17 472A.2.18 472A.3 473A.3.1 473A.3.2 474A.4 477A.4.1 C#477A.4.2 XML480B. 483B.1 483B.1.1 483B.1.2 483B.1.3 484B.1.4 484B.1.5 484B.1.6 484B.1.7 486B.1.8 486B.1.9 488B.1.10 488B.2 491B.2.1 491B.2.2 491B.2.3 492B.2.4 492B.2.5 499B.2.6 502B.2.7 503B.2.8 510B.2.9 511B.2.10 511B.2.11 512B.2.12 513B.2.13 513B.3 514C. 519

1. C# ( - -) , , - . C# C , C, C++ Java. C# ECMA ECMA-334, ISO/IEC ISO/IEC 23270. Microsoft C# .NET Framework .C# - , _ . . , , , , . C# , , . C# , . : , ; , ; , , . C# . C#, (, int double), object. , , , . , C# , . C#, C# . , . C# : virtual override, . C#. . , . , , .

1.1 Hello world Hello, World . C#:using System;class Hello{

static void Main() {

Console.WriteLine("!");

}}

C# cs. Hello, World hello.cs, Microsoft C#, :csc hello.cs hello.exe. :! Hello, World using, System. C#. . , System (, Console), (, IO Collections). using , . using Console.WriteLine System.Console.WriteLine. Hello, Hello, World, Main. Main static. C# this, , . Main . WriteLine Console, System. .NET Framework, Microsoft C#. , C# . C# .NET Framework.1.2 C# : , , , . C# . , . . , , . C# . exe dll .:using System;namespace Acme.Collections{

public class Stack

{

Entry top;

public void Push(object data) {

top = new Entry(top, data);

}

public object Pop() {

if (top == null) throw new InvalidOperationException();

object result = top.data;

top = top.next;

return result;

}

class Entry

{

public Entry next;

public object data;

public Entry(Entry next, object data) {

this.next = next;

this.data = data;

}

}

}}

Stack, Acme.Collections. Acme.Collections.Stack. : top, Push Pop, Entry. Entry : next data, . acme.cs, csc /t:library acme.cs ( Main) acme.dll. (Intermediate Language, IL), . IL JIT- .NET CLR. , , , C# #include. C# , , . , Acme.Collections.Stack, acme.dll:using System;using Acme.Collections;class Test{

static void Main() {

Stack s = new Stack();

s.Push(1);

s.Push(10);

s.Push(100);

Console.WriteLine(s.Pop());

Console.WriteLine(s.Pop());

Console.WriteLine(s.Pop());

}}

test.cs, acme.dll /r:csc /r:acme.dll test.cs test.exe, :

100101

C# . C# . . . C# , , , . C# . .1.3 C# : . , (). . , , . . ( ref out).

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

: sbyte, short, int, long

: byte, ushort, uint, ulong

: char

IEEE : float, double

: decimal

: bool

enum E {...}

struct S {...}

, null

: object

: string

class C {...}

interface I {...}

- , int[] int[,]

, delegate int D(...)

8-, 16-, 32- 64- .

(float double) 32- ( ) 64- ( ) IEEE 754.

decimal 128- , .

bool C# true false.

C# . char UTF-16, string UTF-16.

C#.

8sbyte128...127

16short32768...32767

32int2147483648...2147483647

64long9223372036854775808...9223372036854775807

8byte0...255

16ushort0...65535

32uint0...4294967295

64ulong0...18446744073709551615

32float 1,51045 3,41038 7

64double 5,010324 1,710308 15

128decimal 1,01028 7,91028 28

C# . . C# : , , , . , - () - (, ). , , . , , , - -. , , . . object. -. , , - . . . . , . , . , , - . , , , . , . , . . C# - . , , . , . , int[] int; int[,] int, int[][] , int. . T T?, null. , int? 32- , null. C# , . C# object, . object. . int object int.using System;class Test{

static void Main() {

int i = 123;

object o = i;

//

int j = (int)o;

//

}}

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

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

null

object ,

, ,

, , , ,

,

1.4 . , . : +, -, *, / new. : , , . , , . , x + y * z x + (y * z), * +. . , . C# . .

x.m

x(...)

x[...]

x++

x--

new T(...)

new T(...){...}

new {...}

new T[...]

typeof(T) System.Type T

checked(x) checked

unchecked(x) unchecked

default(T) T

delegate {...} ( )

+x

-x

!x

~x

++x

--x

(T)x x T

x * y

x / y

x % y

x + y, ,

x y,

x > y

x < y

x > y

x = y

x is T true, x T; false

x as T x, T, null, x , T

x == y

x != y

ANDx & y AND, AND

XORx ^ y XOR, XOR

ORx | y OR, OR

AND

x && yy , x true

ORx || yy , x false

nullX ?? y x null, y; x

x ? y : z x true, y; x false, z

x = y

x = y ; :*= /= %= += -= = &= ^= |=

(T x) => y (-)

1.5 . C# , . , . , { }. . . : , new, = , ++ --. - . if switch. . while, do, for foreach. . break, continue, goto, throw, return yield. try...catch , . try...finally , ( , ). checked unchecked . lock , . using , .

C# .

static void Main() { int a; int b = 2, c = 3; a = 1; Console.WriteLine(a + b + c);}

static void Main() { const float pi = 3.1415927f; const int r = 25; Console.WriteLine(pi * r * r);}

static void Main() { int i; i = 123;

// Console.WriteLine(i);// i++;

// Console.WriteLine(i);// }

ifstatic void Main(string[] args) { if (args.Length == 0) {

Console.WriteLine(" "); } else {

Console.WriteLine(" "); }}

switchstatic void Main(string[] args) { int n = args.Length; switch (n) {

case 0:

Console.WriteLine(" ");

break;

case 1:

Console.WriteLine(" ");

break;

default:

Console.WriteLine(": {0}", n);

break;

} }}

whilestatic void Main(string[] args) { int i = 0; while (i < args.Length) {

Console.WriteLine(args[i]);

i++; }}

dostatic void Main() { string s; do {

s = Console.ReadLine();

if (s != null) Console.WriteLine(s); } while (s != null);}

forstatic void Main(string[] args) { for (int i = 0; i < args.Length; i++) {

Console.WriteLine(args[i]); }}

foreachstatic void Main(string[] args) { foreach (string s in args) {

Console.WriteLine(s); }}

breakstatic void Main() { while (true) {

string s = Console.ReadLine();

if (s == null) break;

Console.WriteLine(s); }}

continuestatic void Main(string[] args) { for (int i = 0; i < args.Length; i++) {

if (args[i].StartsWith("/")) continue;

Console.WriteLine(args[i]); }}

gotostatic void Main(string[] args) { int i = 0; goto check; loop: Console.WriteLine(args[i++]); check: if (i < args.Length) goto loop;}

returnstatic int Add(int a, int b) { return a + b;}static void Main() { Console.WriteLine(Add(1, 2)); return;}

yieldstatic IEnumerable Range(int from, int to) { for (int i = from; i < to; i++) {

yield return i; } yield break;}

static void Main() { foreach (int x in Range(-10,10)) {

Console.WriteLine(x); }}

throw trystatic double Divide(double x, double y) { if (y == 0) throw new DivideByZeroException(); return x / y;}static void Main(string[] args) { try {

if (args.Length != 2) {

throw new Exception(" ");

}

double x = double.Parse(args[0]);

double y = double.Parse(args[1]);

Console.WriteLine(Divide(x, y)); } catch (Exception e) {

Console.WriteLine(e.Message); } finally {

Console.WriteLine(" "); }}

checked uncheckedstatic void Main() { int i = int.MaxValue; checked {

Console.WriteLine(i + 1);

// } unchecked {

Console.WriteLine(i + 1);

// }}

lockclass Account{ decimal balance;

public void Withdraw(decimal amount) {

lock (this) {

if (amount > balance) {

throw new Exception(" ");

}

balance -= amount;

} }}

usingstatic void Main() { using (TextWriter w = File.CreateText("test.txt")) {

w.WriteLine(" 1");

w.WriteLine(" 2");

w.WriteLine(" 3"); }}

1.6 C# . , () ( -). ( ). , , . . , , , ( ), , . , , { }. Point:public class Point{

public int x, y;

public Point(int x, int y) {

this.x = x;

this.y = y;

}}

new, , . Point, :Point p1 = new Point(0, 0);Point p2 = new Point(10, 20);, , , . C# ( ) .1.6.1 . . ( ). , .

,

,

,

, ,

,

,

,

,

,

1.6.2 , , . . .

public

protected

internal

protected internal ,

private

1.6.3 . . . Pair TFirst TSecond:public class Pair{

public TFirst First;

public TSecond Second;} , , . , . :Pair pair = new Pair { First = 1, Second = "" };int i = pair.First; // TFirst is intstring s = pair.Second; // TSecond is string , Pair, .1.6.4 , . , , object. Point3D Point, Point object:public class Point{

public int x, y;

public Point(int x, int y) {

this.x = x;

this.y = y;

}}

public class Point3D: Point{

public int z;

public Point3D(int x, int y, int z): base(x, y) {

this.z = z;

}}

. , ( ). , . Point3D x y Point. , Point3D : x, y z. . , . , Point Point, Point3D:Point a = new Point(10, 20);Point b = new Point3D(10, 20, 30);1.6.5 , ., static, . . ., static, . . Color r, g b. Black, White, Red, Green Blue:public class Color{

public static readonly Color Black = new Color(0, 0, 0);

public static readonly Color White = new Color(255, 255, 255);

public static readonly Color Red = new Color(255, 0, 0);

public static readonly Color Green = new Color(0, 255, 0);

public static readonly Color Blue = new Color(0, 0, 255);

private byte r, g, b;

public Color(byte r, byte g, byte b) {

this.r = r;

this.g = g;

this.b = b;

}}

readonly. readonly .1.6.6 , , . . . (, ) , , , , , . , void., , , . , . , . , , , . .1.6.6.1 . , . : , , . . , , . , . , . , , . , . ref. ref.using System;class Test{

static void Swap(ref int x, ref int y) {

int temp = x;

x = y;

y = temp;

}

static void Main() {

int i = 1, j = 2;

Swap(ref i, ref j);

Console.WriteLine("{0} {1}", i, j);

// : "2 1"

}}

. , , . out. out.using System;class Test{

static void Divide(int x, int y, out int result, out int remainder) {

result = x / y;

remainder = x % y;

}

static void Main() {

int res, rem;

Divide(10, 3, out res, out rem);

Console.WriteLine("{0} {1}", res, rem);// : "3 1"

}}

. params. . . Write WriteLine System.Console . .public class Console{

public static void Write(string fmt, params object[] args) {...}

public static void WriteLine(string fmt, params object[] args) {...}

...}

, . , . . Console.WriteLine("x={0} y={1} z={2}", x, y, z); :string s = "x={0} y={1} z={2}";object[] args = new object[3];args[0] = x;args[1] = y;args[2] = z;Console.WriteLine(s, args);1.6.6.2 , .

, . . , . i j .

using System;class Squares{

static void Main() {

int i = 0;

int j;

while (i < 10) {

j = i * i;

Console.WriteLine("{0} x {0} = {1}", i, j);

i = i + 1;

}

}}

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

return . , void, return . , void , return , .

1.6.6.3 , static, . ., static, . , . , , this. this . Entity , .class Entity{

static int nextSerialNo;

int serialNo;

public Entity() {

serialNo = nextSerialNo++;

}

public int GetSerialNo() {

return serialNo;

}

public static int GetNextSerialNo() {

return nextSerialNo;

}

public static void SetNextSerialNo(int value) {

nextSerialNo = value;

}}

Entity (, , , ). Entity ( ) . , serialNo, nextSerialNo. GetNextSerialNo SetNextSerialNo nextSerialNo, serialNo . Entity.using System;class Test{

static void Main() {

Entity.SetNextSerialNo(1000);

Entity e1 = new Entity();

Entity e2 = new Entity();

Console.WriteLine(e1.GetSerialNo());

// : "1000"

Console.WriteLine(e2.GetSerialNo());

// : "1001"

Console.WriteLine(Entity.GetNextSerialNo());

// : "1002"

}}

, SetNextSerialNo GetNextSerialNo , GetSerialNo .1.6.6.4 , virtual, . virtual , . , , . . . override, . . , . . abstract , abstract. . Expression, , : Constant, VariableReference Operation, , . ( , 4.6. .)using System;using System.Collections;public abstract class Expression{

public abstract double Evaluate(Hashtable vars);}public class Constant: Expression{

double value;

public Constant(double value) {

this.value = value;

}

public override double Evaluate(Hashtable vars) {

return value;

}}

public class VariableReference: Expression{

string name;

public VariableReference(string name) {

this.name = name;

}

public override double Evaluate(Hashtable vars) {

object value = vars[name];

if (value == null) {

throw new Exception(" : " + name);

}

return Convert.ToDouble(value);

}}

public class Operation: Expression{

Expression left;

char op;

Expression right;

public Operation(Expression left, char op, Expression right) {

this.left = left;

this.op = op;

this.right = right;

}

public override double Evaluate(Hashtable vars) {

double x = left.Evaluate(vars);

double y = right.Evaluate(vars);

switch (op) {

case '+': return x + y;

case '-': return x - y;

case '*': return x * y;

case '/': return x / y;

}

throw new Exception(" ");

}}

. , x + 3 .Expression e = new Operation(

new VariableReference("x"),

'+',

new Constant(3)); Evaluate Expression double. Hashtable, ( ) ( ). Evaluate . , . Constant Evaluate . VariableReference - . Operation ( Evaluate), . Expression x * (y + 2) x y.

using System;using System.Collections;class Test{

static void Main() {

Expression e = new Operation(

new VariableReference("x"),

'*',

new Operation(

new VariableReference("y"),

'+',

new Constant(2)

)

);

Hashtable vars = new Hashtable();

vars["x"] = 3;

vars["y"] = 5;

Console.WriteLine(e.Evaluate(vars));

// : "21"

vars["x"] = 1.5;

vars["y"] = 9;

Console.WriteLine(e.Evaluate(vars));

// : "16.5"

}}

1.6.6.5 . . , , , , . . Main , .class Test{

static void F() {

Console.WriteLine("F()");

}

static void F(object x) {

Console.WriteLine("F(object)");

}

static void F(int x) {

Console.WriteLine("F(int)");

}

static void F(double x) {

Console.WriteLine("F(double)");

}

static void F(T x) {

Console.WriteLine("F(T)");

}

static void F(double x, double y) {

Console.WriteLine("F(double, double)");

}

static void Main() {

F();

// F()

F(1);

// F(int)

F(1.0);

// F(double)

F("abc");

// F(object)

F((double)1);

// F(double)

F((object)1);

// F(object)

F(1);

// F(T)

F(1, 1);

// F(double, double)}}

, .1.6.7 - , , -. , -. -, C#: , , , , . List, . -.

public class List{

const int defaultCapacity = 4;

T[] items; int count;

public List(): this(defaultCapacity) {}

public List(int capacity) {

items = new T[capacity]; }

public int Count {

get { return count; } }

public int Capacity {

get {

return items.Length;

}

set {

if (value < count) value = count;

if (value != items.Length) {

T[] newItems = new T[value];

Array.Copy(items, 0, newItems, 0, count);

items = newItems;

}

} }

public T this[int index] {

get {

return items[index];

}

set {

items[index] = value;

OnChanged();

} }

public void Add(T item) {

if (count == Capacity) Capacity = count * 2;

items[count] = item;

count++;

OnChanged(); }

protected virtual void OnChanged() {

if (Changed != null) Changed(this, EventArgs.Empty); }

public override bool Equals(object other) {

return Equals(this, other as List); }

static bool Equals(List a, List b) {

if (a == null) return b == null;

if (b == null || a.count != b.count) return false;

for (int i = 0; i < a.count; i++) {

if (!object.Equals(a.items[i], b.items[i])) {

return false;

}

} return true; }

public event EventHandler Changed;

public static bool operator ==(List a, List b) {

return Equals(a, b); }

public static bool operator !=(List a, List b) {

return !Equals(a, b); }

}

1.6.7.1 C# . , , . , , . , . , static, . . . , List : , int. new. List List.List list1 = new List();List list2 = new List(10); , . , . , .1.6.7.2 . , , . , , . , , . , , get set, { }. , get set, . , get, . , set, . get , . , , get . set value, . ++ -- set , . List : Count Capacity ( ). .

List names = new List();names.Capacity = 100;

// setint i = names.Count;

// getint j = names.Capacity;

// get , C# . , static, . .

. virtual, abstract override, .

1.6.7.3 , ( ). , this, , [ ]. . , , . . List , int. List int. :List names = new List();names.Add("");names.Add("");names.Add("");for (int i = 0; i < names.Count; i++) {

string s = names[i];

names[i] = s.ToUpper();}

. , , .1.6.7.4 , . , event. , , , . , , . , null. List Changed, . Changed OnChanged, , null (.. ). , . . . +=, -=. Changed List .

using System;class Test{

static int changeCount;

static void ListChanged(object sender, EventArgs e) {

changeCount++;

}

static void Main() {

List names = new List();

names.Changed += new EventHandler(ListChanged);

names.Add("");

names.Add("");

names.Add("");

Console.WriteLine(changeCount);

// : "3"

}}

, , add remove, set .1.6.7.5 , . : , . public static. List : == !=, , List. , List Equals. == List.using System;class Test{

static void Main() {

List a = new List();

a.Add(1);

a.Add(2);

List b = new List();

b.Add(1);

b.Add(2);

Console.WriteLine(a == b);

// : "True"

b.Add(3);

Console.WriteLine(a == b);

// : "False"

}}

Console.WriteLine True, . List ==, Console.WriteLine False, a b List.1.6.7.6 , , . . . . , . . . using.1.7 , , - -. , , . , . . object. , . , -. . , 100 . Point, 101 100 .class Point{

public int x, y;

public Point(int x, int y) {

this.x = x;

this.y = y;

}}

class Test{

static void Main() {

Point[] points = new Point[100];

for (int i = 0; i < 100; i++) points[i] = new Point(i, i);

}}

Point .struct Point{

public int x, y;

public Point(int x, int y) {

this.x = x;

this.y = y;

}}

. Point . new, . ( ), . . , , . . . , , Point .Point a = new Point(10, 10);Point b = a;a.x = 20;Console.WriteLine(b.x); Point , 20, a b . Point , 10, a b , a.x. , . -, , , - , . -, ( ref out), .1.8 , , . , . , . new. new , . 0 Length - 1. new ( null ). int, .using System;class Test{

static void Main() {

int[] a = new int[10];

for (int i = 0; i < a.Length; i++) {

a[i] = i * i;

}

for (int i = 0; i < a.Length; i++) {

Console.WriteLine("a[{0}] = {1}", i, a[i]);

}

}}

. C# . , . -, - .int[] a1 = new int[10];int[,] a2 = new int[10, 5];int[,,] a3 = new int[10, 5, 2]; a1 10 , a2 50 (10 5) , a3 100 (10 5 2) . ( ). , (jagged array, ), , . int:int[][] a = new int[3][];a[0] = new int[10];a[1] = new int[5];a[2] = new int[20]; , int[], null. . new , , { }. int[], .int[] a = new int[] {1, 2, 3}; , { }. , .int[] a = {1, 2, 3}; :int[] t = new int[3];t[0] = 1;t[1] = 2;t[2] = 3;int[] a = t;1.9 , . , , . . , , . . IComboBox ITextBox IListBox.interface IControl{

void Paint();}interface ITextBox: IControl{

void SetText(string text);}interface IListBox: IControl{

void SetItems(string[] items);}interface IComboBox: ITextBox, IListBox {} . EditBox IControl IDataBound.interface IDataBound{

void Bind(Binder b);}public class EditBox: IControl, IDataBound{

public void Paint() {...}

public void Bind(Binder b) {...}}

, . :

EditBox editBox = new EditBox();IControl control = editBox;IDataBound dataBound = editBox; , , . , IControl IDataBound . EditBox, .

object obj = new EditBox();IControl control = (IControl)obj;IDataBound dataBound = (IDataBound)obj; EditBox Paint IControl Bind IDataBound public. C# , public. . , EditBox IControl.Paint IDataBound.Bind .

public class EditBox: IControl, IDataBound{

void IControl.Paint() {...}

void IDataBound.Bind(Binder b) {...}} . , IControl.Paint, EditBox, EditBox IControl.

EditBox editBox = new EditBox();editBox.Paint();

// : IControl control = editBox;control.Paint();

// 1.10 , . Color, : Red, Green Blue.using System;enum Color{

Red, Green, Blue}class Test{

static void PrintColor(Color color) {

switch (color) {

case Color.Red:

Console.WriteLine("");

break;

case Color.Green:

Console.WriteLine("");

break;

case Color.Blue:

Console.WriteLine("");

break;

default:

Console.WriteLine(" ");

break;

}

}

static void Main() {

Color c = Color.Red;

PrintColor(c);

PrintColor(Color.Blue);

}}

, . , int. . , . , . Alignment, sbyte.enum Alignment: sbyte{

Left = -1,

Center = 0,

Right = 1} , , . . , , , . ( ) . :int i = (int)Color.Blue;

// int i = 2;Color c = (Color)2;

// Color c = Color.Blue; , . , . , 0 . , .Color c = 0;1.11 . , . , . , , - . Function.

using System;delegate double Function(double x);class Multiplier{

double factor;

public Multiplier(double factor) {

this.factor = factor;

}

public double Multiply(double x) {

return x * factor;

}}

class Test{

static double Square(double x) {

return x * x;

}

static double[] Apply(double[] a, Function f) {

double[] result = new double[a.Length];

for (int i = 0; i < a.Length; i++) result[i] = f(a[i]);

return result;

}

static void Main() {

double[] a = {0.0, 0.5, 1.0};

double[] squares = Apply(a, Square);

double[] sines = Apply(a, Math.Sin);

Multiplier m = new Multiplier(2.0);

double[] doubles = Apply(a, m.Multiply);

}}

Function , double. Apply Function double[] double[], . Main Apply double[]. (, Square Math.Sin ), (, m.Multiply ). , , . this. , , . . , Multiplier:

double[] doubles = Apply(a, (double x) => x * 2.0); , , . , , .1.12 , C# , . , public, protected, internal private. C# . . HelpAttribute .using System;public class HelpAttribute: Attribute{

string url;

string topic;

public HelpAttribute(string url) {

this.url = url;

}

public string Url {

get { return url; }

}

public string Topic {

get { return topic; }

set { topic = value; }

}}

System.Attribute, .NET Framework. , . Attribute, . , HelpAttribute .[Help("http://msdn.microsoft.com/.../MyClass.htm")]public class Widget{

[Help("http://msdn.microsoft.com/.../MyClass.htm", Topic = "Display")]

public void Display(string text) {}} HelpAttribute Widget, HelpAttribute Display . , . (, Topic ). .using System;using System.Reflection;class Test{

static void ShowHelp(MemberInfo member) {

HelpAttribute a = Attribute.GetCustomAttribute(member,

typeof(HelpAttribute)) as HelpAttribute;

if (a == null) {

Console.WriteLine(" {0}", member);

}

else {

Console.WriteLine(" {0}:", member);

Console.WriteLine(" Url={0}, Topic={1}", a.Url, a.Topic);

}

}

static void Main() {

ShowHelp(typeof(Widget));

ShowHelp(typeof(Widget).GetMethod("Display"));

}}

, , . , .2.

2.1 C# , (9.1). . , . UTF-8. :

: .

: .

: .

2.2 C#, . (2.2.2) , , , , . (2.2.3) , , , C#.2.2.1

. . , .

, . , . , :_while:while ( _ ) _

_while while, (, _, ) _. , . , :_:_ _ , _ . , , . . ::{ _ } :

:{ }{ _ } _, { }. , , one of ( ) , . . , :__: F f D d M m :

__:FfDdMm2.2.2

C# 2.3, 2.4 2.5. . , (2.4), (2.3.3), (2.3.2) (2.5). C# (2.3).2.2.3

C# , . , . , C#. C# _ (9.1).2.3

C#. C# .:__:___ ____:_ ___:__ __: C# : (2.3.1), (2.3.3), (2.3.2), (2.4) (2.5). C# (2.2.3). C# , . , ; , C#. , . , // , , /.2.3.1

C#.

_: (U+000D) (U+000A) (U+000D) (U+000A) xe ": " (U+0085) (U+2028) (U+2029) , , , C# :

CTRL-Z (U+001A), ;

(U+000D) , (U+000D), (U+000A), (U+2028) (U+2029).

2.3.2 : . // . /* */. .:____:// __:__ __: , ____: (U+000D) (U+000A) xe ": " (U+0085) (U+2028) (U+2029)__:/* ___ /___:______ ______:/ _____:* *_____: , / * . /* */ //, // /* . .

/* Hello, world hello, world*/class Hello{

static void Main() {

System.Console.WriteLine("hello, world");

}}

.

// Hello, world// hello, world//class Hello // {

static void Main() { // "Main"

System.Console.WriteLine("hello, world");

}}

.

2.3.3 Zs ( ), , .

: Zs (U+0009) (U+000B) (U+000C)2.4 : , , , . , .:________2.4.1

. (2.4.2), (2.4.4.4) (2.4.4.5). (, , ).__:\u 16__ 16__ 16__ 16__\U16__16__16__16__16__16__16__16__ (escape- ) , , \u \U. C# 16- , U+10000 U+10FFFF . 0x10FFFF . . , \u005Cu005C \u005C, \. \u005C \.

class Class1{

static void Test(bool \u0066) {

char c = '\u0066';

if (\u0066)

System.Console.WriteLine(c.ToString());

}

}

\u0066, escape- f. :class Class1{

static void Test(bool f) {

char c = 'f';

if (f)

System.Console.WriteLine(c.ToString());

}

}

2.4.2 , 15 , : ( C), escape- , @ , .

:_@ ____: ___, ____:__ ____:__ ( U+005F)__:____ ____:_______: Lu, Ll, Lt, Lm, Lo Nl __, Lu, Ll, Lt, Lm, Lo Nl_: Mn Mc __, Mn Mc_: Nd __, Nd_: Pc__, Pc__: Cf__, Cf . , 3.0, 4.5. : identifier1, _identifier2, @if. , C , 15 . C , .

@ , . @ , , . @ . @ , , , .

class @class{

public static void @static(bool @bool) {

if (@bool)

System.Console.WriteLine("true");

else

System.Console.WriteLine("false");

}

}

class Class1{

static void M() {

cl\u0061ss.st\u0061tic(true);

}}

class static, bool. , , , cl\u0061ss , , @class. , :

@, ;

___ ;

__ .

, (U+005F), . , , .2.4.3

. @._: 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

protectedpublicreadonly

ref

return

sbyte

sealedshort

sizeof

stackallocstatic

stringstruct

switch

this

throw

truetry

typeof

uint

ulong

uncheckedunsafe

ushort

using

virtual

voidvolatile

while , . , get set (10.7.2). , get set, , .

2.4.4 .:______null2.4.4.1

: true false._:truefalse_ bool.2.4.4.2

, int, uint, long ulong. : .

_:______:_ ___:__ __: 0 1 2 3 4 5 6 7 8 9__: U u L l UL Ul uL ul LU Lu lU lu__:0x 16__ __0X 16__ __16__:16__16__ 16__16__: 0 1 2 3 4 5 6 7 8 9 A B C D E F a b c d e f :

, , : int, uint, long ulong;

U u, , : uint ulong;

L l, , : long ulong;

UL, Ul, uL, ul, LU, Lu, lU lu, ulong.

ulong, . L l long, l 1. int long : __ 2147483648 (231) __ , (7.6.2), int 2147483648 (231). __ uint; __ 9223372036854775808 (263) __ __ L l , (7.6.2), long 9223372036854775808 (263). __ ulong.2.4.4.3

float, double decimal._:_ . _ _ __. _ _ ___ _ ___ ___:e _E _: + -__: F f D d M m __ , double. : F f float. , 1f, 1.5f, 1e10f 123.456F float;

D d double. , 1d, 1.5d, 1e10d 123.456D double;

M m decimal. , 1m, 1.5m, 1e10m 123.456M decimal. decimal, (4.1.7). , , , ( 0). , 2.900m 0, 2900 3.

, .

float double IEEE. , . , 1.3F , 1.F .2.4.4.4 , 'a'._:' ':___ _escape-escape-__: , ' (U+0027), \ (U+005C) ____: \' \" \\ \0 \a \b \f \n \r \t \v_escape-:\x 16__ 16__ 16__ 16__, (\) , : ', ", \, 0, a, b, f, n, r, t, u, U, x, v. . escape- , , \x. , , U+FFFF, . (2.4) U+0000 U+FFFF.

, .

Escape-

\' 0x0027

\" 0x0022

\\ 0x005C

\0Null0x0000

\a0x0007

\b0x0008

\f 0x000C

\n 0x000A

\r 0x000D

\t 0x0009

\v 0x000B

_ char.1.1.1.1

C# : . , , "hello", ( \t ), escape- escape- . @ , . @"hello". , __. , , escape- escape- . ._:______:" ___ "___:______ ______:_______escape-escape-_____: , " (U+0022), \ (U+005C) ____:@" ___ "___:______ ______:__________: , "__:"", (\) ___, : ', ", \, 0, a, b, f, n, r, t, u, U, x, v. .

string a = "hello, world";

// hello, worldstring b = @"hello, world";

// hello, world

string c = "hello \t world";

// hello worldstring d = @"hello \t world";

// hello \t world

string e = "Joe said \"Hello\" to me";

// Joe said "Hello" to mestring f = @"Joe said ""Hello"" to me";// Joe said "Hello" to me

string g = "\\\\server\\share\\file.txt";// \\server\share\file.txtstring h = @"\\server\share\file.txt";

// \\server\share\file.txt

string i = "one\r\ntwo\r\nthree";string j = @"onetwothree";

. j , . , , , . escape- , "\x123" 123. , 12 3, "\x00123" "\x12" + "3"._ string. . , (7.9.7), , . , class Test{

static void Main() {

object a = "hello";

object b = "hello";

System.Console.WriteLine(a == b);

}}

True, .2.4.4.5 null

_null:null_null .2.4.5 . , . , a + b + a b. .___: {

}

[

]

(

)

.

,

:

;+

-

*

/

%

&

|

^

!

~=

?

??

::

++

--

&&

||->

==

!=

=

+=

-=

*=

/=

%=&=

|=

^=

_shift_:>|>= shift_c _shift_ , , , ( ) . , __ (10.1.3).2.5 , . C C++. C# ; ._:______ _pragma :

#define #undef, , , (2.5.3);

#if, #elif, #else #endif, (2.5.4);

#line, , (2.5.7);

#error #warning, , , (2.5.5);

#region #endregion, (2.5.6);

#pragma, (2.5.8).

# . # # . , #define, #undef, #if, #elif, #else, #endif #line, . ( /* */) , . C#. , C#. , :#define A#undef Bclass C{#if A

void F() {}#else

void G() {}#endif#if B

void H() {}#else

void I() {}#endif}

, :

class C{

void F() {}

void I() {}}

, , , .

2.5.1 , #if, #elif, #else #endif, (2.5.2) ._: ___, true false : . , ( ). #define , , . , #undef , . , #define #undef . true, false. , . , , false. C#. #define #undef .2.5.2 #if #elif. !, ==, !=, && ||, ._: __ __:____ || ____:____ && ____:____ == ____ != ____:__! ____:truefalse_( _ ) true, false. . , (7.18), , , , .2.5.3 .

_: # define _ __ # undef _ ____: _ _ #define , , . #undef , , . #define #undef (2.3.3) , . , #define #undef .

#define Enterprise

#if Professional || Enterprise

#define Advanced#endif

namespace Megacorp.Data{

#if Advanced

class PivotTable {...}

#endif}

, #define ( namespace) . , #define :#define Anamespace N{

#define B

#if B

class Class1 {}

#endif}

#define , - #undef . A , .#define A#define A #undef . A, ; #undef , .#define A#undef A#undef A

2.5.4 .

__:__if __elif __else _endif__if:#if______elif:__elif__elif __elif__elif:#elif______else: # else __ __endif: # endif ___:___:___ ____:_ ___: __ ___: _, # , , , , #if, #elif, #else #endif. . . , .__ _ : _ #if #elif , true. true, _ ; _ false #else, _ #else;

_ .

_, , _: , , ; , . _, , _: , ; , , . _, _, _ ( #if...#endif #region...#endregion) _. , :

#define Debug

// #undef Trace

//

class PurchaseTransaction{

void Commit() {

#if Debug

CheckConsistency();

#if Trace

WriteToLog(this.ToString());

#endif

#endif

CommitHelper();

}}

, . , , #else:#define Debug

//

class PurchaseTransaction{

void Commit() {

#if Debug

CheckConsistency();

#else

/*

#endif

}}

, .

, . , :class Hello{

static void Main() {

System.Console.WriteLine(@"hello, #if Debug

world#else

Nebraska#endif

");

}}

:

hello,#if Debug

world#else

Nebraska#endif _. #if X

/*#else

/* */ class Q { }#endif

(class Q { }), , X . X , - #if #endif. X , (#if, #else #endif) .2.5.5 , , .

_: # error _ # warning __:_ _ _

#warning Code review needed before check-in

#if Debug && Retail

#error A build can't be both debug and retail#endif

class Test {...}

(Code review needed before check-in) (A build cant be both debug and retail), Debug Retail. , _ ; , ; cant.2.5.6

.

_:__ _ ____: # region ___: # endregion _ ; , . , #region #endregion, , . #region #endregion _.

#region...#endregion :

#if true...#endif

2.5.7

, , .

, C# .

_: # line _ ___:_ _ _defaulthidden_:" __ "__:____ ____: _, " #line . #line, _, default, ( , ). #line default #line. , #line . #line hidden , , . #line hidden #line ( #line hidden) . . , _ , escape- ; \ _.2.5.8 pragma #pragma . , #pragma, ._pragma: # pragma __pragma ____pragma:___pragmaC# #pragma . #pragma. C#, C# #pragma; , .2.5.8.1 pragma warning

#pragma warning .___pragma:warning _warning _ __:disablerestore_:__ , _ #pragma warning, , . #pragma warning, , , . #pragma warning disable . #pragma warning restore , . , , #pragma warning restore ( ) . #pragma warning , , C# .using System;

class Program{

[Obsolete]

static void Foo() {}

static void Main() {#pragma warning disable 612

Foo();#pragma warning restore 612

}}

3.

3.1 .

. . , . . , , . , , , , . , , . . , , . Main :static void Main() {...}

static void Main(string[] args) {...}

static int Main() {...}

static int Main(string[] args) {...}

, int. (3.2). . , string[]. string[], , . string[] null, , . C# , - , . Main, . Main , string[]. . Main, . Main (, ). C# . , (3.5.1) (10.3.5), , , , , . , , . . , . . .3.2 .

. int, . , , . void, (}), , return 0. , , , (, GC.SuppressFinalize).3.3 .

C# . C# (9), . (9.6) (10), (10.14), (13), (14) (15). , , . , (10.4), (10.5), (10.6), (10.7), (10.8), (10.9), (10.10), (10.11), (10.12), (10.13) (10.3.8). , . (3.6), , , . , . , . , . ___ __ , . ___ __ . , . __, __, __ _. , . , . , . , , , (3.6). , , . . . . _. . ___. xe "declaration space:block and" \b

xe "block:declaration space of a" \t "See declaration space, block and" xe "declaration space:switch block and" \b

xe "switch block:declaration space of a" \t "See declaration space, switch block", for, foreach using , xe "variable:local:declaration" \b. __ __. , , get set , , , xe "declaration space:local variable" \b. , _, , . . . , . , . switch_ . _, _goto. . . , , . , , , , , , , , , , . . ( ). (3.7). (14.3) , _. , . :namespace Megacorp.Data{

class Customer

{

...

}}

namespace Megacorp.Data{

class Order

{

...

}}

, Megacorp.Data.Customer Megacorp.Data.Order. , , . , . F G , i . H I , i .class A{

void F() {

int i = 0;

if (true) {

int i = 1;

}

}void G() {

if (true) {

int i = 0;

}

int i = 1;

}

void H() {

if (true) {

int i = 0;

}

if (true) {

int i = 1;

}

}

void I() {

for (int i = 0; i < 10; i++)

H();

for (int i = 0; i < 10; i++)

H();

}}

3.4 .

. , , ., . , . , , , , . , , , . , (3.5.1), , (3.7.1.2).3.4.1 .

, , . , . , , . , . . , , .3.4.2 .

, , , System.ValueType object. . sbyte System.SByte. byte System.Byte. short System.Int16. ushort System.UInt16. int System.Int32. uint System.UInt32. long System.Int64. ulong System.UInt64. char System.Char. float System.Single. double System.Double. decimal System.Decimal. bool System.Boolean.3.4.3 .

, , , System.Enum System.ValueType object.3.4.4 .

, , , ( object, ). , , , , , , , , , , . . , , , , , , , , , . object string , . object System.Object. string System.String.3.4.5 .

, . object, , - (13.2). object (7.3).3.4.6 .

, System.Array.3.4.7 .

, System.Delegate.3.5 .

. (3.5.1) , . , . , , . , , , (3.5.2) .3.5.1 .

. , public . public . , protected . protected - , -. , internal . internal . ( ), protected internal . protected internal , -. , private . private -. , , . , , , , . public. . , , public internal, internal. , private. ( , , , , , , public internal.) public, internal private, private, . , (. . ), protected protected internal. ( , , , public, internal private, , , public internal.) public. . public. .3.5.2 .

( ) , . , , , . , , , {" "} _, _, _ _ (, , , ). (, object, int double) . T (4.4.3), P, . T public, T P , P. T internal, T P. , , . T T A1, ...,AN. M, T P, (, M ). M public, M T. M protected internal, D P , T, P. M T D. M protected, D T , T. M T D. M internal, M T P. M private, M T. , , . , , , . , M . , M ( ), . , M public, . , M protected internal, , , M, , , M, (3.5.3). , M protected, , , M, , , M, (3.5.3). , M internal, , , M. , M private, , , M. , . public class A{

public static int X;

internal static int Y;

private static int Z;}

internal class B{

public static int X;

internal static int Y;

private static int Z;

public class C

{

public static int X;

internal static int Y;

private static int Z;

}

private class D

{

public static int X;

internal static int Y;

private static int Z;

}}

. A A.X . A.Y, B, B.X, B.Y, B.C, B.C.X B.C.Y . A.Z A. B.Z B.D B, B.C B.D. B.C.Z B.C. B.D.X B.D.Y B, B.C B.D. B.D.Z B.D. , . , , X , A.X , -. 3.4 , , , . . , . class A{

int x;

static void F(B b) {

b.x = 1;

// Ok

}}

class B: A{

static void F(B b) {

b.x = 1;

// , x

}}

B x A. , _ A. b.x A.F, B.F.3.5.3 .

protected , , protected internal , , , , . . , . B , M, D B. _ D M . _ _ M. _ E.M , E T T , T D , D. _ base.M. _ (10.11.1). public class A{

protected int x;

static void F(A a, B b) {

a.x = 1;

// Ok

b.x = 1;

// Ok

}}

public class B: A{

static void F(A a, B b) {

a.x = 1;

// , B

b.x = 1;

// Ok

}}

A x A B, A A . B x A, A B. class C{

protected T x;}

class D: C{

static void F() {

D dt = new D();

D di = new D();

D ds = new D();

dt.x = default(T);

di.x = 123;

ds.x = "test";

}}

x , , .3.5.4 .

C# , . , T M, T M. , T M, T , M. . , . , . , . , . , . , . , . , . , . , . , . class A {...}

public class B: A {...}

B , A , B., class A {...}

public class B{

A F() {...}

internal A G() {...}

public A H() {...}}

H B , A , .3.6 .

, , . , (, ) , . , , , . , params, , . (, ) , . params, . , . paramsxe "parameter array:signature and" \b, . , . . , . , , , . , . , , , . , . , out ref , , , ref out. , , , out ref, . (, ) ref out . ( C# Common Language Infrastructure (CLI), , ref out.) .interface ITest{

void F();

// F()

void F(int x);

// F(int)

void F(ref int x);

// F(ref int)

void F(out int x);

// F(out int)

void F(int x, int y);

// F(int, int)

int F(string s);

// F(string)

int F(int x);

// F(int)

void F(string[] a);

// F(string[])

void F(params string[] a);

// F(string[])

}

, ref out (10.6.1) . F(int) F(ref int) . F(ref int) F(out int) , ref out. , params , params. , F(int) F(params string[]), , .

3.7 .

, , . , ( , 3.3, , ). , , . , ___ (9.5), __ . , ___ __ N, __ __ N, N . , __ xe "using-directive:scope of a" \b, _, _ ___ . __ - . , __ , , . , _ (9.4), _____ __, _. _ , _ __, . , _ , _ __, . , __ _ (10.1), _, ___ _ _. , __ _ (11.1), _, ___ _ _. , __ _ (13.1), _, ___ _ _. , __ _ (15.1), __, __ ___ _. , __ (10.1.6), _, . , _ , (3.5.2) . , __ (11.2), _, . , ___ (14.3), __, . , _ (10.6), _ _. , _ (10.9), __ _. , _ (10.10), _. , _ (10.11), _ _. , _ (), __ _. , __ (), __. , _ (8.4), , . , __ (8.5.1), , . , _ switch (8.7.2), _. , _for for (8.8.3), _for, _for, _for for. , __ (8.5.2), , . , _, . , _foreach, _using, _lock _, . , , , . :class A{

void F() {

i = 1;

}

int i = 0;}

F i . , __ , . :class A{

int i = 0;

void F() {

i = 1;

// ,

int i;

i = 2;

}

void G() {

int j = (j = 1);

//

}

void H() {

int a = 1, b = ++a;//

}}

F i , . , , . G j j , __. H __ , __ __. , . , , , , , , . , . using System;

class A {}

class Test{

static void Main() {

string A = "hello, world";

string s = A;

//

Type t = typeof(A);

//

Console.WriteLine(s);

// "hello, world"

Console.WriteLine(t);

// "A"

}}

A A A.3.7.1 .

, . , , , . , . , , . , . .3.7.1.1 .

, . class A{

int i = 0;

void F() {

int i = 1;

}

void G() {

i = 1;

}} F i i, G i . , . class Outer{

static void F(int i) {}

static void F(string s) {}

class Inner

{

void G() {

F(1);

// Outer.Inner.F

F("Hello");

// .

}

static void F(long l) {}

}}

F(1) F, Inner, F . F("Hello") .3.7.1.2 . , , . . , , , , , .

, , , , ( , ).

, , ( ).

, (10.10), , . . , . class Base{

public void F() {}}

class Derived: Base{

public void F() {}

// , }

F Derived . , . , , Base F, . , , , ., , new:class Base{

public void F() {}}

class Derived: Base{

new public void F() {}}

new , F Derived , . .class Base{

public static void F() {}}

class Derived: Base{

new private static void F() {}// Base.F Derived}

class MoreDerived: Derived{

static void G() { F(); }

// Base.F}

F Derived F, Base, , F Derived , MoreDerived. F() MoreDerived.G , Base.F.3.8 . C# __ .xe "name:qualified" \b__:_____:______ :_opt______ . _op_ _____ ____, . ____ __ , . (4.4.1) __ ( )._ ____, . ____ _ , . ____ , 9.7. ____ . I I N.I N.I

I , N ____ __. __ , K . ____ . ____ I I.

K , ____ (10.6), (10.1.3) I, ____ . , ____ , T (10.3.1), , ( ): K , T I, ____ . , T I K, ____ , . , . , , (, , , , , , , , ), ____. , N, , ____, ( ), , . K , I N. , ____, N, __ __, I , ____ , . ____ I N. , N I K. K , ____, N, __ __, I , ____ , . , ____ , . , , ____, N. K __ __, I , ____ . , , ___, I K, ____ , . , , ___, I K, ____ , . ____ , . , ____ N.I N.I. N ____. N , . N.I N.I . K N I, ____ . , N I K, ____ , . , N (, ) I K, ____ , . , . N.I ____, . ____ (10.1.1.3) . ____ T ____ T.I ____ T _typeof (7.5.11) typeof(T).3.8.1 .

, . N . N , N. S.N, S , N . , N , N, . , . .class A {}

// A

namespace X

// X{

class B

// X.B

{

class C {}

// X.B.C

}

namespace Y

// X.Y

{

class D {}

// X.Y.D

}}

namespace X.Y

// X.Y{

class E {}

// X.Y.E}

3.9 .

C# , , . . . , , . , , . C# , . , , , , ( ) . , , (10.13) ( ) . . , , . , , , , . , , , . , , C# , . , C# , , , . System.GC. , ( ) . .

, , . using System;

class A{

~A() {

Console.WriteLine("Destruct instance of A");

}}

class B{

object Ref;

public B(object o) {

Ref = o;

}

~B() {

Console.WriteLine("Destruct instance of B");

}}

class Test{

static void Main() {

B b = new B(new A());

b = null;

GC.Collect();

GC.WaitForPendingFinalizers();

}}

A B. , b null, . :Destruct instance of A ( A)Destruct instance of B ( A)

Destruct instance of BDestruct instance of A

, . . ,

using System;

class A{

~A() {

Console.WriteLine("Destruct instance of A");

}

public void F() {

Console.WriteLine("A.F");

Test.RefA = this;

}}

class B{

public A Ref;

~B() {

Console.WriteLine("Destruct instance of B");

Ref.F();

}}

class Test{

public static A RefA;

public static B RefB;

static void Main() {

RefB = new B();

RefA = new A();

RefB.Ref = RefA;

RefB = null;

RefA = null;

// A B

GC.Collect();

GC.WaitForPendingFinalizers();

// B , A

if (RefA != null)

Console.WriteLine("RefA is not null");

}}

, A B, :Destruct instance of ADestruct instance of BA.FRefA is not null (RefA null)

, , , A , A , A ( F) . , , . B , A, , Test.RefA. WaitForPendingFinalizers B , A - Test.RefA. , , , . System.IDisposable. , using (8.13).3.10 xe " Execution Order " \b C# , . xe " side effect" \b volatile, volatile, . , , volatile (10.5.3), lock (8.12) . C#, . . , , . (10.5.4 10.5.5). volatile (10.5.3). , , , , ( volatile). (, ) .

4.

C# : . , . , .:___

. . 18.2.

, , (). . , , . . .

C# , . C# object, . object. (4.3).

4.1

: . C# , . ._:__

_:___

_:_bool_:____decimal_:sbytebyteshortushortintuintlongulongchar ___:floatdouble_:__ ?__:

_:_

, null , . , null. . , , .4.1.1 System.ValueType

System.ValueType, , , object. . , (10.1.1.2).

, System.ValueType _. _, _.

4.1.2

, . , .

_ , :

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

char '\x0000'.

float 0.0f.

double 0.0d.

decimal 0.0m.

bool false.

_ E 0 E.

_ , , null.

_ , HasValue false, Value . .

, new. . i j .

class A{

void F() {

int i = 0;

int j = new int();

}}

, . , (11.3.8).4.1.3

, , , , , , , , . . 11.1.4.1.4

C# , . . System (. ).

sbyteSystem.SByte

byteSystem.Byte

shortSystem.Int16

ushortSystem.UInt16

intSystem.Int32

uintSystem.UInt32

longSystem.Int64

ulongSystem.UInt64

charSystem.Char

floatSystem.Single

doubleSystem.Double

boolSystem.Boolean

decimalSystem.Decimal

, . , int , System.Int32, , System.Object. :int i = int.MaxValue;

// System.Int32.MaxValue constantstring s = i.ToString();

// System.Int32.ToString() instance methodstring t = 123.ToString();

// System.Int32.ToString() instance method , : (2.4.4). , 123 int, 'a' char. C# . .

, , . _ (7.18). , , , .

const (10.4). , static readonly.

, , . (6.4.3).4.1.5

C# : sbyte, byte, short, ushort, int, uint, long, ulong char. : sbyte 8- 128 127. byte 8- 0 255. short 16- 32768 32767. ushort 16- 0 65535. int 32- 2147483648 2147483647. uint 32- 0 4294967295. long 64- 9223372036854775808 9223372036854775807. ulong 64- 0 18446744073709551615. char 16- 0 65535. char . , char ushort , . , 32- 64- : + ~ T, T int, uint, long ulong, . , T. T. T, T int long, . , T. T. ulong. +, , *, /, %, &, ^, |, ==, !=, >, = T, T int, uint, long ulong, . , T. T. char , : char. , , sbyte, byte ushort char, sbyte, byte ushort char . char _ char. , (char)10 '\x000A' . checked unchecked (7.5.12). checked System.OverflowException. unchecked , , , .

4.1.6

C# : float double. float double 32- ( ) 64- ( ) IEEE 754 :

. . (7.7.2).

. , , . , 1.0 / 0.0 , 1.0 / 0.0 .

NaN. NaN , .

sxmx2e, s 1 1, m e , float 0 < m < 224 149 e 104, double 0 < m < 253 1075 e 970. .

float 1,5x1045 3,4x1038 () 7 .

double 5,0x10324 1,7x10308 () 1516 .

, . : , , . , double, double. , double. double ( bool ).

, float. float ( bool ).

, , . , , :

, .

, .

, NaN.

NaN, NaN.

, . , extended long double, double. . ( ). , C# . - , . x * y / z, , double, , double, .

4.1.7 decimal

decimal 128- , . decimal 1,0x1028 7,9x1028 () 2829 . decimal (1)sxcx10-e, s 0 1, c 0c

_:__ , _

_:

(18) _ . , (10.1.5).4.4.2

. , . : . , . , . , . . , , , . . . . , . , , , . , .4.4.3

. . , . , , . typeof (7.5.11).

4.4.4

, (10.1.5). where A, , :

, , C, . , . , A C : (6.1.1). (6.1.6)

(6.1.7), A .

, A C.

(class), A :

A , , . , System.ValueType System.Enum .

A , (10.1.5). (struct), A : A . , System.ValueType System.Enum . A , (10.1.5). new(), A abstract . , : A , (4.1.2). A , (10.1.5). A , (10.1.5). A , abstract, public, . A abstract (10.11.4). , . , . T D. T , B. , E , List IEnumerable T.class B where T: IEnumerable {...}class D: class B where T: IEnumerable {...}class E: B {...}4.5

, , ._:

, , , . : (10.2.4) (13.1.3).

( ). . 7.3. ( ). . 6.1.9 6.2.6.

null , , , (6.1.9). default (7.5.13). , , , , , null == != (7.9.6). new (7.5.10.1) , _ (10.1.5).

.

(7.5.4) (3.8) .

_ (18.2).

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

4.6

. System.Linq.Expressions.Expression, D . Expression.

D, Expression. , . .

, .

Expression , D. , . Func, Expression:Func del = x => x + 1;

// Expression exp = x => x + 1;

// del , x + 1, exp , x => x + 1. Expression, . : . , , . - , . Expression Compile, D:

Func del2 = exp.Compile();

, . , , del del2 , :int i1 = del(1);

int i2 = del2(1);

i1 i2 2.

5.

. , , . C# , C# , . ++ - -. , (5.3). , . . . , , .5.1

C# : , , , , , . . class A{

public static int x;

int y;

void F(int[] v, int a, ref int b, out int c) {

int x = 1;

c = a + b++;

}}

x , y , v[0] , a , b , c , i .5.1.1

, static, . (10.12) . , (5.2). .5.1.2

, static, .5.1.2.1

, ( ). , (5.2). .5.1.2.2

, , . , . , . , , . , .5.1.3

. (5.2). .5.1.4

, ref out, . - (, , ) , , , . , , - . , (7.14), , , , . .5.1.5

, ref, . . , - . , . . , 5.1.6. (5.3), - .

- .

struct this , struct (7.5.7).5.1.6

, out, . . , - . , . . , 5.1.5. - .

- , , .

- .

- (5.3) - .

struct this , struct (7.5.7).5.1.7

__, , _for, _switch _using, _foreach __catch _try. , . , for, switch, using, foreach __catch, , , for, switch, using, foreach __catch . ( , , for, switch, using, foreach __catch.) (7.14.4.1), , , , , , . , for, switch, using, foreach __catch , , __ ( ). , __, . , __, . __ __. , __. , __, , __, . (8.5.1), __ . , _foreach __catch, . . , , . , , . , , (3.9).5.2

. .

.

.

: ___, , ___ (4.1.2).

_ null.

, , . , .5.3

- , (5.3.3) , . , : (5.3.1) .

(5.3.2) , , , :

(7.16.1), .

(7.5.5) (7.5.10.1), .

(8.5.1), .

, , 5.3.1, 5.3.2 5.3.3. _struct , . _struct : , _struct .

_struct , .

: , . . :

,

,

_struct .

, . , - .

- , - ( return -). , - , -, , .

this _struct , .

5.3.1

: .

.

struct .

.

.

.

, catch foreach.

5.3.2

: struct .

, this struct.

, catch foreach.

5.3.3

, , , . -, . v v -:

(8.1)

,

v : . , v .

. bool ( ) :

true. , v , true, , false.

false. , v , false, , true.

v .5.3.3.1

v -.

v .

v v , . , v , . , (8.1).

v checked, unchecked, if, while, do, for, foreach, lock, using switch v , . v , . v . , (8.1).

5.3.3.2 ,

v , ( , ) v , checked unchecked.5.3.3.3

stmt, expr: v expr, stmt; v expr, stmt; stmt.

5.3.3.4

stmt , v stmt, .

stmt , v ( ).

5.3.3.5 If

if :if ( expr ) then-stmt else else-stmt v expr, stmt;

v expr, then-stmt, else-stmt, stmt, else ;

v true expr, then-stmt else-stmt, stmt, else ;

v false expr, else-stmt then-stmt. stmt , then-stmt;

v then-stmt else-stmt stmt, else .

5.3.3.6 switch

switch stmt expr: v expr v stmt; v switch v expr.

5.3.3.7 while

while stmt :while ( expr ) while-body v expr, stmt; v expr, while-body stmt;

v true expr, while-body, stmt; v false expr, stmt, while-body.

5.3.3.8 do

do stmt :do do-body while ( expr ) ;

v stmt do-body, stmt.

v expr, do-body.

v expr, stmt.

v false expr, stmt.

5.3.3.9 for

for for ( for-initializer ; for-condition ; for-iterator ) embedded-statement

:{

for-initializer ;

while ( for-condition ) {

embedded-statement ;

for-iterator ;

}}

_for for , , _for true.5.3.3.10 break, continue goto

v , break, continue goto, v .5.3.3.11 throw

stmt throw expr ;

v expr v stmt.

5.3.3.12 return

stmt return expr ;

v expr v stmt.

v , :

expr finally try-finally try-catch-finally, return.

stmt

return ;

v , :

stmt finally try-finally try-catch-finally, return.

5.3.3.13 try-catch

stmt

try try-blockcatch(...) catch-block-1...catch(...) catch-block-n v try-block v stmt.

v catch-block-i ( i) v stmt.

v stmt , v try-block catch-block-i ( i 1 n).

5.3.3.14 try-finally

stmt

try try-block finally finally-block v try-block v stmt.

v finally-block v stmt.

v stmt :

v try-block v finally-block (, goto), try-block , v , v finally-block. ( v , .)

5.3.3.15 try-catch-finally

try-catch-finally,

try try-blockcatch(...) catch-block-1...catch(...) catch-block-n , try-finally try-catch:

try {

try try-block

catch(...) catch-block-1

...

catch(...) catch-block-n}finally finally-block , try (8.10) . xe "assignment:definite:try and" \bclass A{

static void F() {

int i, j;

try {

goto LABEL;

// i, j

i = 1;

// i

}

catch {

// i, j

i = 3;

// i

}

finally {

// i, j

j = 5;

// j

}

// i j

LABEL:;

// j

}}

5.3.3.16 foreach

foreach stmt

foreach ( type identifier in expr ) embedded-statement v expr v stmt.

v _ stmt v expr.

5.3.3.17 using

using stmt

using ( _ ) _ v resource-acquisition v stmt.

v _ v _.

5.3.3.18 lock

lock stmt

lock ( expr ) _ v expr v stmt.

v _ v expr.

5.3.3.19 yield

yield return stmt

yield return expr ;

v expr v stmt.

v stmt v expr.

yield break .

5.3.3.20

: (7.5.1), (7.5.2), (7.5.4), (7.5.8), typeof (7.5.11) (7.5.13).

v v .

5.3.3.21

: (7.5.3), (7.5.6), (7.5.8), (7.5.9, 7.6.5), (7.6.6), +, -, ~, *, +, -, *, /, %, , =, ==, !=, is, as, &, |, ^ (7.7, 7.8, 7.9, 7.10), (7.16.2), checked unchecked (7.5.12), (7.5.13).

, . , % , . , . expr, expr1, expr2, ..., exprn, :

v expr1 expr.

v expri (i ) expri-1.

v expr exprn.

5.3.3.22

expr

_ ( arg1 , arg2 , , argn )

new type ( arg1 , arg2 , , argn )

v _ v expr; v arg1 v _; v arg1 v expr; argi v argi ref out;

argi i v argi v argi-1;

v out (, out v) , v expr . v expr v argn;

(7.5.10.4), (7.5.10.2), (7.5.10.3) (7.5.10.6) , .

5.3.3.23

expr w = expr-rhs:

v expr-rhs v expr.

w , v, v expr . v expr v expr-rhs.

5.3.3.24 &&

expr expr-first && expr-second:

v expr-first v expr;

v expr-second , v expr-first true. ;

v expr :

v expr-first , v expr ;

, v expr-second , v expr-first false, v expr ;

, v expr-second true, v expr true;

, v expr-first false, expr-second false, expr false;

v expr .

:

class A{

static void F(int x, int y) {

int i;

if (x >= 0 && (i = y) >= 0) {

// i

}

else {

// i

}

// i

}}

i if. if F i , (i = y) . , i , x >= 0 false, i .

5.3.3.25 ||

expr expr-first || expr-second:

v expr-first v expr;

v expr-second , v expr-first false. ;

v expr :

v expr-first , v expr ;

, v expr-second , v expr-first true, v expr ;

, v expr-second false, v expr false;

, v expr-first true, expr-second true, v expr true;

v expr .

class A{

static void G(int x, int y) {

int i;

if (x >= 0 || (i = y) >= 0) {

// i

}

else {

// i

}

// i

}}

i if. if G i , (i = y) . , i , x >= 0 true, i .

5.3.3.26 ! !

expr ! expr-operand:

v expr-operand v expr.

v expr :

v expr-operand , expr ;

v expr-operand , expr ;

v expr-operand false, expr true;

v expr-operand true, expr false.

5.3.3.27 ??

expr expr-first ?? expr-second:

v expr-first expr;

v expr-second expr-first.

v expr :

expr-first (7.18), null, v expr expr-second; v expr expr-first.

5.3.3.28

expr expr-cond ? expr-true : expr-false:

v expr-cond expr;

v expr-true , expr-cond true;

v expr-false , expr-cond false;

v expr :

expr-cond (7.18), true, v expr expr-true; , expr-cond (7.18), false, v expr expr-false; , v expr-true, expr-false, expr ;

v expr .

5.3.3.29

- __ expr , :

v body expr. , .

v expr expr.

:

delegate bool Filter(int i);

void F() {

int max;

// , max

Filter f = (int n) => n < max;

max = 5;

DoWork(f);}

, max . :

delegate void D();

void F() {

int n;

D d = () => { n = 1; };

d();

// , n

Console.WriteLine(n);}

, n n .

5.4

__ , . __ , , .

__:

C C++ __ lvalue.5.5

: bool, char, byte, sbyte, short, ushort, uint, int, float . . , long, ulong, double decimal, , . , , , , .

6. . , . , . , int long , int long. long int .int a = 123;long b = a;

// int longint c = (int) b;// long int . (6.4).6.1 : .

.

.

.

null.

.

.

.

.

.

.

, (7.4.4), (7.6.6) (7.16). . .6.1.1 . , , .6.1.2 : sbyte short, int, long, float, double decimal. byte short, ushort, int, uint, long, ulong, 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, uint, long ulong float, long ulong double , . . char , char.6.1.3 __ _decimal 0 _ _, _. _ (4.1.9).6.1.4 . , S T, : S? T?. S T?. , S T, : S? T?: null ( HasValue false), null T?.

S? S, S T (4.1.9) T T?.

S T?, S T T T?.6.1.5 null null . null (4.1.9) .6.1.6 : _ object. _ S _ T ( S T). _ S _ T ( S T). _ S _ T ( S T). _ S, SE, _ T, TE, : S T . , S T .

SE TE _.

SE TE.

_ System.Array. S[] System.Collections.Generic.IList , S T. _ System.Delegate. null _. , , . , , . 6.1.9. _ . . , , , . , . , List ( , ) List, B A. , List List.6.1.7 _ . __ object, System.ValueType, _, __. , _ System.Enum. _ , __ . __ . System.ValueType, (11.3.2). _ : null ( HasValue false), null . T, . . 4.3.1.6.1.8 : _ (7.18) int sbyte, byte, short, ushort, uint ulong, _ . _ long ulong, _ .6.1.9 , T: T C, T C , T , C. T , . . T I, T, T I. T , . . T U, T U. T , U , . , T U , T U , . T , U , . null T, T . T (10.1.5), (6.1.6). , T , , , (6.1.7).6.1.10 , , . 6.4.4.6.1.11 , . . 6.5 6.6 .6.2 : . . . . . . . . (7.6.6). . ,