I have a Tree-type class Foo<T> with its interface IFoo<T>.
I want Foo<T> to be able to implement IEquatable<Foo<T>> when T : IEquatable<T> (or more generally if T : I<T> i might want Foo<T> : I<Foo<T>> in addition of other implemented interfaces).
I tried the pseudo-following code :
public interface IFoo<T> : IEnumerable<IFoo<T>>
...
public class Foo<T> : IFoo<T>
...
public interface IFooTwo<T> : IFoo<T>, IEquatable<IFooTwo<T>>
where T : IEquatable<T>
...
public class FooTwo<T> : IFooTwo<T> {
... // I implement Equals
public bool NewMethod(FooTwo<T> Other) { ... }
}
So now I have succesfully implemented Equals (I also overrided the geniric Equals, etc.).
But FooTwo<T> now does not implement IEnumerable<IFooTwo<T>> (instead it implements IEnumerable<IFoo<T>>).
So I have two questions :
- Is there a better way for me to organize my code in order to achieve my goal (if
T : IEquatable<T>I want to be able to implementIEquatable<Foo<T>>forFoo<T>) ? A sort of conditionalwhere. - How can I make
FooTwo<T>implementsIEnumerable <FooTwo<T>>usingFoo<T> IEnumerableimplementation in a quick and easy way ?
edit :
In the special case of IEquatable<Foo<T>>, I have a simple answer for question 1. I can test if T:IEquatable<T> and change my implementation of IEquatable<Foo<T>> if needed. However I am still wondering how to do it in a more general case.