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.

92 ❘ ChaPTer 4 inheritAnce<br />

}<br />

{<br />

}<br />

return “This method is an override defined in MyDerivedClass.”;<br />

This syntax for method overriding removes potential runtime bugs that can easily occur in C++, when a<br />

method signature in a derived class unintentionally differs slightly from the base version, resulting in<br />

the method failing to override the base version. In <strong>C#</strong>, this is picked up as a compile-time error because the<br />

compiler would see a function marked as override but no base method for it to override.<br />

Neither member fields nor static functions can be declared as virtual. The concept simply wouldn’t make<br />

sense for any class member other than an instance function member.<br />

hiding methods<br />

If a method with the same signature is declared in both base <strong>and</strong> derived classes, but the methods are<br />

not declared as virtual <strong>and</strong> override, respectively, then the derived class version is said to hide the base<br />

class version.<br />

In most cases, you would want to override methods rather than hide them. By hiding them you risk calling<br />

the wrong method for a given class instance. However, as shown in the following example, <strong>C#</strong> syntax is<br />

designed to ensure that the developer is warned at compile time about this potential problem, thus making<br />

it safer to hide methods if that is your intention. This also has versioning benefits for developers of<br />

class libraries.<br />

Suppose that you have a class called HisBaseClass:<br />

class HisBaseClass<br />

{<br />

// various members<br />

}<br />

At some point in the future, you write a derived class that adds some functionality to HisBaseClass. In<br />

particular, you add a method called MyGroovyMethod(), which is not present in the base class:<br />

class MyDerivedClass: HisBaseClass<br />

{<br />

public int MyGroovyMethod()<br />

{<br />

// some groovy implementation<br />

return 0;<br />

}<br />

}<br />

One year later, you decide to extend the functionality of the base class. By coincidence, you add a method<br />

that is also called MyGroovyMethod() <strong>and</strong> that has the same name <strong>and</strong> signature as yours, but probably<br />

doesn’t do the same thing. When you compile your code using the new version of the base class, you<br />

have a potential clash because your program won’t know which method to call. It’s all perfectly legal<br />

in <strong>C#</strong>, but because your MyGroovyMethod() is not intended to be related in any way to the base class<br />

MyGroovyMethod(), the result is that running this code does not yield the result you want. Fortunately,<br />

<strong>C#</strong> has been designed to cope very well with these types of conflicts.<br />

In these situations, <strong>C#</strong> generates a compilation warning that reminds you to use the new keyword to declare<br />

that you intend to hide a method, like this:<br />

class MyDerivedClass: HisBaseClass<br />

{<br />

public new int MyGroovyMethod()<br />

{<br />

// some groovy implementation<br />

return 0;<br />

}<br />

}<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!