Sorry about the strange title. I really have no idea how to express it any better…
I get an error on the following snippet. I use the class Dummy everywhere. Doesn’t the compiler understand the constraint I’ve added on DummyImplBase? Is this a compiler bug as it works if I use Dummy directly instead of setting it as a constraint?
Error 1 ‘ConsoleApplication53.DummyImplBase’ does not implement interface member ‘ConsoleApplication53.IRequired.RequiredMethod()’. ‘ConsoleApplication53.RequiredBase.RequiredMethod()’ cannot implement ‘ConsoleApplication53.IRequired.RequiredMethod()’ because it does not have the matching return type of ‘ConsoleApplication53.Dummy’. C:\Documents and Settings\simen\My Documents\Visual Studio 2008\Projects\ConsoleApplication53\ConsoleApplication53\Program.cs 37 27 ConsoleApplication53
public class Dummy
{
}
public interface IRequired<T>
{
T RequiredMethod();
}
public interface IDummyRequired : IRequired<Dummy>
{
void OtherMethod();
}
public class RequiredBase<T> : IRequired<T>
{
public T RequiredMethod()
{
return default(T);
}
}
public abstract class DummyImplBase<T> : RequiredBase<T>, IDummyRequired
where T: Dummy
{
public void OtherMethod()
{
}
}
You could add this to DummyImplBase:
EDIT: Or, if you’re using C# 4.0, you could change definition of IRequired like so:
then remove IDummyRequired and you would still be able to assign derived classes to
IRequired<Dummy>(but not to IDummyRequired).2nd EDIT: Your original code did not compile, because T could be Dummy or a class derived from Dummy. And the implemented Method
has not the same as signature as
which was declared in IDummyDerived.