0

I want to implement the methods in interface using generics. But I am getting error. e.g. //Filter is a class here.

public interface IComponent<T>
{
  List<T> GetOrderSummary(Filter input);
  T GetOrderDetails(string orderId);
  List<T> GetOrderSummaryDetails(Filter input);
}

// ORDER A AND ORDER B ARE TWO MODEL CLASSES HERE
public class OrderDetails : IComponent<OrderA>,IComponent<OrderB>
{
  public List<OrderA> GetOrderSummary(Filter input)
  {
    //Some logic
    //lst of type OrderA
    return lst;
  }

  public List<OrderB> GetOrderSummaryDetails(Filter input)
  {
    //Some logic
    //lst of type OrderB
    return lst
  }

  public OrderA GetOrderDetails(string orderId)
  {
    throw new NotImplementedException();
  }
}

I am getting error building that

OrderDetails does not implement interface member IComponent.GetOrderSummaryDetails(Filter).OrderDetails.GetOrderSummaryDetails cannot implement IComponent.GetOrderSummaryDetails(Filter) beacuse it does not have the matching return type of List<OrderA>

OrderDetails does not implement interface member IComponent.GetOrderSummary(Filter).OrderDetails.GetOrderSummary cannot implement IComponent.GetOrderSummary(Filter) beacuse it does not have the matching return type of List<OrderB>

OrderDetails does not implement interface member IComponent.GetOrderDetails(string).OrderDetails.GetOrderDetails(string) cannot implement IComponent.GetOrderDetails(string) beacause it does not have the matching return type OrderB

Please let me know how to fix these issues.

2
  • Have you tried using explicit interface implementation? afaik methods are only distinguished by their names and input parameters, not by their return values. Commented Mar 8, 2016 at 19:16
  • 1
    Does the OrderDetails class contain 3 methods only? Please note that you cannot implement these two interfaces implicitly because then you would have two methods that have the same parameters but different return value types which is illegal in c#. You can implement them explicitly though. Commented Mar 8, 2016 at 19:29

1 Answer 1

1

You need explicit implementation of both interfaces, like this:

public class OrderDetails : IComponent<OrderA>, IComponent<OrderB>
{
    List<OrderA> IComponent<OrderA>.GetOrderSummary(Filter input)
    {
        throw new NotImplementedException();
    }

    OrderB IComponent<OrderB>.GetOrderDetails(string orderId)
    {
        throw new NotImplementedException();
    }

    List<OrderB> IComponent<OrderB>.GetOrderSummaryDetails(Filter input)
    {
        throw new NotImplementedException();
    }

    List<OrderB> IComponent<OrderB>.GetOrderSummary(Filter input)
    {
        throw new NotImplementedException();
    }

    OrderA IComponent<OrderA>.GetOrderDetails(string orderId)
    {
        throw new NotImplementedException();
    }

    List<OrderA> IComponent<OrderA>.GetOrderSummaryDetails(Filter input)
    {
        throw new NotImplementedException();
    }
}

Later, to access proper method you can use either casting or proper variable type:

var orderDetails1 = new OrderDetails();
var details1 = ((IComponent<OrderA>)orderDetails1).GetOrderDetails("");

IComponent<OrderA> orderDetails2 = new OrderDetails();
var details2 = orderDetails2.GetOrderDetails("");
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.