for WPF developers
Home Profile Tips 全記事一覧

Concat 拡張メソッドで 2 つのシーケンスを連結する

(2017/03/07 20:21:50 created.)

(2017/03/13 8:04:39 modified.)

Concat 拡張メソッドを使うと 2 つのシーケンスを結合できます。

Program.cs
  1. namespace Tips_Linq
  2. {
  3.     using System;
  4.     using System.Linq;
  5.  
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             var numbers1 = new int[] { 1, 2, 3 };
  11.             Console.WriteLine("コレクションの要素は {{ {0} }} です。", string.Join(", ", numbers1));
  12.  
  13.             var numbers2 = new int[] { 4, 5, 6 };
  14.             Console.WriteLine("コレクションの要素は {{ {0} }} です。", string.Join(", ", numbers2));
  15.  
  16.             var numbers = numbers1.Concat(numbers2);
  17.             Console.WriteLine("コレクションの要素は {{ {0} }} です。", string.Join(", ", numbers));
  18.  
  19.             Console.ReadKey();
  20.         }
  21.     }
  22. }


当たり前ですが、連結するシーケンスの要素は元のシーケンスの要素の型と同一である必要があります。