0

Could someone, please, explain why an answer in this question advocates usage of extension methods while defining base interfaces. - Why not including the the SteerLeft() and Stop() methods in their respective interfaces? - Is it to illustrate adding behaviors that should not/could not be anticipated/forced by the "base"? - Isn't it better to "force" something as basic as "steering" behavior when you're requiring a steering wheel?

Below, I've extracted relevant code. The answering person states:

you could use the Extension Methods feature added to C# 3.0 to further simplify calling methods on those implied properties

public interface ISteerable { SteeringWheel wheel { get; set; } }

public interface IBrakable { BrakePedal brake { get; set; } }

public class Vehicle : ISteerable, IBrakable
{
    public SteeringWheel wheel { get; set; }

    public BrakePedal brake { get; set; }

    public Vehicle() { wheel = new SteeringWheel(); brake = new BrakePedal(); }
}

public static class SteeringExtensions
{
    public static void SteerLeft(this ISteerable vehicle)
    {
        vehicle.wheel.SteerLeft();
    }
}

public static class BrakeExtensions
{
    public static void Stop(this IBrakable vehicle)
    {
        vehicle.brake.ApplyUntilStop();
    }
}


public class Main
{
    Vehicle myCar = new Vehicle();
    public void main()
    {
     myCar.SteerLeft();
     myCar.Stop();
    }
}
Community
  • 1
  • 1
G. Stoynev
  • 7,389
  • 6
  • 38
  • 49
  • 1
    In this case it is useful if you have many `ISteerables` and you don't want to copy-paste the same method body into all of them. – default.kramer Apr 13 '13 at 15:45
  • 1
    If you have a question about that particular answer then why not *ask the person who wrote the answer*? – Eric Lippert Apr 13 '13 at 16:06
  • @EricLippert - because the original thread was on a different subject. My interest is educational, in a broader (than multiple inheritance) sense. And the code example, coupled with the comments, hint on a subject different than multiple inheritance. – G. Stoynev Apr 14 '13 at 02:01
  • @default.kramer, I think your comment answers my questions best - as I was re-reading the original (referenced) post and the answers here, I realized that EMs give you the only way to provide implementations in this "hacked" multiple inheritance scenario. So the original statement is very valid in its context, but - phew - does not hold universal truth speaking about EMs. – G. Stoynev Apr 14 '13 at 02:57

2 Answers2

4

The point of using extension method is that you can add method to an existing .Net class even if you do not have the Source code or it reside within different assembly.

And extension method helps to

  1. These methods can be added later (than type authoring time) after type has already been published.
  2. Extension methods can target interfaces.
  3. Different people can extend the same type differently as per their needs.

Take LINQ for example it provides Methods that work on any IEnumerable type!

Parimal Raj
  • 20,189
  • 9
  • 73
  • 110
1

EM are not some substitute of multiple inheritance and is not an inheritance mechanism. It's just a tool, like name suggests, to extend functionality of some type by your means.

In this concrete code there is no much sense of using EM. As you noted, you can easily extend functionality of the class, just by adding a new method inside its body.

EM are extremely useful in cases when you can not change original source of a class or not allowed to do so.

Tigran
  • 61,654
  • 8
  • 86
  • 123