Thread: interface implementation

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    1,579

    interface implementation

    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'.
    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;
        }
    }

    thanks in advance,
    George

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    compiler thinks that List<int> and object are different types... You know, I think I agree with it.
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks vart,


    Question answered.

    Quote Originally Posted by vart View Post
    compiler thinks that List<int> and object are different types... You know, I think I agree with it.

    regards,
    George

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to get RSSI value, send to sensor, sensor receive package, repackage it?
    By techissue2008 in forum Networking/Device Communication
    Replies: 1
    Last Post: 03-04-2009, 10:13 AM
  2. interface, implementation, & application files
    By trongsi in forum C++ Programming
    Replies: 4
    Last Post: 04-08-2006, 02:11 PM
  3. OOP in C
    By lyx in forum C Programming
    Replies: 4
    Last Post: 11-23-2003, 01:12 PM
  4. Replies: 2
    Last Post: 02-11-2002, 01:03 PM
  5. Exporting Object Hierarchies from a DLL
    By andy668 in forum C++ Programming
    Replies: 0
    Last Post: 10-20-2001, 01:26 PM