Replies: 11 comments 9 replies
-
I think it is already possible in C#7 to use tuple destructuring syntax applied on array by using user-defined extension method |
Beta Was this translation helpful? Give feedback.
-
public static class ArrayDestructuringExtensions {
public static void Deconstruct<T>(this T[] array, out T item1, out T item2, out T item3) {
item1 = default(T);
item2 = default(T);
item3 = default(T);
if (array != null) {
if (array.Length > 2) {
item3 = array[2];
}
if (array.Length > 1) {
item3 = array[1];
}
if (array.Length > 0) {
item3 = array[0];
}
}
}
} What's kludgy in C# 7.0 is that deconstruction is guaranteed to succeed, so if you try to deconstruct an array that is too small your options are to throw or to succeed with some default value (I chose the latter above). With conditional patterns you should have more options. Oh, and you'd need an overload per arity. Here's hoping for array patterns. |
Beta Was this translation helpful? Give feedback.
-
Why did you close this? I don't think there's a good solution yet and I've already been in situations where I could use one. |
Beta Was this translation helpful? Give feedback.
-
#388 (comment) is the solution |
Beta Was this translation helpful? Give feedback.
-
@HaloFour included the deficiencies of the approach in the same comment. |
Beta Was this translation helpful? Give feedback.
-
How did this happen? This needs fixing. Deconstruction is almost an absurd feature without conditionality. |
Beta Was this translation helpful? Give feedback.
-
That would be #45. Conditional deconstruction would be a part of the wider feature of pattern matching which was deferred for more bake time. |
Beta Was this translation helpful? Give feedback.
-
Would love to see this moved forward a bit... seems silly it died on the vine in 2017... |
Beta Was this translation helpful? Give feedback.
-
As a (mostly but newly) JavaScript developer, I would like to use this when using C#. Thanks 😊. |
Beta Was this translation helpful? Give feedback.
-
This could be done as an extension to #3435. Specifically, it would be filling in this chart: #3107. |
Beta Was this translation helpful? Give feedback.
-
We need something like this: var (host, port) = "github.com:443".Split(':'); |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I would like to be able to destruct an array into multiple objects similar to js's feature
The syntax could be similar to tuple destructuring:
Beta Was this translation helpful? Give feedback.
All reactions