StringConcat Method
 

Concatenates the members of an IEnumerable implementation.

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

Syntax
[ComVisibleAttribute(false)]
public static string Concat<T>(
	IEnumerable<T> values
)
public:
generic<typename T>
[ComVisibleAttribute(false)]
static String^ Concat(
	IEnumerable<T>^ values
)
[<ComVisibleAttribute(false)>]
static member Concat<'T> : 
        values:IEnumerable<'T> -> string
<ComVisibleAttribute(False)>
Public Shared Function Concat(Of T) (
	values As IEnumerable(Of T)
) As String

Parameters

values
Type:

A collection object that implements the IEnumerable interface.

Return Value

Type:

The concatenated members in values.

Type Parameters

T

The type of the members of values.

Exceptions
Exception Condition
ArgumentNullException

values is null.

Remarks

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 is a convenience method that lets you concatenate each element in an IEnumerable 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 collection is derived by calling that object's ToString method.

Examples

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 object to contain a number of Animal objects. The EnumerableWhere extension method is called to extract the Animal objects whose Order property equals "Rodent". The result is passed to the Concat 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
Imports System.Collections.Generic

Public Class Animal
   Public Kind As String
   Public Order As String

   Public Sub New(kind As String, order As String)
      Me.Kind = kind
      Me.Order = order
   End Sub

   Public Overrides Function ToString() As String
      Return Me.Kind
   End Function
End Class

Module Example
   Public Sub Main()
      Dim animals As New List(Of Animal)
      animals.Add(New Animal("Squirrel", "Rodent"))
      animals.Add(New Animal("Gray Wolf", "Carnivora"))
      animals.Add(New Animal("Capybara", "Rodent")) 
      Dim output As String = String.Concat(animals.Where(Function(animal) _
                                           animal.Order = "Rodent"))
      Console.WriteLine(output)                                           
   End Sub
End Module
' The example displays the following output:
'      SquirrelCapybara
Version Information
Universal Windows Platform
Available since 8
.NET Framework
Available since 4.0
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 4.0
Windows Phone Silverlight
Available since 7.1
Windows Phone
Available since 8.1
Return to top