Export (0) Print
Expand All

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

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

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

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

Type Parameters

T

The type of the members of values.

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.

ExceptionCondition
ArgumentNullException

values is Nothing.

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


.NET Framework

Supported in: 4

.NET Framework Client Profile

Supported in: 4

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

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

Community Additions

ADD
Show:
© 2014 Microsoft