New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tuple support for iterating over dictionaries in C# #20393

Closed
alex-slover opened this Issue Jun 22, 2017 · 5 comments

Comments

Projects
None yet
4 participants
@alex-slover

alex-slover commented Jun 22, 2017

The syntax for iterating over dictionaries, if you needed both the key and the value, has always been a little awkward in C#, being a specific case of the awkwardness of the tuple-like hacks prior to the real thing in C# 7. You either had to do this:

IDictionary<string, Foo> dict = GetFoos();

foreach (var entry in dict) { 
   string key = entry.Key;
   Foo value = entry.Value;
   // ...
}

or this:

IDictionary<string, Foo> dict = GetFoos();

foreach (string key in dict.Keys) {
    Foo value = dict[key];
    // ...
}

Now that native tuples are present, it would be a great boon if dictionary iterators could be overloaded to return them if requested, so you could do something like

IDictionary<string, Foo> dict = GetFoos();

foreach ((string key, Foo value) in dict) {
    // ...
}

Is this feasible for future versions of C#? I realize that it would probably mean IDictionary<TKey, TVal> would have to implement multiple IEnumerable<T>s, which is considered a no-no, but I feel it would be very convenient, if it were done carefully and properly. Or perhaps KeyValuePair could have a Deconstructor, would that also allow this to work, so did PR #19126 make this possible?

@alrz

This comment has been minimized.

Show comment
Hide comment
@alrz

alrz Jun 22, 2017

Contributor

You can define an extension Deconstruct method,

static class C {
    static void M(Dictionary<string, Foo> dict) {
        foreach (var (key, value) in dict) {}
    }
    
    static void Deconstruct<T, U>(this KeyValuePair<T, U> k, out T t, out U u) {
        t = k.Key; u = k.Value;
    }
}
Contributor

alrz commented Jun 22, 2017

You can define an extension Deconstruct method,

static class C {
    static void M(Dictionary<string, Foo> dict) {
        foreach (var (key, value) in dict) {}
    }
    
    static void Deconstruct<T, U>(this KeyValuePair<T, U> k, out T t, out U u) {
        t = k.Key; u = k.Value;
    }
}
@sharwell

This comment has been minimized.

Show comment
Hide comment
@sharwell

sharwell Jun 22, 2017

Member

The KeyValuePair<TKey, TValue>.Deconstruct method is available in .NET Core 2.0 as well.

The extension method in #19126 is my favorite use of Deconstruct to date. 😄

Member

sharwell commented Jun 22, 2017

The KeyValuePair<TKey, TValue>.Deconstruct method is available in .NET Core 2.0 as well.

The extension method in #19126 is my favorite use of Deconstruct to date. 😄

@sharwell

This comment has been minimized.

Show comment
Hide comment
@sharwell

sharwell Jun 22, 2017

Member

@alex-slover The .NET Core version of this is dotnet/corefx#13746. The .NET Framework version is internal "Bug 292574", which I'm trying to get a status update on just so you can know.

For now I'm going to close this as External since it's being tracked already for both use cases.

Member

sharwell commented Jun 22, 2017

@alex-slover The .NET Core version of this is dotnet/corefx#13746. The .NET Framework version is internal "Bug 292574", which I'm trying to get a status update on just so you can know.

For now I'm going to close this as External since it's being tracked already for both use cases.

@sharwell

This comment has been minimized.

Show comment
Hide comment
@sharwell

sharwell Jun 22, 2017

Member

@alex-slover I heard back, and there is no specific time frame for if or when .NET Standard or .NET Framework will be getting that method. Best bet for now is the extension method 👍

Member

sharwell commented Jun 22, 2017

@alex-slover I heard back, and there is no specific time frame for if or when .NET Standard or .NET Framework will be getting that method. Best bet for now is the extension method 👍

@hardhub

This comment has been minimized.

Show comment
Hide comment
@hardhub

hardhub Aug 27, 2017

@sharwell

.Net Core 2.0 is released.. but no news about .Net Framework update... a bit frustrating... Please let us know about any news...

hardhub commented Aug 27, 2017

@sharwell

.Net Core 2.0 is released.. but no news about .Net Framework update... a bit frustrating... Please let us know about any news...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment