Defining shared objects with a class and interface

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 { // 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 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.

+4
source share
4 answers

How about this:

 abstract class Base { } abstract class Intermediate : Base, IComparable<Base> { } class Class1 : Intermediate { } class Class2 : Intermediate { } class Class3 : Base { } public static List<Intermediate> BigList = new List<Intermediate>(); BigList.Add(new Class1()); // ok BigList.Add(new Class2()); // ok BigList.Add(new Class3()); // error 

This, of course, is only necessary if you need to implement Base classes, but not IComparable<Base> , otherwise you could just declare Base as

 abstract class Base : IComparable<Base> { } 
+7
source

Why don't you create another class inheritance base and implement IComparable?

 public class ComparableBase :Base, IComparable<Base> { .... } class Class2 : ComparableBase { public int CompareTo(Base other) { return GetHashCode().CompareTo(other.GetHashCode()); } } public static List<ComparableBase > BigList = new List<ComparableBase >(); 
0
source

If you set BigList as List<IComparable<Base>> , which should get the behavior of your display in the validation method.

 public static List<IComparable<Base>> BigList = new List<IComparable<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 will now cause a compile error // BigList.Add(c3); Check(new Class1()); } public static void Check(Base bb) { foreach (IComparable<Base> c in BigList) { 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"); } } 
0
source

IMO, an implementation without an intermediate class can be performed as follows:

 abstract class Base{} class Class1 : Base, IComparable<Base>{} class Class2 : Base, IComparable<Base>{} class Class3 : Base{} var BigList = new List<IComparable<Base>>(); BigList.Add(new Class1()); BigList.Add(new Class2()); BigList.Add(new Class3()); // error 

And you can compare:

 public static void Check(Base BB){ foreach (var c in BigList) { 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"); } } 
0
source

Source: https://habr.com/ru/post/1412015/


All Articles