I'm having trouble figuring out how to require the class to extend the interface when working with shared objects. I find this hard to explain, but I want to be able to require a class and interface when creating my shared object.
I created the simplest version of this that I can think of - I hope this explains 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 {
As you can see from the inline comments in the code, I would like the BigList
list BigList
require that the added objects expand Base
and implement IComparable<Base>
. This is actually not multiple inheritance, because it is only one class (and an interface).
Does anyone know how to do this?
EDIT
Thanks for the comments to everyone. Basically, the answers come in two ways: create an intermediate class or use List<IComparable<Base>>
. # 2 is not an option, because there is logic in the database that we really need. # 1 is the most viable option, and the one we reluctantly go with. The concern for this approach is the amount of clutter and complexity that will be added to the actual class hierarchy.
Regardless of the fact that C # will not allow us to do what we want to do, we will have to do this with intermediate classes. Thanks to everyone.
source share