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.

<strong>C#</strong> LANGUAGE SPECIFICATION}public KeyValuePair(string key, string value) {if (key == null || value == null) throw new ArgumentException();this.key = key;this.value = value;}the user-defined instance constructor protects against null values only where it is explicitly called. In caseswhere a KeyValuePair variable is subject to default value initialization, the key and value fields will benull, and the struct must be prepared to handle this state. end note]18.3.5 Boxing and unboxingA value of a class type can be converted to type object or to an interface type that is implemented by theclass simply by treating the reference as another type at compile-time. Likewise, a value of type object ora value of an interface type can be converted back to a class type without changing the reference (but ofcourse a run-time type check is required in this case).Since structs are not reference types, these operations are implemented differently for struct types. When avalue of a struct type is converted to type object or to an interface type that is implemented by the struct, aboxing operation takes place. Likewise, when a value of type object or a value of an interface type isconverted back to a struct type, an unboxing operation takes place. A key difference from the sameoperations on class types is that boxing and unboxing copies the struct value either into or out of the boxedinstance. [Note: Thus, following a boxing or unboxing operation, changes made to the unboxed struct are notreflected in the boxed struct. end note]For further details on boxing and unboxing, see §11.3.18.3.6 Meaning of thisWithin an instance constructor or instance function member of a class, this is classified as a value. Thus,while this can be used to refer to the instance for which the function member was invoked, it is notpossible to assign to this in a function member of a class.Within an instance constructor of a struct, this corresponds to an out parameter of the struct type, andwithin an instance function member of a struct, this corresponds to a ref parameter of the struct type. Inboth cases, this is classified as a variable, and it is possible to modify the entire struct for which thefunction member was invoked by assigning to this or by passing this as a ref or out parameter.18.3.7 Field initializersAs described in §18.3.4, the default value of a struct consists of the value that results from setting all valuetype fields to their default value and all reference type fields to null. For this reason, a struct does notpermit instance field declarations to include variable initializers. [Example: As such, the following exampleresults in one or more compile-time errors:struct Point{public int x = 1; // Error, initializer not permittedpublic int y = 1; // Error, initializer not permitted}end example]This restriction applies only to instance fields. Static fields of a struct are permitted to include variableinitializers.18.3.8 ConstructorsUnlike a class, a struct is not permitted to declare a parameterless instance constructor. Instead, every structimplicitly has a parameterless instance constructor, which always returns the value that results from settingall value type fields to their default value and all reference type fields to null (§11.1.1). A struct can declareinstance constructors having parameters. [Example: For example270

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

Saved successfully!

Ooh no, something went wrong!