03.01.2015 Views

C# 5.0 Programmer's Reference

Visual Studio 2013 C# 5.0 Programmer's Reference

Visual Studio 2013 C# 5.0 Programmer's Reference

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

Method Declarations ❘ 125<br />

Now suppose the Employee class is derived from Person. The Employee class adds a new Office<br />

field and provides the following new Address method that includes the Office value.<br />

public new string Address()<br />

{<br />

return FirstName + " " + LastName + ", " + Office + '\n' +<br />

Street + '\n' +<br />

City + " " + State + " " + Zip;<br />

}<br />

Without the new keyword, this would raise the warning.<br />

static<br />

As Chapter 4 explained, a variable declared with the static keyword is shared by all instances of<br />

the class. Similarly, a static method applies to all instances of the class. In that case, the method<br />

applies to the class itself rather than to a particular instance of the class.<br />

For example, suppose the Person class has a LookupPerson method that looks up a person’s name in<br />

a database and returns a Person object representing that person. It wouldn’t make sense to require<br />

you to create a Person object just to invoke its LookupPerson method. Instead you can make this a<br />

static method.<br />

To invoke a static method, use the class name as in Person.LookupPerson("Eddie Russett").<br />

virtual and override<br />

The virtual keyword and override keywords go together.<br />

Suppose the Person and Employee classes have Address methods as described in the previous<br />

sections. The Employee class’s version of Address hides the Person class’s version. Now consider<br />

the following code.<br />

Employee employee = new Employee { FirstName = "Rod", ... };<br />

Console.WriteLine("Employee:\n" + employee.Address());<br />

Console.WriteLine();<br />

Person person = employee;<br />

Console.WriteLine("Person:\n" + person.Address());<br />

The code creates an Employee object and displays the result of its Address method in the Console window.<br />

This includes the object’s FirstName, LastName, Office, Street, City, State, and Zip values.<br />

Next the code sets a Person variable equal to the same Employee. This is allowed because an<br />

Employee is a kind of Person. The code then displays the result of the Person object’s Address<br />

method. Because this is a Person object, its Address method doesn’t include the object’s Office<br />

value. Even though this object is actually an Employee, the Person class’s version of the Address<br />

method doesn’t include the Office value. The following text shows the result.<br />

Employee:<br />

Rod Stephens, B-24<br />

1337 Leet St<br />

Bugsville HI 98765<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!