views:

559

answers:

4

Hello, I am trying for the first time to create an extension method and i am having some trouble... maybe you guys can help out :)

public static class Extentions
    {
        public static int myMethod(this MyClass c)
        {
              return 1;
        }
    }

then when i do "MyClass.myMethod" i get q compiler error saying that the method does not exists...

Why is that?

+6  A: 

Did you include the namespace where your extension is defined? I've been burned by that before.

And a way to get around having to add the namespace where your extension is to define the extension in that namespace. This is not a good practice though

phsr
+5  A: 

Did you import (using clause at the beginning) the namespace in which Extensionsclass is?

using Myself.MyProduct.MyNamespace;

Matthias

Mudu
They are in the same namespace
Sergio
+17  A: 

First, you need a "using" directive to include the namespace that includes your class with extension methods.

Second - re "MyClass.myMethod" - extension methods work on instances, not as static - so you'd need:

MyClass foo = ...
int i = foo.myMethod(); // translates as: int i = Extentions.myMethod(foo);

Finally - if you want to use the extension method inside MyClass (or a subclass), you need an explicit this - i.e.

int i = this.myMethod(); // translates as: int i = Extentions.myMethod(this);

This is one of the few cases where this matters.

Marc Gravell
Just our of curiosity, why would there ever be a need to call an extension method within a class? If you're coding the class, why is this method an extension in the first place? Shouldn't it just be part of the class itself? (I understand you posted it to show the syntax, but I'm just ciurious).
BFree
The class might implement an interface, with the extension method defined for that interface. Or the code might be in a subclass, with the extension method defined (separately) against the base class - perhaps in a different assembly to the base class.
Marc Gravell
Re this last; I use this approach myself to provide C# 3.0/.NET 3.5 extensions to a library that is itself .NET 2.0 - i.e. to allow lambda rather than member-name based usage.
Marc Gravell
Marc: I've edited to change '"using" statement' to '"using" directive'. Hope you don't mind :)
Jon Skeet
I agree with @BFree -- there's no reason you'd ever *want* to use an extension method from within a class. An extension method extends a class without modifying code. If you're modifying your code, just promote the extension method to a normal member method.
Randolpho
@Jon Skeet, you should have removed the quotes and put it in a code section. `using` looks much better
Samuel
@Jon - as long as you didn't mind me editing your LINQ earlier ;-p
Marc Gravell
BFree: "MyClass" belongs to a DLL that I cannot change...Mark: You are absolutely right about the instance. As soon as I instantiated the object I could access my function. This will lead to another question that I will open soon :)
Sergio
@Randolpho; so you'd never want to use GroupBy, Where, or any of the rest of LINQ for some processing within a collection class?
Marc Gravell
@Sergio - look forward to it ;-p
Marc Gravell
A: 

Is there a reason forcing you to use Extension methods?

You should only be using extension methods if you cannot add the method in the original source and cannot extend the class with a sub-class (if it is declared sealed)

The page on Extension methods states:

In general, we recommend that you implement extension methods sparingly and only when you have to. Whenever possible, client code that must extend an existing type should do so by creating a new type derived from the existing type. For more information, see Inheritance (C# Programming Guide).

Here is even more info regarding classes and polymorphism.

If extending the class is really not a possibility then follow the other answers regarding the using directive and instance methods vs static methods.

Ben S
I need extension methods because I am not allowed to change that particular assembly.
Sergio
Is the class also sealed, preventing you from sub-classing it?
Ben S