103

In JavaScript ES6, you are able to destructure arrays like this:

const [a,b,...rest] = someArray;

where a is the first element in the array, b is the second, and rest is an array with the remaining elements.

I know in C#7 that you can destructure tuples during assignment, but could not find anything related to destructuring arrays/enumerables like this:

var (a,b) = someTuple;

I have an IEnumerable where I need the first and second elements as variables, and I need the rest of the elements as another IEnumerable. I have a solution, but feel that destructuring will look cleaner.

5
  • 2
    It's tricky to put the rest as an IEnumerable<T>, because in order to get the first and second elements you need to start iterating. You could wrap the resulting IEnumerator<T> in an IEnumerable<T>, but you'd only be able to iterate over it once. If you don't mind it being copied to a List<T>, that would work, but that's pretty inefficient...
    – Jon Skeet
    Commented Dec 14, 2017 at 14:32
  • 7
    (Doing all of this with arrays would be much simpler.)
    – Jon Skeet
    Commented Dec 14, 2017 at 14:32
  • You could use the Succinc<T> library (disclaimer: I wrote it). It provides deconstruction of IEnumerable<T> (without re-enumeration problems that @JonSkeet mentions). Your code would then just be var (a, (b, rest)) = someArray;
    – David Arno
    Commented Dec 14, 2017 at 14:46
  • @DavidArno I think you should post this as answer, because syntax matches quite close to what OP wants. Even better if it's possible to extract this functionality and post the code of it.
    – Evk
    Commented Dec 14, 2017 at 14:53
  • @Evk, OK, I've added it as an answer.
    – David Arno
    Commented Dec 14, 2017 at 15:23

10 Answers 10

81

It turns out not only tuples can be deconstructed but any type which has Deconstruct static (or extension) method with matching signature. Doing deconstruction correctly for IEnumerable is not trivial (see library suggested by David Arno in this answer), so let's see how it works with simple IList instead (implementation is irrelevant, this one is for example and of course can be better/different):

public static class Extensions {
    public static void Deconstruct<T>(this IList<T> list, out T first, out IList<T> rest) {
        first = list.Count > 0 ? list[0] : default(T); // or throw
        rest = list.Skip(1).ToList();
    }

    public static void Deconstruct<T>(this IList<T> list, out T first, out T second, out IList<T> rest) {
        first = list.Count > 0 ? list[0] : default(T); // or throw
        second = list.Count > 1 ? list[1] : default(T); // or throw
        rest = list.Skip(2).ToList();
    }
}

Then (after adding relevant using statements if necessary) you can use exactly the syntax you want:

var list = new [] {1,2,3,4};
var (a,rest) = list;
var (b,c,rest2) = list;

Or you can chain deconstruction like this (because last returned value can itself be deconstructed):

 var (a, (b, (c, rest))) = list;

With last version, you can deconstruct to any number of items using single Deconstruct method (that one which returns first item and the rest).

For real usage for IEnumerables, I'd suggest to not reimplement the wheel and use David Arno's library mentioned in this answer.

1
  • 4
    For those that, unlike OP, don't care about the rest, you can use a discard: var (a, (b, (c, _))) = list;
    – asontu
    Commented Apr 26, 2021 at 17:09
29

If you want a solution that is fully integrated with the C# language features, use Evk's answer, which hides some of the implementation detail. If you don't care about that, you can use either of the answers.


To my knowledge there is not. However, it is not very hard to make something similar.

What about an extension method like this:

public static class EX
{
    public static void Deconstruct<T>(this T[] items, out T t0)
    {
        t0 = items.Length > 0 ? items[0] : default(T);
    }

    public static void Deconstruct<T>(this T[] items, out T t0, out T t1)
    {
        t0 = items.Length > 0 ? items[0] : default(T);
        t1 = items.Length > 1 ? items[1] : default(T);
    }
}

And you can use it like so:

int[] items = { 1, 2 };

items.Deconstruct(out int t0);

The drawback is that you need an extension method per number of items to return. So if you have more than a few variables to return, this method might not be very useful.

Note that I left out checking the length, and related stuff, but you understand what needs to be done I guess.

7
  • This will probably work for me since I can convert my IEnumerable to an array. The only thing this is missing is getting the rest of the items as a new array, but that is easy to do.
    – kmc059000
    Commented Dec 14, 2017 at 14:42
  • You are welcome. Did you downvote and unaccept by accident maybe? Or wasn't it you? @kmc059000 Commented Dec 14, 2017 at 14:43
  • No downvotes from me. I misclicked accept. I like both responses so am waiting to officially accept.
    – kmc059000
    Commented Dec 14, 2017 at 14:45
  • What do you mean with "adjusted the usage"? @Sebastian Commented Aug 19, 2019 at 7:59
  • 1
    That is a cool language feature, and indeed a good answer. It is not my intention to replicate that. My answer does the same job, but with another syntax and sometimes easier to understand than having to know the exact language rules. If you think that is worth a downvote, so be it. Commented Aug 19, 2019 at 8:04
27

What you are describing is generally known in functional languages as "cons", which often takes the form:

let head :: tail = someCollection

I did propose this be added to C#, but it didn't receive very favourable feedback. So I wrote my own, which you can use via the Succinc<T> nuget package.

It uses deconstruction to achieve that splitting of the head and tail of any IEnumerable<T>. Deconstructs can be nested, so you can use it to extract multiple elements in one go:

var (a, (b, rest)) = someArray;

This potentially could provide the functionality you are after.

17

To extend the solutions hinted by other contributors, I provide an answer that uses IEnumerable. It might not be optimized but it works quite well.

public static class IEnumerableExt
{
    public static void Deconstruct<T>(this IEnumerable<T> seq, out T first, out IEnumerable<T> rest)
    {
        first = seq.FirstOrDefault();
        rest = seq.Skip(1);
    }

    public static void Deconstruct<T>(this IEnumerable<T> seq, out T first, out T second, out IEnumerable<T> rest)
        => (first, (second, rest)) = seq;

    public static void Deconstruct<T>(this IEnumerable<T> seq, out T first, out T second, out T third, out IEnumerable<T> rest)
        => (first, second, (third, rest)) = seq;

    public static void Deconstruct<T>(this IEnumerable<T> seq, out T first, out T second, out T third, out T fourth, out IEnumerable<T> rest)
        => (first, second, third, (fourth, rest)) = seq;

    public static void Deconstruct<T>(this IEnumerable<T> seq, out T first, out T second, out T third, out T fourth, out T fifth, out IEnumerable<T> rest)
        => (first, second, third, fourth, (fifth, rest)) = seq;
}

Then just use these deconstructors like this:

var list = new[] { 1, 2, 3, 4 };
var (a, b, rest1) = list;
var (c, d, e, f, rest2) = rest1;
Console.WriteLine($"{a} {b} {c} {d} {e} {f} {rest2.Any()}");
// Output: 1 2 3 4 0 0 False
0
16

Really quick: No.

C# does not support destructuring for Arrays yet.

Currently, I cannot find any information of this on the roadmap, either. Seems like there will be a lot of waiting involved until we get this syntactic sugar by default.

As @Nekeniehl added in the comments, it can be implemented though: gist.github.com/waf/280152ab42aa92a85b79d6dbc812e68a

3
  • There is a way to kind of implement it: gist.github.com/waf/280152ab42aa92a85b79d6dbc812e68a
    – Nekeniehl
    Commented Dec 14, 2017 at 14:31
  • Since this question is about C# itself, I still think my answer is correct. Nice to see that it can be added, though.
    – NikxDa
    Commented Dec 14, 2017 at 14:32
  • I'm sorry, I did not want to say your answer is not right. It is actually right, there is no implementation so far, I just wanted to point out that it can be implemented =)
    – Nekeniehl
    Commented Dec 14, 2017 at 14:33
16

We have it in C# 11 now as part of list patterns.

Usage examples:

List<int> list = new() { 1, 2, 3 };

// Here array must contain exactly 3 elements to match
// taking first element, disregarding last 2 ("_" disregards a single element)
if (list is [var firstElm, _, _])
{
  // Do stuff with firstElm
}

// Do stuff with firstElm here too

Or

public void SomeFunc(int[] arr)
    {
        // guard
        if (arr is not [var a, var b, var c]) return;

        // act
        a = b - c;
    }

Or

Queue<int[]> queue = new ();
queue.Enqueue(new int[] { 1, 2, 3 });

// Here matching an array with length >= 2
// and taking first 2 elements (".." matches and disregards 0 or more elements)
// Arrays with less then 2 elements won't match
while (queue.TryDequeue(out var arr) && arr is [var a, var b, ..])
{
  // Do stuff with a & b
}

See language reference on topic for full info.

As what such code is lowered to.

Or more pattern examples like

[var a, .., var y, _] for first and (second to last) elements

[_, >= 0, .., >=0, var z] to check & assign simultaneously

[.., [.., var zz]] to match last element of last subarray


PS Atm using stuff mentioned in first 2 examples is plenty for me.

Would be nice to have optional elements, but this already saves a lot of boilerplate.

5

In C# you will need to write your own, like this one I'm using:

public static class ArrayExtensions
    {
        public static void Deconstruct<T>(this T[] array, out T first, out T[] rest)
        {
            first = array.Length > 0 ? array[0] : default(T);
            rest = array.Skip(1).ToArray();
        }

        public static void Deconstruct<T>(this T[] array, out T first, out T second, out T[] rest)
            => (first, (second, rest)) = array;

        public static void Deconstruct<T>(this T[] array, out T first, out T second, out T third, out T[] rest)
            => (first, second, (third, rest)) = array;

        public static void Deconstruct<T>(this T[] array, out T first, out T second, out T third, out T fourth, out T[] rest)
            => (first, second, third, (fourth, rest)) = array;

        public static void Deconstruct<T>(this T[] array, out T first, out T second, out T third, out T fourth, out T fifth, out T[] rest)
            => (first, second, third, fourth, (fifth, rest)) = array;

// .. etc.
    }

Then simply do:

var (first, second,_ , rest) = new[] { 1, 2, 3, 4 }
1
  • 3
    Please note that you are making almost a full copy of the array at each deconstruction step, making the deconstruction effectively O(N^2), so if the array is big, using this method could be slow.
    – marco6
    Commented Feb 5, 2020 at 11:20
1

There is no special syntax for it in the language.

You could leverage the tuple syntax, though, to arrive at this

class Program
{
    static void Main(string[] args)
    {
        int[] ints = new[] { 1, 2, 3 };

        var (first, second, rest) = ints.Destruct2();
    }
}

public static class Extensions
{
    public static (T first, T[] rest) Desctruct1<T>(this T[] items)
    {
        return (items[0], items.Skip(1).ToArray());
    }

    public static (T first, T second, T[] rest) Destruct2<T>(this T[] items)
    {
        return (items[0], items[1], items.Skip(2).ToArray());
    }
}

(which should be extended with error handling for obvious error scenarios before being used in production code).

1

You need to be slightly careful if you want to handle infinite streams, as from a while(true) yield return block for example. In that case, you can't practically check the length of the stream to make sure you have enough items to populate the requested tuple.

If your source is actually infinite a combination of the above approaches would work -- rather than counting the length of the IEnumerable<T>, just check that it has any content at all, and then implement the multi-parameter overloads in terms of the single-parameter overload:

    public static void Deconstruct<T>(this IEnumerable<T> list, out T head, out IEnumerable<T> tail)
    {
        head = list.First(); // throws InvalidOperationException for empty list

        tail = list.Skip(1);
    }

    public static void Deconstruct<T>(this IEnumerable<T> list, out T head, out T next, out IEnumerable<T> tail)
    {
        head = list.First();
        
        (next, tail) = list.Skip(1);
    }

The critical question is what you want to happen when the stream runs out. The code above will throw an InvalidOperationException. Returning default<T> might not be what you want instead. In a functional context you'd typically be doing a cons, and splitting the stream into a single head an stream tail - and then checking for empty streams outside of your cons implementation (so outside of the Deconstruct method).

1

I tried to make it more shorter and performance effective. So I avoided the calling of IEnumerable<T>.ToList() method, which would be expensive if we had a large list.

public static void Deconstruct<T>(this IEnumerable<T> list, out T first, out IEnumerable<T> rest) {
    first = list.FirstOrDefault();
    rest = list.Skip(1);
}

public static void Deconstruct<T>(this IEnumerable<T> list, out T first, out T second, out IEnumerable<T> rest) {
    first = list.FirstOrDefault();
    (second, rest) = list.Skip(1);
}

public static void Deconstruct<T>(this IEnumerable<T> list, out T first, out T second, out T third, out IEnumerable<T> rest) {
    first = list.FirstOrDefault();
    (second, third, rest) = list.Skip(1);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.