I am working through a bug. In recreating the bug for the following sample I was able to determine why the problem is happening. But I am stuck for a better solution. So given the following program:
public interface IFoo<T> {
T OutputType(T param);
}
class Foo : IFoo<Foo> {
public virtual Foo OutputType(Foo param) {
Console.WriteLine("foo");
return param;
}
}
class Bar : Foo, IFoo<Bar> {
public virtual Bar OutputType(Bar param) {
Console.WriteLine("bar");
return param;
}
}
class Program {
static void Main(string[] args) {
Bar bar = new Bar();
CallOutputType(bar);
bar.OutputType(bar);
}
static void CallOutputType<T>(T t) where T : Foo {
t.OutputType(t);
}
}
I was expecting the output to be:
bar
bar
But what I am getting is:
foo
bar
Seeing the problem simplified like this it is obvious that Bar.OutputType isn't overriding Foo.OutputType. What are my best options for improving this design? Bar.OutputType can't override Foo.OutputType because the signatures are different. Changing the signature of Bar.OutputType to match Foo.OutputType won't work because then Bar won't be implimenting IFoo.