I'm having some trouble figuring out how to require a class has extended an interface when working with generic objects. I find this to be difficult to explain, but I want to be able to require a class & an interface when creating my generic object.
I've created the simplest version of this that I can think of - hopefully that will explain my problem better than I can.
abstract class Base
{}
class Class1 : Base, IComparable<Base>
{
public int CompareTo(Base other)
{
return GetHashCode().CompareTo(other.GetHashCode());
}
}
class Class2 : Base, IComparable<Base>
{
public int CompareTo(Base other)
{
return GetHashCode().CompareTo(other.GetHashCode());
}
}
class Class3 : Base
{}
class Program
{
// This list should only accept objects that extend (Base & IComparable<Base>)
public static List<Base> BigList = new List<Base>();
static void Main(string[] args)
{
Class1 c1 = new Class1();
Class2 c2 = new Class2();
Class3 c3 = new Class3();
BigList.Add(c1);
BigList.Add(c2);
// This should not be allowed because Class3 does not extend IComparable<Base>
BigList.Add(c3);
Check(new Class1());
}
public static void Check(Base bb)
{
foreach (Base b in BigList)
{
// Assert that this is true
IComparable<Base> c = (IComparable<Base>)b;
if (c.CompareTo(bb) < 0)
Console.WriteLine("Less Than");
else if (c.CompareTo(bb) == 0)
Console.WriteLine("Equal");
else if (c.CompareTo(bb) > 0)
Console.WriteLine("Greater Than");
}
}
}
As you can see from the inline comments in the code, I'd like the list BigList to require that the objects being added extend Base AND implements IComparable<Base>. This isn't really multiple inheritance though because it's only one class (and an interface).
Does anyone know of a way to do this?
EDIT
Thanks for the comments everyone. Basically the answers come in two flavors: Create an Intermediate class, or use List<IComparable<Base>>. #2 isn't an option because there is logic in Base that we do actually need. #1 is the most viable option, and the one that we are grudgingly going with. The concern with this approach is the amount of mess and complexity this will add in the actual class hierarchy.
Regardless if C# won't let us do what we want to do then we will have to do it with the intermediate classes. Thanks everyone.