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.

implementation inheritance ❘ 91<br />

If you do not specify a base class in a class definition, the <strong>C#</strong> compiler will assume that System.Object is<br />

the base class. Hence, the following two pieces of code yield the same result:<br />

<strong>and</strong><br />

class MyClass: Object // derives from System.Object<br />

{<br />

// etc.<br />

}<br />

class MyClass // derives from System.Object<br />

{<br />

// etc.<br />

}<br />

For the sake of simplicity, the second form is more common.<br />

Because <strong>C#</strong> supports the object keyword, which serves as a pseudonym for the System.Object class, you<br />

can also write:<br />

class MyClass: object // derives from System.Object<br />

{<br />

// etc.<br />

}<br />

If you want to reference the Object class, use the object keyword, which is recognized by intelligent<br />

editors such as Visual Studio .<strong>NET</strong> <strong>and</strong> thus facilitates editing your code.<br />

Virtual methods<br />

By declaring a base class function as virtual, you allow the function to be overridden in any<br />

derived classes:<br />

class MyBaseClass<br />

{<br />

public virtual string VirtualMethod()<br />

{<br />

return "This method is virtual <strong>and</strong> defined in MyBaseClass";<br />

}<br />

}<br />

It is also permitted to declare a property as virtual. For a virtual or overridden property, the syntax is<br />

the same as for a nonvirtual property, with the exception of the keyword virtual, which is added to the<br />

definition. The syntax looks like this:<br />

public virtual string ForeName<br />

{<br />

get { return foreName;}<br />

set { foreName = value;}<br />

}<br />

private string foreName;<br />

For simplicity, the following discussion focuses mainly on methods, but it applies equally well to properties.<br />

The concepts behind virtual functions in <strong>C#</strong> are identical to st<strong>and</strong>ard OOP concepts. You can override a<br />

virtual function in a derived class, <strong>and</strong> when the method is called, the appropriate method for the type of<br />

object is invoked. In <strong>C#</strong>, functions are not virtual by default but (aside from constructors) can be explicitly<br />

declared as virtual. This follows the C++ methodology: For performance reasons, functions are not virtual<br />

unless indicated. In Java, by contrast, all functions are virtual. <strong>C#</strong> does differ from C++ syntax, though,<br />

because it requires you to declare when a derived class’s function overrides another function, using the<br />

override keyword:<br />

class MyDerivedClass: MyBaseClass<br />

{<br />

public override string VirtualMethod()<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!