CSharp - LINQ Concat

Introduction

The Concat operator concatenates two input sequences and yields a single output sequence.

Prototypes

public static IEnumerable<T> Concat<T>(
  this IEnumerable<T> first,
  IEnumerable<T> second);

In this prototype, two sequences of the same type T of elements are input, as first and second.

An object is returned that, when enumerated, enumerates the first input sequence, followed the second input sequence.

Exceptions

ArgumentNullException is thrown if any arguments are null.

The following code uses the Concat operator, as well as the Take and Skip operators.

Demo

using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
class Program//from ww  w  . j  a  va2 s .c  om
{
    static void Main(string[] args)
    {
          string[] codeNames = {"Python", "Java", "Javascript", "Bash", "C++", "Oracle"};
    
          IEnumerable<string> items = codeNames.Take(5).Concat(codeNames.Skip(5));
    
          foreach (string item in items)
              Console.WriteLine(item);
    }
}

Result

This code above takes the first five elements from the input sequence, codeNames.

Then concatenates all but the first five input elements from the codeNames sequence.

The results should be a sequence with the identical contents of the codeNames sequence.

Related Topics