August 25, 2004 4:27 pm

Constraints and interfaces

Spent most of the day working on generic methods in interfaces and their constraints. If you have a generic method in an interface and you explicitly implement this interface method, you may not specify any constraints on that implementation - they're automatically inherited from the declaration in the interface:

public interface IFoo<t> { void Hello<u> (T t, U u) where U : T; } public class Foo<t> : IFoo<t> { void IFoo<t>.Hello<s> (T t, S s) { } }

The same applies when overriding an abstract or virtual method.

However, if you implement the interface method implicitly, you must repeat the constraints - and they may not conflict with the ones from the declaration in the interface:

public interface IFoo<t> { void Hello<u> (T t, U u) where U : T; } public class Foo<t> : IFoo<t> { public void Hello<s> (T t, S s) where S : T { } }
Posted by martin at August 25, 2004 4:27 pm.