0

I have two methods that are identical. one is

    public void ExtendFrameIntoClientArea(Window w, int amount)
    {
        if (internals.DwmIsCompositionEnabled())
        {
            WindowInteropHelper wi = new WindowInteropHelper(w);
            internals.DwmExtendFrameIntoClientArea(wi.Handle, new internals.MARGINS(amount));
        }
    }

and the other is

public void ExtendFrameIntoClientArea(this Window w,int amount)
        {
            this.ExtendFrameIntoClientArea(w, amount);
        }

One of them is an extension method and the other one is not. This however, results in an error "This call is ambiguous"

How would I work around this?

1
  • Does it compile? Extensions methods must be static... Commented Oct 22, 2010 at 23:32

3 Answers 3

3

Extension methods should be static.

public static class XExtender
{
    public static void A(this X x)
    {
        x.A(x);
    }
}
public class X
{
    public void A(X x)
    {

    }
}

Extension methods should have a static class and a static method.

Sign up to request clarification or add additional context in comments.

2 Comments

I believe this is just a typo - won't compile without the static modifier.
I just wonder: An extension method will never be called if it has the same signature as a method defined in the type: bit.ly/91A9Jn (General Guidelines), so whats the point?
1

According to C# Version 3.0 Specification, the search order is:

  • instance method in the type definition
  • extension method in the current namespace
  • extension method in the current namespace’s parents namespaces
  • extension method in the other namespaces imported by “using”

So how you declared your methods and where?

Comments

0

I think the error is not caused by the extension method.

First, an extension method

public static void ExtendFrameIntoClientArea(this Window w, int amount) { }

(by the way, you missed the static modifier) would be ambiguous with a instance method

public void ExtendFrameIntoClientArea(int amount) { }

declared in the class Window but not with a instance method

public void ExtendFrameIntoClientArea(Window w, int amount) { }

no matter in what class it is declared. Further - as far as I remember - instance methods take precedence over extension method - so they should never be ambiguous with extension methods. I suggest to have a look at the error message again and verify that you are looking at the right methods.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.