13.07.2015 Views

C# Language Specification - Willy .Net

C# Language Specification - Willy .Net

C# Language Specification - Willy .Net

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 18 StructsPoint a = new Point(10, 10);Point b = a;a.x = 100;System.Console.WriteLine(b.x);outputs the value 10. The assignment of a to b creates a copy of the value, and b is thus unaffected by theassignment to a.x. Had Point instead been declared as a class, the output would be 100 because a and bwould reference the same object. end example]18.3.2 InheritanceAll struct types implicitly inherit from System.ValueType, which, in turn, inherits from class object. Astruct declaration may specify a list of implemented interfaces, but it is not possible for a struct declarationto specify a base class.Struct types are never abstract and are always implicitly sealed. The abstract and sealed modifiers aretherefore not permitted in a struct declaration.Since inheritance isn’t supported for structs, the declared accessibility of a struct member cannot beprotected or protected internal.Function members in a struct cannot be abstract or virtual, and the override modifier is allowedonly to override methods inherited from the type System.ValueType.18.3.3 AssignmentAssignment to a variable of a struct type creates a copy of the value being assigned. This differs fromassignment to a variable of a class type, which copies the reference but not the object identified by thereference.Similar to an assignment, when a struct is passed as a value parameter or returned as the result of a functionmember, a copy of the struct is created. A struct may be passed by reference to a function member using aref or out parameter.When a property or indexer of a struct is the target of an assignment, the instance expression associated withthe property or indexer access must be classified as a variable. If the instance expression is classified as avalue, a compile-time error occurs. This is described in further detail in §14.13.1.18.3.4 Default valuesAs described in §12.2, several kinds of variables are automatically initialized to their default value whenthey are created. For variables of class types and other reference types, this default value is null. However,since structs are value types that cannot be null, the default value of a struct is the value produced bysetting all value type fields to their default value and all reference type fields to null.[Example: Referring to the Point struct declared above, the examplePoint[] a = new Point[100];initializes each Point in the array to the value produced by setting the x and y fields to zero. end example]The default value of a struct corresponds to the value returned by the default constructor of the struct(§11.1.1). Unlike a class, a struct is not permitted to declare a parameterless instance constructor. Instead,every struct implicitly has a parameterless instance constructor, which always returns the value that resultsfrom setting all value type fields to their default value and all reference type fields to null.[Note: Structs should be designed to consider the default initialization state a valid state. In the exampleusing System;struct KeyValuePair{string key;string value;269

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

Saved successfully!

Ooh no, something went wrong!