Skip to content

Add explanation for anonymous function about contravariant #31678

Open
@LeaFrock

Description

@LeaFrock

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions