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

Chapter 18 Structs

118. Structs

2Structs are similar to classes in that they represent data structures that can contain data members and

3function members. However, unlike classes, structs are value types and do not require heap allocation. A

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

5reference to the data, the latter known as an object.

6[Note: Structs are particularly useful for small data structures that have value semantics. Complex numbers,

7points in a coordinate system, or key-value pairs in a dictionary are all good examples of structs. Key to

8these data structures is that they have few data members, that they do not require use of inheritance or

9referential identity, and that they can be conveniently implemented using value semantics where assignment

10copies the value instead of the reference. end note]

11As described in §11.1.4, the simple types provided by C#, such as int, double, and bool, are, in fact, all

12struct types. Just as these predefined types are structs, it is also possible to use structs and operator

13overloading to implement new “primitive” types in the C# language. Two examples of such types are given

14at the end of this clause (§18.4).

1518.1 Struct declarations

16A struct-declaration is a type-declaration (§16.6) that declares a new struct:

17struct-declaration:

18

attributesopt struct-modifiersopt partialopt struct identifier type-parameter-listopt

19

struct-interfacesopt type-parameter-constraints-clausesopt struct-body ;opt

20A struct-declaration consists of an optional set of attributes (§24), followed by an optional set of struct-

21modifiers (§18.1.1), followed by an optional partial modifier (§17.1.4), followed by the keyword struct

22and an identifier that names the struct, followed by an optional type-parameter-list (§26.1.1), followed by an

23optional struct-interfaces specification (§18.1.2), followed by an optional type-parameter-constraints-

24clauses (§26.7), followed by a struct-body (§18.1.3), optionally followed by a semicolon.

25A struct declaration shall not supply a type-parameter-constraints-clauses unless it also supplies a type-

26parameter-list.

27A struct declaration that supplies a type-parameter-list is a generic struct declaration (§26.2).

2818.1.1 Struct modifiers

29A struct-declaration can optionally include a sequence of struct modifiers:

30struct-modifiers:

31

struct-modifier

32

struct-modifiers struct-modifier

33

struct-modifier:

34

new

35

public

36

protected

37

internal

38private

39It is a compile-time error for the same modifier to appear multiple times in a struct declaration.

40The modifiers of a struct declaration have the same meaning as those of a class declaration (§17.1.1).

321

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