Skip to content

Add explanation for anonymous function about contravariant #31678

@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.


Associated WorkItem - 477085

Metadata

Metadata

Labels

📌 seQUESTeredIdentifies that an issue has been imported into Quest.advanced-concepts/subsvcdotnet-csharp/svcin-prThis issue will be closed (fixed) by an active pull request.okr-qualityContent-quality KR: Concerns article defects (bugs), freshness, or build warnings.

Type

No type

Projects

Status

✅ Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions