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

C# LANGUAGE SPECIFICATION

118.1.2 Struct interfaces

2A struct declaration can include a struct-interfaces specification, in which case the struct is said to

3implement the given interface types.

4struct-interfaces:

5: interface-type-list

6The handling of interfaces on multiple parts of a partial struct declaration (§17.1.4) are discussed further

7in §17.1.2.2.

8Interface implementations are discussed further in §20.4.

918.1.3 Struct body

10The struct-body of a struct defines the members of the struct.

11struct-body:

12{ struct-member-declarationsopt }

1318.2 Struct members

14The members of a struct consist of the members introduced by its struct-member-declarations and the

15members inherited from the type System.ValueType.

16struct-member-declarations:

17

18

19

20

21

22

23

24

25

26

27

28

29

struct-member-declaration

struct-member-declarations struct-member-declaration

struct-member-declaration: constant-declaration field-declaration method-declaration property-declaration event-declaration indexer-declaration operator-declaration constructor-declaration static-constructor-declaration type-declaration

30All kinds of class-member-declarations except destructor-declaration are also struct-member-declarations.

31Except for the differences noted in §18.3, the descriptions of class members provided in §17.1.4 through

32§17.11 apply to struct members as well.

3318.3 Class and struct differences

3418.3.1 Value semantics

35Structs are value types (§11.1) and are said to have value semantics. Classes, on the other hand, are reference

36types (§11.2) and are said to have reference semantics.

37A variable of a struct type directly contains the data of the struct, whereas a variable of a class type contains

38a reference to the data, the latter known as an object.

39With classes, it is possible for two variables to reference the same object, and thus possible for operations on

40one variable to affect the object referenced by the other variable. With structs, the variables each have their

41own copy of the data, and it is not possible for operations on one to affect the other. Furthermore, because

42structs are not reference types, it is not possible for values of a struct type to be null.

43[Example: Given the declaration

322

Chapter 18 Structs

1struct Point

2{

3

public int x, y;

4

public Point(int x, int y) {

5

this.x = x;

6

this.y = y;

7}

8}

9the code fragment

10Point a = new Point(10, 10);

11Point b = a;

12a.x = 100;

13System.Console.WriteLine(b.x);

14outputs the value 10. The assignment of a to b creates a copy of the value, and b is thus unaffected by the

15assignment to a.x. Had Point instead been declared as a class, the output would be 100 because a and b

16would reference the same object. end example]

1718.3.2 Inheritance

18All struct types implicitly inherit from System.ValueType, which, in turn, inherits from class object. A

19struct declaration can specify a list of implemented interfaces, but it is not possible for a struct declaration to

20specify a base class.

21Struct types are never abstract and are always implicitly sealed. The abstract and sealed modifiers are

22therefore not permitted in a struct declaration.

23Since inheritance isn’t supported for structs, the declared accessibility of a struct member cannot be

24protected or protected internal.

25Function members in a struct cannot be abstract or virtual, and the override modifier is allowed

26only to override methods inherited from the type System.ValueType.

2718.3.3 Assignment

28Assignment to a variable of a struct type creates a copy of the value being assigned. This differs from

29assignment to a variable of a class type, which copies the reference but not the object identified by the

30reference.

31Similar to an assignment, when a struct is passed as a value parameter or returned as the result of a function

32member, a copy of the struct is created. A struct can be passed by reference to a function member using a

33ref or out parameter.

34When a property or indexer of a struct is the target of an assignment, the instance expression associated with

35the property or indexer access shall be classified as a variable. If the instance expression is classified as a

36value, a compile-time error occurs. This is described in further detail in §14.13.1.

3718.3.4 Default values

38As described in §12.2, several kinds of variables are automatically initialized to their default value when

39they are created. For variables of class types and other reference types, this default value is null. However,

40since structs are value types that cannot be null, the default value of a struct is the value produced by

41setting all value type fields to their default value and all reference type fields to null.

42[Example: Referring to the Point struct declared above, the example

43Point[] a = new Point[100];

44initializes each Point in the array to the value produced by setting the x and y fields to zero. end example]

45The default value of a struct corresponds to the value returned by the default constructor of the struct

46(§11.1.1). Unlike a class, a struct is not permitted to declare a parameterless instance constructor. Instead,

323

C# LANGUAGE SPECIFICATION

1every struct implicitly has a parameterless instance constructor, which always returns the value that results

2from setting all value type fields to their default value and all reference type fields to null.

3[Note: Structs should be designed to consider the default initialization state a valid state. In the example

4using System;

5struct KeyValuePair

6{

7

string key;

8

string value;

9

public KeyValuePair(string key, string value) {

10

if (key == null || value == null) throw new ArgumentException();

11

this.key = key;

12

this.value = value;

13}

14}

15the user-defined instance constructor protects against null values only where it is explicitly called. In cases

16where a KeyValuePair variable is subject to default value initialization, the key and value fields will be

17null, and the struct should be prepared to handle this state. end note]

1818.3.5 Boxing and unboxing

19A value of a class type can be converted to type object or to an interface type that is implemented by the

20class simply by treating the reference as another type at compile-time. Likewise, a value of type object or

21a value of an interface type can be converted back to a class type without changing the reference (but, of

22course, a run-time type check is required in this case).

23Since structs are not reference types, these operations are implemented differently for struct types. When a

24value of a struct type is converted to type object or to an interface type that is implemented by the struct, a

25boxing operation takes place. Likewise, when a value of type object or a value of an interface type is

26converted back to a struct type, an unboxing operation takes place. A key difference from the same

27operations on class types is that boxing and unboxing copies the struct value either into or out of the boxed

28instance. [Note: Thus, following a boxing or unboxing operation, changes made to the unboxed struct are not

29reflected in the boxed struct. end note]

30For further details on boxing and unboxing, see §11.3.

3118.3.6 Meaning of this

32Within an instance constructor or instance function member of a class, this is classified as a value. Thus,

33while this can be used to refer to the instance for which the function member was invoked, it is not

34possible to assign to this in a function member of a class.

35Within an instance constructor of a struct, this corresponds to an out parameter of the struct type, and

36within an instance function member of a struct, this corresponds to a ref parameter of the struct type. In

37both cases, this is classified as a variable, and it is possible to modify the entire struct for which the

38function member was invoked by assigning to this or by passing this as a ref or out parameter.

3918.3.7 Field initializers

40As described in §18.3.4, the default value of a struct consists of the value that results from setting all value

41type fields to their default value and all reference type fields to null. For this reason, a struct does not

42permit instance field declarations to include variable initializers. [Example: As such, the following example

43results in one or more compile-time errors:

44struct Point

45{

46

public int x = 1; // Error, initializer not permitted

47public int y = 1; // Error, initializer not permitted

48}

49end example]

324

Chapter 18 Structs

1This restriction applies only to instance fields. Static fields of a struct are permitted to include variable

2initializers.

318.3.8 Constructors

4Unlike a class, a struct is not permitted to declare a parameterless instance constructor. Instead, every struct

5implicitly has a parameterless instance constructor, which always returns the value that results from setting

6all value type fields to their default value and all reference type fields to null (§11.1.1). A struct can declare

7instance constructors having parameters. [Example:

8struct Point

9{

10

int x, y;

11

public Point(int x, int y) {

12

this.x = x;

13

this.y = y;

14}

15}

16Given the above declaration, the statements

17Point p1 = new Point();

18Point p2 = new Point(0, 0);

19both create a Point with x and y initialized to zero. end example]

20A struct instance constructor is not permitted to include a constructor initializer of the form base(…).

21The this variable of a struct instance constructor corresponds to an out parameter of the struct type, and

22similar to an out parameter, this shall be definitely assigned (§12.3) at every location where the

23constructor returns. [Example: Consider the instance constructor implementation below:

24struct Point

25{

26

int x,

y;

 

27

public

int X {

 

28

set { x = value; }

29

}

 

 

30

public int Y {

 

31

set { y = value; }

32

}

 

 

33

public Point(int x, int y) {

34

X = x;

// error, this is not yet definitely assigned

35

Y = y;

// error, this is not yet definitely assigned

36}

37}

38No instance member function (including the set accessors for the properties X and Y) can be called until all

39fields of the struct being constructed have been definitely assigned. Note, however, that if Point were a

40class instead of a struct, the instance constructor implementation would be permitted.

41end example]

4218.3.9 Destructors

43A struct is not permitted to declare a destructor.

4418.3.10 Static constructors

45Static constructors for structs follow most of the same rules as for classes. The execution of a static

46constructor for a struct is triggered by the first of the following events to occur within an application

47domain:

48An instance member of the struct is referenced.

325

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