コガネブログ

平日更新を目標に Unity や C#、Visual Studio、ReSharper などのゲーム開発アレコレを書いていきます

【C#】配列やリストの分解代入を可能にする Deconstruct

ソースコード

using System.Collections.Generic;
using System.Linq;

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

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

    public static void Deconstruct<T>
    (
        this IList<T> self,
        out  T        first,
        out  T        second,
        out  T        third,
        out  IList<T> rest
    )
    {
        first  = self.Count > 0 ? self[ 0 ] : default;
        second = self.Count > 1 ? self[ 1 ] : default;
        third  = self.Count > 2 ? self[ 2 ] : default;
        rest   = self.Skip( 3 ).ToArray();
    }

    public static void Deconstruct<T>
    (
        this IList<T> self,
        out  T        first,
        out  T        second,
        out  T        third,
        out  T        four,
        out  IList<T> rest
    )
    {
        first  = self.Count > 0 ? self[ 0 ] : default;
        second = self.Count > 1 ? self[ 1 ] : default;
        third  = self.Count > 2 ? self[ 2 ] : default;
        four   = self.Count > 3 ? self[ 3 ] : default;
        rest   = self.Skip( 4 ).ToArray();
    }
}

使用例

配列

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

Console.WriteLine( a );
Console.WriteLine( b );
Console.WriteLine( string.Join( ", ", rest ) );

// 1
// 2
// 3, 4, 5

リスト

var ( a, b, c, rest ) = new List<int> { 1, 2, 3, 4, 5 };

Console.WriteLine( a );
Console.WriteLine( b );
Console.WriteLine( c );
Console.WriteLine( string.Join( ", ", rest ) );

// 1
// 2
// 3
// 4, 5

(rest には残りの要素が入ってきます)

参考サイト様