-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
Comments
You can define an extension 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;
}
} |
The The extension method in #19126 is my favorite use of |
@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. |
@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 👍 |
.Net Core 2.0 is released.. but no news about .Net Framework update... a bit frustrating... Please let us know about any news... |
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:
or this:
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
Is this feasible for future versions of C#? I realize that it would probably mean
IDictionary<TKey, TVal>
would have to implement multipleIEnumerable<T>
s, which is considered a no-no, but I feel it would be very convenient, if it were done carefully and properly. Or perhapsKeyValuePair
could have a Deconstructor, would that also allow this to work, so did PR #19126 make this possible?The text was updated successfully, but these errors were encountered: