0

Extension methods are of course useful for adding a method to a class that you do not own.

But I want to practice this concept in Visual Studio but not sure of the notation required.

For example, I have the following class

public static class Dog
{
    public static void Bark()
    {
        Console.WriteLine("Woof!");
    }
}

Let's assume I don't own this method (which I do but let's pretend I don't). How do I go about extending the class with a new method (void in nature) called Jump, where all the new method will do is print to the console that the Dog jumped?

I have attempted to add this using:

public static class SomeOtherClass
{
    //extension method to the Dog class
    public static Dog Jump(this Dog)
    {
        Console.WriteLine("Dog Jumped");
    }
}

However, I am getting errors:

"Dog: Static types cannot be used as parameters"

and

"Dog: Static types cannot be used as return types"

Can you please help me how to solve this problem?

Stephan Bauer
  • 9,120
  • 5
  • 36
  • 58
Baba.S
  • 324
  • 2
  • 14

2 Answers2

5

There are some issues:

  1. If you want a method that returns nothing, don't write a method that returns a Dog:
public static Dog Jump(this Dog)
--------------^^^
public static void Jump(this Dog)
  1. Your parameter of type Dog has no name:
public static void Jump(this Dog)
------------------------------^^^  
public static void Jump(this Dog dog)
  1. Most important:
    Extension methods are just some kind of "syntactic sugar" so that you can write myDog.Jump(); instead of SomeOtherClass.Jump(myDog);.
    That means that you need an instance of a class that is passed to the extension method. You can't call an extension method on class (e.g. Dog.Jump();) but only on an object (e.g. myDog.Jump();). That's just how extension methods work.
    Furthermore your class Dog is static, which means that you cannot create an instance of it so you won't be able to call Dog myDog = new Dog(); and thus won't be able to call extension methods on it.
Stephan Bauer
  • 9,120
  • 5
  • 36
  • 58
2

You need to make your Dog class non-static and add a parameter to Jump and return it:

public class Dog { ... }

public static class SomeOtherClass
{
    //extension method to the Dog class
    public static Dog Jump(this Dog dog)
    {
        Console.WriteLine("Dog Jumped");
        return dog;
    }
}
Lee
  • 142,018
  • 20
  • 234
  • 287
  • I have tried this. But still get the same errors and a squiggly line underneath 'Jump' – Baba.S Jan 12 '17 at 10:23
  • This is because your `Dog` class is `static`. Remove the `static` keyword from this class. – Bidou Jan 12 '17 at 10:24
  • "Let's assume I don't own this method (which I do but let's pretend I don't).". Your solution doesn't solve a problem with this criteria. – Pawel Maga Jan 12 '17 at 10:31
  • @PawelMaga - What method are you referring to? – Lee Jan 12 '17 at 10:39
  • It's a quote from question. If author don't own method he don't own class as well. He wants add extension method to static class what is impossible. Anyway, this question has been asked many times on SO and adds nothing.. – Pawel Maga Jan 12 '17 at 10:43
  • @Bidou Thanks. It works now. But I do not understand why I must include a parameter in the method Jump. I mean, all I am trying to do is add a void method, but it seems that its not possible. – Baba.S Jan 12 '17 at 10:45
  • @Baba.S - You have to name all parameters, you can't just supply the type. – Lee Jan 12 '17 at 10:47
  • @Pawel Maga I am sorry this doesn't add anything for you. But for me I have learnt a lot – Baba.S Jan 12 '17 at 10:50