1

Maybe this is already answered somewhere else, but this is the kind of situation where the question in itself is hard to formulate. Excuse me in advance if it is the case.

I have a container class who contains base properties and a list of items of the generic type specified in the class definition.

public class Container<TItem> where T : IItem
{
   public string PropA {get; set;}

   public List<TItem> Items {get; set;}
}

IITem is only an empty interface used for the purpose of grouping specific classes. It will be clearer in a short while.

public interface IITem
{}

I then have some classes who inherits this interface.

public class ItemA : IItem
{
   public string ItemAProperty { get; set; }
}

public class ItemB : IItem
{
   public int ItemBProperty { get; set; }
}

Here is the problem. I have a method who populate a Container class and I want it to return it. So, I have this :

public Container<IItem> GetItems(int itemCategory)
{
   Container<IItem> items;

   switch(itemCategory)
   {
      case 1 :
         items = GetItemsA(); // Will return an instance of Container<ItemA>.
         break;

      case 2:
         items = GetItemsB(); // Will return an instance of Conainter<ItemB>.
         break;
   }
}

The lines begining with item = GetItems... don't compile, indicating :

Container<ItemA> can't be cast as Container<IITem>.
sbeaudoin
  • 158
  • 2
  • 11
  • Could `GetItems` take a generic T parameter rather than an `int`? And then return `Container`? – mjwills May 26 '20 at 00:24
  • _"Maybe this is already answered somewhere else"_ -- yes, many times. Technically, you are looking for "generic type variance", but there are lots of other search terms you might have used to find duplicate questions, including variations on the error message you're getting. See marked duplicate for one of the explanations about why what you're trying to do isn't safe, and thus is prohibited by the compiler. – Peter Duniho May 26 '20 at 02:09
  • @mjWills, The goal is to have a return type able to support a Container or Container, it has nothing to do with the itemCategory parater. This last one is only to indicate which type of Container with be returned. – sbeaudoin May 27 '20 at 10:18
  • @Peter Dunhio, yes, I searched at least an hour within noise answers returned because I'm not able to define a precise question. I thought it would be a lot easier for someone who already did something similar than me, searching another hour or more and, for him, gain easily a reputation point or more. It seems StackOverflow does not like this efficiency rule. – sbeaudoin May 27 '20 at 10:21
  • @Pter Dunhio, concerning your reference to another question. I am trying to do almost the reverse. As I can't have help and searched another hour without result, I decided for the moment to return an ImmutableList and include the properties found in Container inside the returned ItemA, ItemB, classes, causing duplicated code. – sbeaudoin May 27 '20 at 10:31
  • `This last one is only to indicate which type of Container with be returned.` I suppose I am suggesting generics as an alternate approach... – mjwills May 27 '20 at 11:51

0 Answers0