3
Answers

interface implementation

Ask a question
George George

George George

16y
2.1k
1

Hello everyone,


I am migrating from C++ to C#. The following compile error makes me confused. Suppose in interface there is a method called Abc which returns object, and in the implementation class, there is also a method called Abc, but the return type is List<int>, I think List<int> is already a type (derived type) of object, so no need to explicitly implement Interface.Abc again, but here is a compile error.

[Code]
D:\Visual Studio 2008\Projects\ConsoleApplication1\ConsoleApplication1\Program.cs(14,11): error CS0738: 'MyList.Foo' does not implement interface member 'MyList.IFoo.Abc()'. 'MyList.Foo.Abc()' cannot implement 'MyList.IFoo.Abc()' because it does not have the matching return type of 'object'.
[/Code]

Could anyone show me what is the rule I break here please?

[Code]
public class MyList
{

    interface IFoo
    {
        object Abc();
    }

    class Foo : IFoo
    {
        public Foo()
        {

        }
       
        public List<int> Abc()
        {
            return new List<int>;
        }

    }

    static void Main()
    {
        Foo f = new Foo();
        return;
    }
}
[/Code]


thanks in advance,
George


Answers (3)