O'Reilly logo

Pro C# 2010 and the .NET 4 Platform, Fifth Edition by Andrew Troelsen

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

9.5. Interfaces As Parameters

Given that interfaces are valid .NET types, you may construct methods that take interfaces as parameters, as illustrated by the CloneMe() method earlier in this chapter. For the current example, assume you have defined another interface named IDraw3D:

// Models the ability to render a type in stunning 3D.
public interface IDraw3D
{
  void Draw3D();
}

Next, assume that two of your three shapes (ThreeDCircle and Hexagon) have been configured to support this new behavior:

// Circle supports IDraw3D.
class ThreeDCircle : Circle, IDraw3D
{
... public void Draw3D() { Console.WriteLine("Drawing Circle in 3D!"); } } // Hexagon supports IPointy and IDraw3D. class Hexagon : Shape, IPointy, IDraw3D { ... public void Draw3D() ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required