Skip to content

Improve Deconstruct resolution to handle two or more methods with the same number of parameters #1092

@DavidArno

Description

@DavidArno

The following code:

namespace Deconstructions
{
    class Program
    {
        static void Main()
        {
            var foo = new Foo();
            (string x, string y) = foo;
        }
    }

    public class Foo
    {
        public void Deconstruct(out string x, out string y) => (x, y) = ("", "");
        public void Deconstruct(out string x, out int y) => (x, y) = ("", 0);
    }
}

results in the compiler complaining that:

Error	CS0121	The call is ambiguous between the following methods or properties: 
'Foo.Deconstruct(out string, out string)' and 'Foo.Deconstruct(out string, out int)'

out parameters were chosen for Deconstruct's to support multiple overloads. It seems odd therefore that the deconstruct behaviour should not be able to resolve which one to use, based on matching the types of those out parameters to the types of the deconstruct expression.

There seems room for improving deconstruct behaviour in future for situations where two Deconstruct methods exist with clearly different parameter types.

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