15.02.2015 Views

C# 4 and .NET 4

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

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

84 ❘ ChaPTer 3 Objects And types<br />

sTaTiC Classes<br />

Earlier, this chapter discussed static constructors <strong>and</strong> how they allowed the initialization of static member<br />

variables. If a class contains nothing but static methods <strong>and</strong> properties, the class itself can become static. A<br />

static class is functionally the same as creating a class with a private static constructor. An instance of the<br />

class can never be created. By using the static keyword, the compiler can help by checking that instance<br />

members are never accidentally added to the class. If they are, a compile error happens. This can help<br />

guarantee that an instance is never created. The syntax for a static class looks like this:<br />

static class StaticUtilities<br />

{<br />

public static void HelperMethod()<br />

{<br />

}<br />

}<br />

An object of type StaticUtilities is not needed to call the HelperMethod(). The type name is used to<br />

make the call:<br />

StaticUtilities.HelperMethod();<br />

The objeCT Class<br />

As indicated earlier, all .<strong>NET</strong> classes are ultimately derived from System.Object. In fact, if you don’t<br />

specify a base class when you define a class, the compiler will automatically assume that it derives<br />

from Object. Because inheritance has not been used in this chapter, every class you have seen here is<br />

actually derived from System.Object. (As noted earlier, for structs this derivation is indirect — a struct is<br />

always derived from System.ValueType, which in turn derives from System.Object.)<br />

The practical significance of this is that, besides the methods <strong>and</strong> properties <strong>and</strong> so on that you define,<br />

you also have access to a number of public <strong>and</strong> protected member methods that have been defined for the<br />

Object class. These methods are available in all other classes that you define.<br />

system.object methods<br />

For the time being, we simply summarize the purpose of each method in the following list, <strong>and</strong> then, in the<br />

next section, we provide more detail about the ToString() method in particular.<br />

➤<br />

➤<br />

➤<br />

ToString() — A fairly basic, quick-<strong>and</strong>-easy string representation, use it when you just want<br />

a quick idea of the contents of an object, perhaps for debugging purposes. It provides very little<br />

choice of how to format the data. For example, dates can, in principle, be expressed in a huge variety<br />

of different formats, but DateTime.ToString() does not offer you any choice in this regard.<br />

If you need a more sophisticated string representation that, for example, takes account of your<br />

formatting preferences or of the culture (the locale), then you should implement the IFormattable<br />

interface (see Chapter 9).<br />

GetHashCode() — If objects are placed in a data structure known as a map (also known as a hash<br />

table or dictionary), it is used by classes that manipulate these structures to determine where to<br />

place an object in the structure. If you intend your class to be used as a key for a dictionary, you<br />

will need to override GetHashCode(). Some fairly strict requirements exist for how you implement<br />

your overload, <strong>and</strong> you learn about those when you examine dictionaries in Chapter 10,<br />

“Collections.”<br />

Equals() (both versions) <strong>and</strong> ReferenceEquals() — As you’ll gather by the existence of three different<br />

methods aimed at comparing the equality of objects, the .<strong>NET</strong> Framework has quite a sophisticated<br />

scheme for measuring equality. Subtle differences exist between how these three methods,<br />

along with the comparison operator, ==, are intended to be used. Not only that, but restrictions also<br />

exist on how you should override the virtual, one-parameter version of Equals() if you choose to do<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!