Open
Description
It's counterintuitive and seems there isn't any doc about it. I find a answer related to it on SO.
I think it deserves adding extra explanation at this page.
See the example below,
class Program
{
private static void Main()
{
var personReadContact = (Person p) => p.ReadContact();
// OK
Action<Employee> employeeReadContact = personReadContact;
// Compile error: CS1661
// Action<Employee> employeeReadContact = (Person p) => p.ReadContact();
}
}
public class Person
{
public virtual void ReadContact() { /*...*/ }
}
public class Employee : Person
{
public override void ReadContact() { /*...*/ }
}
Document Details
⚠ Do not edit this section. It is required for learn.microsoft.com ➟ GitHub issue linking.
- ID: 61dc3fdf-0154-aa6b-50f4-eb85b5079b05
- Version Independent ID: 0e7cf4e1-1b69-7800-b949-02d60672d396
- Content: Using Variance for Func and Action Generic Delegates (C#)
- Content Source: docs/csharp/programming-guide/concepts/covariance-contravariance/using-variance-for-func-and-action-generic-delegates.md
- Product: dotnet-csharp
- Technology: csharp-advanced-concepts
- GitHub Login: @BillWagner
- Microsoft Alias: wiwagn