ExtensionMethod.NET Home of 819 C#, Visual Basic, F# and Javascript extension methods

Concat

Adds an element to an IEnumerable (System.Linq.Concat only adds multiple elements)

Source

static IEnumerable<T> Concat<T>(IEnumerable<T> target, T element)
{
  return System.Linq.Concat(target, new[]{ element };
}

or

static IEnumerable<T> Concat<T>(IEnumerable<T> target, T element)
{
  foreach(T e in target) yield return e;
  yield return element;
}

Example

IEnumerable<String> x = new[]{"foo", "bar"};
x = x.Concat("bla");
Console.WriteLine(string.Join('--',x));

// returns foo--bar--bla

Author: B.W. Kemps

Submitted on: 22 aug 2011

Language: C#

Type: System.Collections.IEnumerable<T>

Views: 4139