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

C# LANGUAGE SPECIFICATION

1Array types are described in §19.

211.2.6 Delegate types

3A delegate is a data structure that refers to one or more methods, and for instance methods, it also refers to

4their corresponding object instances.

5[Note: The closest equivalent of a delegate in C or C++ is a function pointer, but whereas a function pointer

6can only reference static functions, a delegate can reference both static and instance methods. In the latter

7case, the delegate stores not only a reference to the method’s entry point, but also a reference to the object

8instance on which to invoke the method. end note]

9Delegate types are described in §22.

1011.3 Boxing and unboxing

11The concept of boxing and unboxing is central to C#’s type system. It provides a bridge between value-types

12and reference-types by permitting any value of a value-type to be converted to and from type object.

13Boxing and unboxing enables a unified view of the type system wherein a value of any type can ultimately

14be treated as an object.

1511.3.1 Boxing conversions

16A boxing conversion permits any value-type to be implicitly converted to object or System.ValueType

17or to any interface-type implemented by the value-type. Furthermore, there is an implicit boxing conversion

18from any enumeration type to System.Enum. Boxing a value of a value-type consists of allocating an object

19instance and copying the value-type value into that instance.

20The actual process of boxing a value of a value-type is best explained by imagining the existence of a boxing

21class for that type. [Example: For any value-type T, the boxing class behaves as if it were declared as

22follows:

23sealed class T_Box

24{

25

T value;

26

public T_Box(T t) {

27

value = t;

28}

29}

30Boxing of a value v of type T now consists of executing the expression new T_Box(v), and returning the

31resulting instance as a value of type object. Thus, the statements

32int i = 123;

33object box = i;

34conceptually correspond to

35int i = 123;

36object box = new int_Box(i);

37end example]

38Boxing classes like T_Box and int_Box above don’t actually exist and the dynamic type of a boxed value

39isn’t actually a class type. Instead, a boxed value of type T has the dynamic type T, and a dynamic type

40check using the is operator can simply reference type T. [Example:

41int i = 123;

42object box = i;

43if (box is int) {

44Console.Write("Box contains an int");

45}

46will output the string “Box contains an int” on the console. end example]

112

Chapter 11 Types

1A boxing conversion implies making a copy of the value being boxed. This is different from a conversion of

2a reference-type to type object, in which the value continues to reference the same instance and simply is

3regarded as the less derived type object. [Example: Given the declaration

4struct Point

5{

6

public int x, y;

7

public Point(int x, int y) {

8

this.x = x;

9

this.y = y;

10}

11}

12the following statements

13Point p = new Point(10, 10);

14object box = p;

15p.x = 20;

16Console.Write(((Point)box).x);

17will output the value 10 on the console because the implicit boxing operation that occurs in the assignment

18of p to box causes the value of p to be copied. Had Point been declared a class instead, the value 20

19would be output because p and box would reference the same instance. end example]

2011.3.2 Unboxing conversions

21An unboxing conversion permits an explicit conversion from object or System.ValueType to any value-

22type or from any interface-type to any value-type that implements the interface-type. Furthermore, there is an

23explicit unboxing conversion from System.Enum to any enumeration type. An unboxing operation consists

24of first checking that the object instance is a boxed value of the given value-type, and then copying the value

25out of the instance.

26Referring to the imaginary boxing class described in the previous subclause, an unboxing conversion of an

27object box to a value-type T consists of executing the expression ((T_Box)box).value. [Example: Thus,

28the statements

29object box = 123;

30int i = (int)box;

31conceptually correspond to

32object box = new int_Box(123);

33int i = ((int_Box)box).value;

34end example]

35For an unboxing conversion to a given value-type to succeed at run-time, the value of the source operand

36shall be a reference to an object that was previously created by boxing a value of that value-type. If the

37source operand is null a System.NullReferenceException is thrown. If the source operand is a

38reference to an incompatible object, a System.InvalidCastException is thrown.

113

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