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
[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
{
return new List
}
}
static void Main()
{
Foo f = new Foo();
return;
}
}
[/Code]
thanks in advance,
George
George GeorgePosted May 3, 2008, 11:47 PM
Thanks Brandon,
Your reply is clear.
regards,
George
Posted May 3, 2008, 7:32 PM
Posted May 3, 2008, 7:24 PM
public class MyList
{
interface IFoo
{
object Abc();
}
class Foo : IFoo
{
public Foo()
{
}
public object Abc() //Must return type of object
{
return new List<int>();
}
}
static void Main()
{
Foo f = new Foo();
List<int> myList = (List<int>)f.Abc(); //Cast it back
return;
}
}