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.

80 ❘ ChaPTer 3 Objects And types<br />

sTruCTs<br />

So far, you have seen how classes offer a great way of encapsulating objects in your program. You have also<br />

seen how they are stored on the heap in a way that gives you much more flexibility in data lifetime, but with<br />

a slight cost in performance. This performance cost is small thanks to the optimizations of managed heaps.<br />

However, in some situations all you really need is a small data structure. In this case, a class provides more<br />

functionality than you need, <strong>and</strong> for performance reasons you will probably prefer to use a struct. Look at<br />

this example:<br />

class Dimensions<br />

{<br />

public double Length;<br />

public double Width;<br />

}<br />

This code defines a class called Dimensions, which simply stores the length <strong>and</strong> width of an item.<br />

Perhaps you’re writing a furniture-arranging program to let people experiment with rearranging<br />

their furniture on the computer, <strong>and</strong> you want to store the dimensions of each item of furniture.<br />

It looks as though you’re breaking the rules of good program design by making the fields public, but<br />

the point is that you don’t really need all the facilities of a class for this. All you have is two numbers,<br />

which you’ll find convenient to treat as a pair rather than individually. There is no need for a lot of<br />

methods, or for you to be able to inherit from the class, <strong>and</strong> you certainly don’t want to have the<br />

.<strong>NET</strong> runtime go to the trouble of bringing in the heap with all the performance implications, just<br />

to store two doubles.<br />

As mentioned earlier in this chapter, the only thing you need to change in the code to define a type as a<br />

struct instead of a class is to replace the keyword class with struct:<br />

struct Dimensions<br />

{<br />

public double Length;<br />

public double Width;<br />

}<br />

Defining functions for structs is also exactly the same as defining them for classes. The following code<br />

demonstrates a constructor <strong>and</strong> a property for a struct:<br />

struct Dimensions<br />

{<br />

public double Length;<br />

public double Width;<br />

}<br />

public Dimensions(double length, double width)<br />

{<br />

Length=length;<br />

Width=width;<br />

}<br />

public double Diagonal<br />

{<br />

get<br />

{<br />

return Math.Sqrt(Length*Length + Width*Width);<br />

}<br />

}<br />

Structs are value types, not reference types. This means they are stored either in the stack or inline (if they<br />

are part of another object that is stored on the heap) <strong>and</strong> have the same lifetime restrictions as the simple<br />

data types.<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!