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

 

Chapter 8 Language overview

 

 

 

checked and unchecked

static void Main() {

 

statements

int x = Int32.MaxValue;

 

Console.WriteLine(x + 1);

// Overflow

 

 

checked {

 

 

Console.WriteLine(x + 1);

// Exception

 

}

 

 

unchecked {

 

 

Console.WriteLine(x + 1);

// Overflow

 

}

 

 

}

 

 

 

 

lock statements

static void Main() {

 

 

A a = …;

 

 

lock(a) {

 

 

a.P = a.P + 1;

 

 

}

 

 

}

 

 

 

 

using statements

static void Main() {

 

 

using (Resource r = new Resource()) {

 

r.F();

 

 

}

 

 

}

 

 

 

 

1

28.7 Classes

3Class declarations define new reference types. A class can inherit from another class, and can implement

4interfaces. Generic class declarations (§26.1) have one or more type parameters.

5Class members can include constants, fields, methods, properties, events, indexers, operators, instance

6constructors, destructors, static constructors, and nested type declarations. Each member has an associated

7accessibility (§10.5), which controls the regions of program text that are able to access the member. There

8are five possible forms of accessibility. These are summarized in the table below.

9

Form

Intuitive meaning

 

 

public

Access not limited

 

 

protected

Access limited to the containing class or types derived from the containing class

 

 

internal

Access limited to this program

 

 

protected

Access limited to this program or types derived from the containing class

internal

 

private

Access limited to the containing type

 

 

10

11The example

12using System;

13class MyClass

14{

15

public

const int MyConst = 12;

16

public

int MyField = 34;

17

public

static int MyStaticField = 34;

18

public void MyMethod(){

19

Console.WriteLine("MyClass.MyMethod");

20

}

 

21

public int MyProperty {

22

get {

23

return MyField;

24

}

 

31

 

C# LANGUAGE SPECIFICATION

1

set {

2

MyField = value;

3

}

4

}

5

public event EventHandler MyEvent;

6

public int this[int index] {

7

get {

8

return 0;

9

}

10

set {

11

Console.WriteLine("this[{0}] = {1}", index, value);

12

}

13

}

14

public static MyClass operator+(MyClass a, MyClass b) {

15

return new MyClass(a.MyField + b.MyField);

16

}

17

public MyClass() {

18

Console.WriteLine("Instance constructor");

19

}

20

public MyClass(int value) {

21

MyField = value;

22

Console.WriteLine("Instance constructor");

23

}

24

~MyClass() {

25

Console.WriteLine("Destructor");

26

}

27

static MyClass() {

28

MyStaticField *= 2;

29

Console.WriteLine("Static constructor");

30

}

31

internal class MyNestedClass

32{}

33}

34shows a class that contains each kind of member. The example

35class Test

36{

37

static void Main() {

38

// Instance constructor usage

39

MyClass a = new MyClass();

40

MyClass b = new MyClass(123);

41

// Constant usage

42

Console.WriteLine("MyConst = {0}", MyClass.MyConst);

43

// Field usage

44

a.MyField++;

45

Console.WriteLine("a.MyField = {0}", a.MyField);

46

// Method usage

47

a.MyMethod();

48

// Property usage

49

a.MyProperty++;

50

Console.WriteLine("a.MyProperty = {0}", a.MyProperty);

51

// Indexer usage

52

a[3] = a[1] = a[2];

53

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

54

// Event usage

55

a.MyEvent += new EventHandler(MyHandler);

56

// Overloaded operator usage

57

MyClass c = a + b;

32

 

Chapter 8 Language overview

1

// Nested type usage

2

MyClass.MyNestedClass d = new MyClass.MyNestedClass();

3

}

4

static void MyHandler(object sender, EventArgs e) {

5

Console.WriteLine("Test.MyHandler");

6}

7}

8shows uses of these members.

98.7.1 Constants

10A constant is a class member that represents a constant value: a value that can be computed at compile-time.

11Constants are permitted to depend on other constants within the same program as long as there are no

12circular dependencies. The rules governing constant expressions are defined in §14.15. The example

13class Constants

14{

15

public const int A = 1;

16public const int B = A + 1;

17}

18shows a class named Constants that has two public constants.

19Even though constants are considered static members, a constant declaration neither requires nor allows the

20modifier static. Constants can be accessed through the class, as in

21using System;

22class Test

23{

24

static void Main() {

25

Console.WriteLine("{0}, {1}", Constants.A, Constants.B);

26}

27}

28which prints out the values of Constants.A and Constants.B, respectively.

298.7.2 Fields

30A field is a member that represents a variable associated with an object or class. The example

31class Color

32{

33

internal ushort redPart;

34

internal ushort bluePart;

35

internal ushort greenPart;

36

public Color(ushort red, ushort blue, ushort green) {

37

redPart = red;

38

bluePart = blue;

39

greenPart = green;

40

}

41

public static Color Red = new Color(0xFF, 0, 0);

42

public static Color Blue = new Color(0, 0xFF, 0);

43

public static Color Green = new Color(0, 0, 0xFF);

44public static Color White = new Color(0xFF, 0xFF, 0xFF);

45}

46shows a Color class that has internal instance fields named redPart, bluePart, and greenPart, and

47static fields named Red, Blue, Green, and White

48The use of static fields in this manner is not ideal. The fields are initialized at some point before they are

49used, but after this initialization there is nothing to stop a client from changing them. Such a modification

50could cause unpredictable errors in other programs that use Color and assume that the values do not

51change. Readonly fields can be used to prevent such problems. Assignments to a readonly field can only

52occur as part of the declaration, or in an instance constructor or static constructor in the same class. A static

53readonly field can be assigned in a static constructor, and a non-static readonly field can be assigned in an

33

C# LANGUAGE SPECIFICATION

1instance constructor. Thus, the Color class can be enhanced by adding the modifier readonly to the static

2fields:

3class Color

4{

5

internal ushort redPart;

6

internal ushort bluePart;

7

internal ushort greenPart;

8

public Color(ushort red, ushort blue, ushort green) {

9

redPart = red;

10

bluePart = blue;

11

greenPart = green;

12

}

13

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

14

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

15

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

16public static readonly Color White = new Color(0xFF, 0xFF, 0xFF);

17}

188.7.3 Methods

19A method is a member that implements a computation or action that can be performed by an object or class.

20Methods have a (possibly empty) list of formal parameters, a return value (unless the method’s return-type is

21void), and are either static or non-static. Static methods are accessed through the class. Non-static methods,

22which are also called instance methods, are accessed through instances of the class. A generic method

23(§26.6) has a list of one or more type parameters. The example

24using System;

25public class Stack

26{

27

public static Stack Clone(Stack s) {…}

28

public static Stack Flip(Stack s) {…}

29

public object Pop() {…}

30

public void Push(object o) {…}

31

public void PushMultiple<T>(T[] a) {…}

32

public override string ToString() {…}

33

34}

35class Test

36{

37

static void Main() {

38

Stack s = new Stack();

39

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

40

s.Push(i);

41

Stack flipped = Stack.Flip(s);

42

Stack cloned = Stack.Clone(s);

43

Console.WriteLine("Original stack: " + s.ToString());

44

Console.WriteLine("Flipped stack: " + flipped.ToString());

45

Console.WriteLine("Cloned stack: " + cloned.ToString());

46}

47}

48shows a Stack that has several static methods (Clone and Flip) and several instance methods (Pop, Push,

49and ToString) and a generic method (PushMultiple<T>).

50Methods can be overloaded, which means that multiple methods can have the same name so long as they

51have unique signatures. The signature of a method consists of the name of the method and the number,

52modifiers, and types of its formal parameters, and the number of generic type parameters. The signature of a

53method does not include the return type or the names of the formal parameters or type parameters. The

54example

34

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