Skip to content

Proposal: Supporting C# 7 deconstruction of certain types. #19397

@jamesqo

Description

@jamesqo

Background

C# 7 added support for deconstruction of user-defined types via a Deconstruct(out ...) method. (see it in action) It would make sense to be able to use this feature with tuple-like types such as KeyValuePair and DictionaryEntry.

Proposal

public struct KeyValuePair<TKey, TValue>
{
    // BLOCKED (do not add now): [EditorBrowsable(EditorBrowsableState.Never)]
    public void Deconstruct(out TKey key, out TValue value);
}

public struct DictionaryEntry
{
    // BLOCKED (do not add now): [EditorBrowsable(EditorBrowsableState.Never)]
    public void Deconstruct(out object key, out object value);
}

Usage

public void MyDictionaryMethod()
{
    var dict = new Dictionary<int, string>();
    dict.Add(1, "2");
    var (one, two) = dict.First();
}

Metadata

Metadata

Assignees

Labels

api-approvedAPI was approved in API review, it can be implementedarea-System.Collectionshelp wanted[up-for-grabs] Good issue for external contributorswishlistIssue we would like to prioritize, but we can't commit we will get to it yet

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions