Let's say i have list of Tanks,Airplanes and many other things that the to common thing they implement is IHaveLocation which is and abstract class and IHaveColor which is an interface.
I want to make a list of them because i need to query the list on the basis of this two interfaces.
How do i do it ?
is there some trick to this ?
I Want to make something like List<IHaveLocation and IHaveColor>
You can wrap this in a generic class with constraints on both types:
public class YourClass<T> where T : IHaveLocation, IHaveColor
{
List<T> items = new List<T>;
public void Add(T item)
{
items.Add(item);
}
// ...
}
Methods within this class can then use both interfaces as needed, as the type is guaranteed to implement them.
As I mentioned in the comments of Reed's answer, you could use generic methods to allow you to add both Tank's and Airplane's. Something like this:
class TwoTypesList<T1, T2>
{
List<Tuple<T1, T2>> m_list = new List<Tuple<T1,T2>>();
public void Add<ConcreteT>(ConcreteT item) where ConcreteT : T1, T2
{
m_list.Add(Tuple.Create<T1, T2>(item, item));
}
}
Then the usage would be:
TwoTypesList<IHaveColor, IHaveLocation> list = new TwoTypesList<IHaveColor, IHaveLocation>();
Airplane a = new Airplane();
Tank t = new Tank();
list.Add(a);
list.Add(t);
The downside, of course, is that you're storing the object twice. If you don't like that, then of course you could change the internal storage of the list to be an object or only one of the interfaces.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With