C# Invalid variance error when having an out method parameter

时间:2018-01-24 15:11:14

标签: c# generics c#-7.2 invariance

I have looked at similar questions, though my case seems to be different.

For this code:

public interface MyInterface<out T> {
  bool TrySomething(out T val);
}

I get this error on C# 7.2:

Invalid variance: The type parameter 'T' must be invariantly valid on 'MyInterface.TrySomething(out T)'. 'T' is covariant.

Is this a bug, or am I missing something? I am not supplying the T value here, I am getting it out (i.e. reading, hence the out method parameter modifier in TrySomething. So, why doesn't it compile?

0 个答案:

没有答案