I have the following interfaces and an implementation of them:
public interface IFoo<in T>
{
void Notify(T obj);
}
public interface IBar
{
void DoSomething(IFoo<IBar> fooBar);
}
public class Bar
: IBar
{
public void DoSomething(IFoo<IBar> fooBar)
{
fooBar.Notify(this);
}
}
public class Foo
: IFoo<Bar>
{
public void Notify(Bar obj)
{
Console.WriteLine("Hello world!");
}
}
When I use this classes in my main program:
internal class Program
{
private static void Main(string[] args)
{
var foo = new Foo();
var bar = new Bar();
bar.DoSomething(foo);
}
}
I get a compile error:
cannot convert from 'Foo' to 'IFoo< IBar >'
However, as Foo implements IFoo< Bar > and Bar implements IBar I don't understand why this cannot be resolved.