15.02.2015 Views

C# 4 and .NET 4

Create successful ePaper yourself

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

82 ❘ ChaPTer 3 Objects And types<br />

structs <strong>and</strong> inheritance<br />

Structs are not designed for inheritance. This means that it is not possible to inherit from a struct. The<br />

only exception to this is that structs, in common with every other type in <strong>C#</strong>, derive ultimately from the<br />

class System.Object. Hence, structs also have access to the methods of System.Object, <strong>and</strong> it is even<br />

possible to override them in structs — an obvious example would be overriding the ToString() method.<br />

The actual inheritance chain for structs is that each struct derives from a class, System.ValueType, which<br />

in turn derives from System.Object. ValueType does not add any new members to Object, but provides<br />

implementations of some of them that are more suitable for structs. Note that you cannot supply a different<br />

base class for a struct: every struct is derived from ValueType.<br />

Constructors for structs<br />

You can define constructors for structs in exactly the same way that you can for classes, except that you are<br />

not permitted to define a constructor that takes no parameters. This may seem nonsensical, <strong>and</strong> the reason<br />

is buried in the implementation of the .<strong>NET</strong> runtime. Some rare circumstances exist in which the .<strong>NET</strong><br />

runtime would not be able to call a custom zero-parameter constructor that you have supplied. Microsoft<br />

has therefore taken the easy way out <strong>and</strong> banned zero-parameter constructors for structs in <strong>C#</strong>.<br />

That said, the default constructor, which initializes all fields to zero values, is always present implicitly,<br />

even if you supply other constructors that take parameters. It’s also impossible to circumvent the default<br />

constructor by supplying initial values for fields. The following code will cause a compile-time error:<br />

struct Dimensions<br />

{<br />

public double Length = 1;<br />

public double Width = 2;<br />

}<br />

// error. Initial values not allowed<br />

// error. Initial values not allowed<br />

Of course, if Dimensions had been declared as a class, this code would have compiled without any<br />

problems.<br />

Incidentally, you can supply a Close() or Dispose() method for a struct in the same way you do for<br />

a class.<br />

ParTial Classes<br />

The partial keyword allows the class, struct, method or interface to span across multiple files. Typically, a<br />

class will reside entirely in a single file. However, in situations where multiple developers need access to the<br />

same class, or more likely in the situation where a code generator of some type is generating part of a class,<br />

then having the class in multiple files can be beneficial.<br />

The way that the partial keyword is used is to simply place partial before class, struct, or<br />

interface. In the following example, the class TheBigClass resides in two separate source files,<br />

BigClassPart1.cs <strong>and</strong> BigClassPart2.cs:<br />

//BigClassPart1.cs<br />

partial class TheBigClass<br />

{<br />

public void MethodOne()<br />

{<br />

}<br />

}<br />

//BigClassPart2.cs<br />

partial class TheBigClass<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!