5
public void Foo<T>(Func<T> bar)
 where T: IMyInterface
{
   Func<IMyInterface> func = bar;
}

It has been a while since I'd understood covariance, but shouldn't this compile?

Anything bar can return is also an IMyInterface. What seems to be the problem?

TDaver
  • 7,164
  • 5
  • 47
  • 94

1 Answers1

7

Is this a covariance bug in C# 4?

the correct code is:

public void Foo<T>(Func<T> bar)
 where T: class, IMyInterface
{
   Func<IMyInterface> func = bar;
}
Community
  • 1
  • 1
TDaver
  • 7,164
  • 5
  • 47
  • 94