-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Add record Deconstruct section #23429
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
Conversation
docs/csharp/deconstruct.md
Outdated
| ## Deconstructing user-defined types | ||
|
|
||
| C# does not offer built-in support for deconstructing non-tuple types. However, as the author of a class, a struct, or an interface, you can allow instances of the type to be deconstructed by implementing one or more `Deconstruct` methods. The method returns void, and each value to be deconstructed is indicated by an [out](language-reference/keywords/out-parameter-modifier.md) parameter in the method signature. For example, the following `Deconstruct` method of a `Person` class returns the first, middle, and last name: | ||
| C# does not offer built-in support for deconstructing non-tuple types other than [`record` types](#deconstructing-a-record-type). However, as the author of a class, a struct, or an interface, you can allow instances of the type to be deconstructed by implementing one or more `Deconstruct` methods. The method returns void, and each value to be deconstructed is indicated by an [out](language-reference/keywords/out-parameter-modifier.md) parameter in the method signature. For example, the following `Deconstruct` method of a `Person` class returns the first, middle, and last name: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There another built-in support and it's the KeyValuePair<TKey, TValue>:
foreach (var (key, value) in new Dictionary<int, string>
{
[1] = "One", [2] = "Two", [3] = "Three"
})
{
Console.WriteLine($"{key} = {value}");
}I knew about the KVP, but did a quick search for other options.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch, thanks!
IEvangelist
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me,
when you're ready.
Fixes #22583