Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
C-sharp language specification.2004.pdf
Скачиваний:
14
Добавлен:
23.08.2013
Размер:
2.55 Mб
Скачать

Chapter 8 Language overview

1public class Stack

2{

3

private Node GetNode(int index) {

4

Node temp = first;

5

while (true) {

6

if (temp == null || index < 0)

7

throw new Exception("Index out of range.");

8

if (index == 0)

9

return temp;

10

temp = temp.Next;

11

index--;

12

}

13

}

14

public object this[int index] {

15

get {

16

return GetNode(index).Value;

17

}

18

set {

19

GetNode(index).Value = value;

20

}

21

}

22

23}

24class Test

25{

26

static void Main() {

 

27

Stack s = new Stack();

 

28

s.Push(1);

 

 

29

s.Push(2);

 

 

30

s.Push(3);

 

 

31

s[0] = 33;

// Changes the top item from 3 to 33

32

s[1] = 22;

// Changes the middle item from 2

to 22

33

s[2] = 11;

// Changes the bottom item from 1

to 11

34}

35}

36shows an indexer for the Stack class.

378.7.8 Instance constructors

38An instance constructor is a member that implements the actions required to initialize an instance of a class.

39The example

40using System;

41class Point

42{

43

public double x, y;

44

public Point() {

45

this.x = 0;

46

this.y = 0;

47

}

48

public Point(double x, double y) {

49

this.x = x;

50

this.y = y;

51

}

52

public static double Distance(Point a, Point b) {

53

double xdiff = a.x - b.x;

54

double ydiff = a.y - b.y;

55

return Math.Sqrt(xdiff * xdiff + ydiff * ydiff);

56

}

39

 

C# LANGUAGE SPECIFICATION

1

public override string ToString() {

2

return string.Format("({0}, {1})", x, y);

3}

4}

5class Test

6{

7

static void Main() {

8

Point a = new Point();

9

Point b = new Point(3, 4);

10

double d = Point.Distance(a, b);

11

Console.WriteLine("Distance from {0} to {1} is {2}", a, b, d);

12}

13}

14shows a Point class that provides two public instance constructors, one of which takes no arguments, while

15the other takes two double arguments.

16If no instance constructor is supplied for a class, then one having no parameters is automatically provided,

17which simply invokes the parameterless constructor of the direct base class.

188.7.9 Destructors

19A destructor is a member that implements the actions required to destruct an instance of a class. Destructors

20cannot have parameters, they cannot have accessibility modifiers, and they cannot be called explicitly. The

21destructor for an instance is called automatically during garbage collection.

22The example

23using System;

24class Point

25{

26

public double x, y;

27

public Point(double x, double y) {

28

this.x = x;

29

this.y = y;

30

}

31

~Point() {

32

Console.WriteLine("Destructed {0}", this);

33

}

34

public override string ToString() {

35

return string.Format("({0}, {1})", x, y);

36}

37}

38shows a Point class with a destructor.

398.7.10 Static constructors

40A static constructor is a member that implements the actions required to initialize a class. Static constructors

41cannot have parameters, they cannot have accessibility modifiers, and they cannot be called explicitly. The

42static constructor for a class is called automatically.

43The example

44using Personnel.Data;

45class Employee

46{

47

private static DataSet ds;

48

static Employee() {

49

ds = new DataSet(…);

50

}

40

Chapter 8 Language overview

1

public

string Name;

2

public

decimal Salary;

3

4}

5shows an Employee class with a static constructor that initializes a static field.

68.7.11 Inheritance

7Classes support single inheritance, and the type object is the ultimate base class for all classes.

8The classes shown in earlier examples all implicitly derive from object. The example

9using System;

10class A

11{

12public void F() { Console.WriteLine("A.F"); }

13}

14shows a class A that implicitly derives from object. The example

15class B: A

16{

17public void G() { Console.WriteLine("B.G"); }

18}

19class Test

20{

21

static void Main() {

22

B b = new B();

 

23

b.F();

// Inherited from A

24

b.G();

// Introduced in B

25

 

 

26

A a = b;

// Treat a B as an A

27

a.F();

 

28}

29}

30shows a class B that derives from A. The class B inherits A’s F method, and introduces a G method of its own.

31Methods, properties, and indexers can be virtual, which means that their implementation can be overridden

32in derived classes. The example

33using System;

34class A

35{

36public virtual void F() { Console.WriteLine("A.F"); }

37}

38class B: A

39{

40

public override void F() {

41

base.F();

42

Console.WriteLine("B.F");

43}

44}

45class Test

46{

47

static void Main() {

48

B b = new B();

49

b.F();

50

A a = b;

51

a.F();

52}

53}

54shows a class A with a virtual method F, and a class B that overrides F. The overriding method in B contains

55a call, base.F(), which calls the overridden method in A.

41

C# LANGUAGE SPECIFICATION

1A class can indicate that it is incomplete, and is intended only as a base class for other classes, by including

2the modifier abstract. Such a class is called an abstract class. An abstract class can specify abstract

3members—members that a non-abstract derived class shall implement. The example

4using System;

5abstract class A

6{

7public abstract void F();

8}

9class B: A

10{

11public override void F() { Console.WriteLine("B.F"); }

12}

13class Test

14{

15

static void Main() {

16

B b = new B();

17

b.F();

18

A a = b;

19

a.F();

20}

21}

22introduces an abstract method F in the abstract class A. The non-abstract class B provides an implementation

23for this method.

248.7.12 Static classes

25Classes that are not intended to be instantiated, and which contain only static members should be declared as

26static classes. Examples of such classes are System.Console and System.Environment. Static classes

27are implicitly sealed and have no instance constructors. Static classes can be used only with the typeof

28operator and to access elements of the class. In particular, a static class cannot be used as the type of a

29variable or be used as a type argument.

30public static class Months

31{

32

static Months() { … }

33

private static readonly string[] monthName = { … }

34

public static string GetMonthName(int mm) { … }

35

private static readonly int[,] daysInMonth = { … }

36

public static int GetDaysInMonth(bool isLeapYear, int mm) { … }

37public static bool IsLeapYear(int yy) { … }

38}

398.7.13 Partial type declarations

40In certain situations, the declaration of a type may grow so large that keeping it in a single source file

41becomes impractical or difficult. In such cases, it is often desirable to split that class declaration into

42multiple source files, with each source file focussing on one or more semi-independent concerns.

43Another common situation occurs when code is generated from a program rather than written by a person. In

44rich frameworks and development environments, it is often most efficient to have parts of a project’s source

45code generated automatically from visual form designers, database schemas, RPC descriptions, etc. While

46these kinds of tools produce huge productivity gains, they suffer from problems when we wish to customize

47the output, possibly by adding members to generated classes. If we directly modify the output of the code

48generator, then those changes will be lost if the code generator needs to be run again. By placing the

49customized additions in a different source file, lost modifications can be greatly reduced or eliminated.

50Partial type declarations allow greater flexibility in these situations by allowing the definition of a class,

51struct, or interface to be split into as many different pieces as needed. For example, when the following

52source files are compiled together:

42

Соседние файлы в предмете Электротехника