Home > Articles > Programming > Windows Programming

  • Print
  • + Share This
This chapter is from the book

Extending Other People's Classes

The last topic I want to cover in this chapter is the capability to extend classes written by other developers or companies without actually having the source code to those classes. Keep in mind that in both iOS and C#, extensions to third-party classes can access only class members and methods to which your code would normally have access. In other words, you cannot use class extensions to circumvent encapsulation methods surrounding private or protected members.

C# gives us a facility called static extensions, which is roughly analogous to the concept of categories in the Objective-C world. When a static extension to a specific class is in scope of the current code block, that code block can invoke methods on the extension class as if those methods actually belonged to the original class.

Let's assume that we didn't write the Combatant class and that it's sealed and we cannot inherit from it. We want to be able to add a method to the Combatant class that makes the object move in a square, as if it was square dancing. Perhaps this effect is a custom spell that can be cast on our combatants that gives them an irresistible urge to get their hoedown on.

We can accomplish this custom extension to the Combatant class using the code in Listing 3.19.

Listing 3.19. CombatantSquareDancingExtension.cs

using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Chapter3
    public static class CombatantSquareDancingExtension
        public static void SquareDance(this Combatant dancer)
            Point origin = dancer.Location;
            Point step1 =
              new Point(origin.X, origin.Y - 1); // move forward
            Point step2 =
              new Point(step1.X - 1, step1.Y); // move left
            Point step3 =
              new Point(step2.X, step2.Y + 1); // move back
            Point step4 =
              new Point(step3.X + 1, step3.Y); // move right

Although there are no hard and fast rules about naming conventions used for building extension classes, I like to follow a simple naming scheme: [OriginalClass][ExtensionDescription]Extension. The presence of the suffix Extension immediately tells me and any other developer looking at this class that it is not a complete class on its own; rather, it provides additional functionality to some other class. The class being extended is the first section of the name of the extension class. So in our example, a class that extends the Combatant class by providing square dancing capabilities would be called CombatantSquareDancingExtension.

If the code in Listing 3.19 is in scope, it should be perfectly legal (and possibly amusing) to invoke the SquareDance method of any object that is (directly or through inheritance) of type Combatant.

  • + Share This
  • 🔖 Save To Your Account