Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

String.Concat<T> Method (IEnumerable<T>)

Concatenates the members of an IEnumerable<T> implementation.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

[ComVisibleAttribute(false)]
public static string Concat<T>(
	IEnumerable<T> values
)

Type Parameters

T

The type of the members of values.

Parameters

values
Type: System.Collections.Generic.IEnumerable<T>

A collection object that implements the IEnumerable<T> interface.

Return Value

Type: System.String
The concatenated members in values.

ExceptionCondition
ArgumentNullException

values is null.

The method concatenates each object in values; it does not add any delimiters.

An Empty string is used in place of any null argument.

Concat<T>(IEnumerable<T>) is a convenience method that lets you concatenate each element in an IEnumerable<T> collection without first converting the elements to strings. It is particularly useful with Language-Integrated Query (LINQ) query expressions, as the example illustrates. The string representation of each object in the IEnumerable<T> collection is derived by calling that object's ToString method.

The following example defines a very simple Animal class that contains the name of an animal and the order to which it belongs. It then defines a List<T> object to contain a number of Animal objects. The Enumerable.Where extension method is called to extract the Animal objects whose Order property equals "Rodent". The result is passed to the Concat<T>(IEnumerable<T>) method and displayed to the console.

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

public class Animal
{
   public string Kind;
   public string Order;

   public Animal(string kind, string order)
   {
      this.Kind = kind;
      this.Order = order;
   }

   public override string ToString()
   {
      return this.Kind;
   }
}

public class Example
{
   public static void Main()
   {
      List<Animal> animals = new List<Animal>();
      animals.Add(new Animal("Squirrel", "Rodent"));
      animals.Add(new Animal("Gray Wolf", "Carnivora"));
      animals.Add(new Animal("Capybara", "Rodent"));
      string output = String.Concat(animals.Where( animal => 
                      (animal.Order == "Rodent")));
      Console.WriteLine(output);  
   }
}
// The example displays the following output: 
//      SquirrelCapybara

.NET Framework

Supported in: 4.6, 4.5, 4

.NET Framework Client Profile

Supported in: 4

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft