   [.net] Confusing constraints
  I have an interface taking a type parameter T. This interface has a method with yet another type parameter S, with the constraint that this type parameter S should inherit/implement the type T and be a reference type. Not totally strange? I mean, a scenario like this could make sense, right?Well, in the case that you agree with me, here is some code to visualize what I just said:public interface TheInterface<T>{    void SomeMethod<S>(T t, S a)   where S : class, T;}So far so good. Now, I want to make use of this interface with something like this:public class Data{}public class MyClass : TheInterface<Data>{    public void SomeMethod<S>(Data t, S a)   where S : class, Data    {   // Do something...    }}Looks like it should work, but it doesn't. The compiler gives me this:error CS0450: 'Data': cannot specify both a constraint class and the 'class' or 'struct' constraintSo I say "fine, Data is a class already of course it doesn't want me to write 'class' too!". And I remove the "class constraint" from above (edit: from SomeMethod, that is. I will not change the interface, since it has to be defined as in my first statement) (no need to post code for it, your imagination will suffice). But it still doesn't work! The compiler gives me a new error: error CS0425: The constraints for type parameter 'A' of method 'MyClass.SomeMethod<A>(Data, A)' must match the constraints for type parameter 'A' of interface method 'TheInterface<Data>.SomeMethod<A>(Data, A)'. Consider using an explicit interface implementation instead.Once again I try to understand why it hates me. To me, this seems like some kind of contradiction. Then of course, we have this stuff about "explicit interface implementation". Lets try one of those!public class Data{}public class MyClass : TheInterface<Data>{    public void TheInterface<Data>.SomeMethod<A>(Data t, A a)    {   // Do something...    }}Now, this sure looks like an explicit interface implementation to me (after reading about those)! But surprise!error CS0106: The modifier 'public' is not valid for this itemI'm like, WTF, the compiler's driving me nuts! I must have done something terribly wrong or missed some crucial information, because logic appears to have left the building.Anyone wise (and kind) enough to enlighten me? :)[Edited by - JakobM on December 10, 2005 2:23:24 PM]
  This code compiles and runs fine for me, perhaps you didn't remove the "class" constraint from MyClass after you removed it from TheInterface<T>?class Program {    static void Main(string[] args) {    new MyClass().SomeMethod(new Data(), new A());    } }public interface TheInterface<T> {    void SomeMethod<S>(T t, S a)   where S : T;}public class Data {}public class A : Data {}public class MyClass : TheInterface<Data> {    public void SomeMethod<S>(Data t, S a)   where S : Data {   Console.WriteLine(a.GetType());    }};  Oh, I probably wasn't too clear. I dont want to remove the class constraint from the interface. If I do that, the code will compile, yes. But if I dont want to (or cant) change the interface, how would I go about it?   ;  Okay, do it like this then:public interface TheInterface<T> where T : class {    void SomeMethod<S>(T t, S a)   where S : T;};  Hmm.. how stupid of me, that would probably work :) At least in my case. But for curiosity (and possibly future needs), could it be solved without altering the interface? It seems very, very strange to me that an interface like the one I first provided would be so useless that it even wont compile.But hey, thanks a bunch for the last suggestion! ;)   ;  Doh, that didn't take long! I cannot force the parameter T to be a reference type, which means the problem is still causing me agony.To rephrase myself for new readers: I want to implement the interface declared by me at the top of this thread, without modifying the interface itself. How am I supposed to write that implementation??   