.NET's Func<T1, T2, TResult>
is written so that T1
and T2
are contravariant and TResult
is covariant.
This means that:
Func<object, object, bool> objectEquals = object.Equals;
Func<MyObject, MyObject, bool> myObjectEquals = objectEquals;
is a valid assignment.
Now, I have a class with methods along these lines:
public void DoSomething<T>(T value)
{
DoSomethingCore(T, Object.Equals);
}
protected abstract void DoSomethingCore<T>(T value, Func<T, T, bool> equals);
The code would not compile, with the following error:
CS0123 No overload for 'object.Equals(object, object)' matches delegate 'Func<T, T, bool>'
Why does contravariance not seem to work in this case?