Export (0) Print
Expand All
Expand Minimize

Compiler Warning (Level 1) CS1956

Member 'name' implements interface member 'name' in type 'type'. There are multiple matches for the interface member at run-time. It is implementation dependent which method will be called.

This warning is generated when two interface methods are differentiated only by whether a particular parameter is ref or out. It is best to change your code to avoid this warning because it is not obvious or guaranteed which of the methods will actually be called at runtime.

Although C# distinguishes between out and ref, the CLR sees them as the same. When deciding which method implements the interface, the CLR just picks one.

To avoid this warning

  • Give the compiler some way to differentiate the methods. For example you can give them different names or provide an additional parameter on one of them.

The following code generates CS1956 because the two Test methods in Base differ only by the ref/out modifier on their first parameter:

// cs1956.cs
class Base<T, S>
{
    // This is the method that should be called.
    public virtual int Test(out T x) // CS1956
    {
        x = default(T);
        return 0;
    }

    // This is the "last" method and is the one that ends up being called
    public virtual int Test(ref S x)
    {
        return 1;
    }
}

interface IFace
{
    int Test(out int x);
}

class Derived : Base<int, int>, IFace
{
    static int Main()
    {
        IFace x = new Derived();
        int y;
        return x.Test(out y);
    }
}

Community Additions

ADD
Show:
© 2014 Microsoft