13.07.2015 Views

C# Language Specification - Willy .Net

C# Language Specification - Willy .Net

C# Language Specification - Willy .Net

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 18 Structs18. StructsStructs are similar to classes in that they represent data structures that can contain data members andfunction members. However, unlike classes, structs are value types and do not require heap allocation. Avariable of a struct type directly contains the data of the struct, whereas a variable of a class type contains areference to the data, the latter known as an object.[Note: Structs are particularly useful for small data structures that have value semantics. Complex numbers,points in a coordinate system, or key-value pairs in a dictionary are all good examples of structs. Key tothese data structures is that they have few data members, that they do not require use of inheritance orreferential identity, and that they can be conveniently implemented using value semantics where assignmentcopies the value instead of the reference. end note]As described in §11.1.3, the simple types provided by <strong>C#</strong>, such as int, double, and bool, are, in fact, allstruct types. Just as these predefined types are structs, it is also possible to use structs and operatoroverloading to implement new “primitive” types in the <strong>C#</strong> language. Two examples of such types are givenat the end of this chapter (§18.4).18.1 Struct declarationsA struct-declaration is a type-declaration (§16.5) that declares a new struct:struct-declaration:attributes opt struct-modifiers opt struct identifier struct-interfaces opt struct-body ; optA struct-declaration consists of an optional set of attributes (§24), followed by an optional set of structmodifiers(§18.1.1), followed by the keyword struct and an identifier that names the struct, followed by anoptional struct-interfaces specification (§18.1.2), followed by a struct-body (§18.1.3), optionally followedby a semicolon.18.1.1 Struct modifiersA struct-declaration may optionally include a sequence of struct modifiers:struct-modifiers:struct-modifierstruct-modifiers struct-modifierstruct-modifier:newpublicprotectedinternalprivateIt is a compile-time error for the same modifier to appear multiple times in a struct declaration.The modifiers of a struct declaration have the same meaning as those of a class declaration (§17.1.1).18.1.2 Struct interfacesA struct declaration may include a struct-interfaces specification, in which case the struct is said toimplement the given interface types.struct-interfaces:: interface-type-list267

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!