.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?
Contravariance, and variance in general, don't work with value types.
Hence, you must constrain the
DoSomethingmethod to only work with reference types if you want to useObject.Equalsas the function forFunc<T, T, bool>.In other words, add
where T : classin the method signature: