String.Concat(Of T) Method (IEnumerable(Of T))

 
System_CAPS_noteNote

The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

Concatenates the members of an IEnumerable(Of T) implementation.

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

<ComVisibleAttribute(False)>
Public Shared Function Concat(Of T) (
	values As IEnumerable(Of T)
) As String

Parameters

values
Type: System.Collections.Generic.IEnumerable(Of T)

A collection object that implements the IEnumerable(Of T) interface.

Return Value

Type: System.String

The concatenated members in values.

Type Parameters

T

The type of the members of values.

Exception Condition
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(Of T)(IEnumerable(Of T)) is a convenience method that lets you concatenate each element in an IEnumerable(Of 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(Of 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(Of T) object to contain a number of Animal objects. The Enumerable.Where(Of TSource) extension method is called to extract the Animal objects whose Order property equals "Rodent". The result is passed to the Concat(Of T)(IEnumerable(Of T)) method and displayed to the console.

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

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
Show: