Igor Levashov
Igor Levashov

Reputation: 328

Implementation of generic interfaces in c#

I am new to c# and trying to learn generic interfaces. I am getting an error when trying to implement GetComments. Error: 'cannot convert expression type 'WallGetObject' to return type T'. Is it possible to do something like that? Thanks.

interface IInterface
{
    T GetComments<T>(int id);
}


public class A: IInterface
{
    public T GetComments<T>()
    {
        WallGetObject item
        //... getting item
        return item
    }
}

public class B : IInterface
{
     public T GetComments<T>()
      {
        TopicFeedObject item
        //... getting item
        return item
    }
}


public class MainClass()
{

var item1 = new A();
var item2 = new B();

var list = new List<IInterface>();
list.Add(item1);
list.Add(item2);


foreach (var i in list)
{
  i.GetComments(5);
}

Upvotes: 0

Views: 62

Answers (2)

lc.
lc.

Reputation: 116458

On first glance, I believe what you are trying to do is implement an interface method that returns a different type for each concrete class. That is, A.GetComments should return WallGetObject and B.GetComments() should return a TopicFeedObject. This would be accomplished by using a generic interface (not a generic method as you have).

interface IInterface<T>
{
    T GetComments(int id);
}

public class A: IInterface<WallGetObject>
{
    public WallGetObject GetComments()
    {
        WallGetObject item
        //... getting item
        return item
    }
}

However, this will then lead to a problem later on when you try to create a generic list of different types of IInterfaces. That is, creating a List<IInterface> requires a common base class/interface, and is simply not possible with the above.


The real answer is not to use generics here, but instead a base (abstract) class or interface on WallGetObject and TopicFeedObject that exposes the common functionality you need. For example.

interface IComments {}

class WallGetObject : IComments {}
class TopicFeedObject : IComments {}

interface IInterface
{
    IComments GetComments(int id);
}

public class A: IInterface
{
    public IComments GetComments()
    {
        WallGetObject item
        //... getting item
        return item
    }
}

public class B : IInterface
{
     public IComments GetComments()
     {
        TopicFeedObject item
        //... getting item
        return item
    }
}

Upvotes: 3

Niyoko
Niyoko

Reputation: 7672

You should declare and implement your interface like this. Note the generic type definition is moved to interface declaration instead of method declaration.

interface IInterface<T>
{
    T GetComments(int id);
}


public class A: IInterface<WallGetObject>
{
    public WallGetObject GetComments(int id)
    {
        WallGetObject item
        //... getting item
        return item
    }
}

public class B : IInterface<TopicFeedObject>
{
     public TopicFeedObject GetComments(int id)
     {
        TopicFeedObject item
        //... getting item
        return item
     }
}

Upvotes: 2

Related Questions