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?