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.

636 ❘ APPENDIX A Solutions to Exercises<br />

}<br />

}<br />

return true;<br />

4. To define a method without providing any implementation, the method’s declaration must<br />

include the abstract keyword. If the class contains an abstract method, its declaration<br />

must also include the abstract keyword. The following code shows the Contactable class.<br />

abstract class Contactable<br />

{<br />

abstract public bool Contact(string message);<br />

}<br />

5. The following code shows a Mailable class that inherits from Contactable and implements<br />

the Contact method.<br />

class Mailable : Contactable<br />

{<br />

public override bool Contact(string message)<br />

{<br />

return true;<br />

}<br />

}<br />

6. The following code shows an implementation of the Root extension method.<br />

static class DoubleExtensions<br />

{<br />

public static double Root(this double number)<br />

{<br />

return (Math.Sqrt(number));<br />

}<br />

}<br />

7. The following code shows an overloaded version of the Root extension method. (In the same<br />

DoubleExtensions class used in Exercise 6.)<br />

public static double Root(this double number, int rootBase)<br />

{<br />

return (Math.Pow(number, 1.0 / rootBase));<br />

}<br />

8. If the program is going to use Piece variables to represent Pieces and Kings, the CanMoveTo<br />

method must be virtual and the King class must override the method. Then if the program<br />

uses a Piece variable to invoke CanMoveTo for a King object, it executes the King’s version of<br />

the method.<br />

The following code shows the Piece class.<br />

class Piece<br />

{<br />

public virtual bool CanMoveTo(int row, int column)<br />

{<br />

return false;<br />

}<br />

}<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!