.NET Framework Class Library
StringConcat Method (IEnumerableString)

Concatenates the members of a constructed IEnumerableT collection of type String.

Namespace:   System
Assembly:  mscorlib (in mscorlib.dll)
Syntax
<[%$TOPIC/dd784338_en-us_VS_110_3_0_0_0_0%](False)> _
Public Shared Function Concat ( _
	values As [%$TOPIC/dd784338_en-us_VS_110_3_0_0_0_1%](Of [%$TOPIC/dd784338_en-us_VS_110_3_0_0_0_2%]) _
) As [%$TOPIC/dd784338_en-us_VS_110_3_0_0_0_3%]
[[%$TOPIC/dd784338_en-us_VS_110_3_0_1_0_0%](false)]
public static [%$TOPIC/dd784338_en-us_VS_110_3_0_1_0_1%] Concat(
	[%$TOPIC/dd784338_en-us_VS_110_3_0_1_0_2%]<[%$TOPIC/dd784338_en-us_VS_110_3_0_1_0_3%]> values
)
[[%$TOPIC/dd784338_en-us_VS_110_3_0_2_0_0%](false)]
public:
static [%$TOPIC/dd784338_en-us_VS_110_3_0_2_0_1%]^ Concat(
	[%$TOPIC/dd784338_en-us_VS_110_3_0_2_0_2%]<[%$TOPIC/dd784338_en-us_VS_110_3_0_2_0_3%]^>^ values
)
[<[%$TOPIC/dd784338_en-us_VS_110_3_0_3_0_0%](false)>]
static member Concat : 
        values:[%$TOPIC/dd784338_en-us_VS_110_3_0_3_0_1%]<[%$TOPIC/dd784338_en-us_VS_110_3_0_3_0_2%]> -> [%$TOPIC/dd784338_en-us_VS_110_3_0_3_0_3%]
public static function Concat(
	values : [%$TOPIC/dd784338_en-us_VS_110_3_0_4_0_0%]<[%$TOPIC/dd784338_en-us_VS_110_3_0_4_0_1%]>
) : [%$TOPIC/dd784338_en-us_VS_110_3_0_4_0_2%]

Parameters

values
Type: System.Collections.GenericIEnumerable String

A collection object that implements IEnumerableT and whose generic type argument is String.

Return Value

Type: SystemString
The concatenated strings in values.
Exceptions
ExceptionCondition
ArgumentNullException

values is .

Remarks

The method concatenates each object in values; it does not add any delimiters. To specify a delimiter between each member of values, call the Join(String, IEnumerableString) method.

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

Concat(IEnumerableString) is a convenience method that lets you concatenate each element in an IEnumerable(Of String) collection without first converting the elements to a string array. It is particularly useful with Language-Integrated Query (LINQ) query expressions. The following example passes a List(Of String) object that contains either the uppercase or lowercase letters of the alphabet to a lambda expression that selects letters that are equal to or greater than a particular letter (which, in the example, is "M"). The IEnumerable(Of String) collection that is returned by the EnumerableWhere method is passed to the Concat(IEnumerableString) method to display the result as a single string.

Imports System.Collections.Generic
Imports System.Linq

Module modMain
   Public Sub Main()
      Dim output As String = String.Concat(GetAlphabet(true).Where(Function(letter) _
                                                         letter >= "M"))

      Console.WriteLine(output)                                     
   End Sub 

   Private Function GetAlphabet(upper As Boolean) As List(Of String)
      Dim alphabet As New List(Of String)
      Dim charValue As Integer = CInt(IIf(upper, 65, 97))
      For ctr As Integer = 0 To 25
         alphabet.Add(ChrW(charValue + ctr).ToString())
      Next 
      Return alphabet 
   End Function 
End Module 
' The example displays the following output: 
'       MNOPQRSTUVWXYZ
using System;
using System.Collections.Generic;
using System.Linq;

public class Example
{
   public static void Main()
   {
      string output = String.Concat( GetAlphabet(true).Where( letter => 
                      letter.CompareTo("M") >= 0));
      Console.WriteLine(output);  
   }

   private static List<string> GetAlphabet(bool upper)
   {
      List<string> alphabet = new List<string>();
      int charValue = upper ? 65 : 97;
      for (int ctr = 0; ctr <= 25; ctr++)
         alphabet.Add(Convert.ToChar(charValue + ctr).ToString());
      return alphabet; 
   }
}
// The example displays the following output: 
//      MNOPQRSTUVWXYZ
Examples

The following example uses the Sieve of Eratosthenes algorithm to calculate the prime numbers that are less than or equal to 100. It assigns the result to a ListT object of type String, which it then passes to the Concat(IEnumerableString) method.

Imports System.Collections.Generic

Module Example
   Public Sub Main()
      Dim maxPrime As Integer = 100
      Dim primeList As IEnumerable(Of String) = GetPrimes(maxPrime)
      Console.WriteLine("Primes less than {0}:", maxPrime)
      Console.WriteLine("   {0}", String.Concat(primeList))
   End Sub 

   Private Function GetPrimes(maxPrime As Integer) As IEnumerable(Of String)
      Dim values As Array = Array.CreateInstance(GetType(Integer), _
                              New Integer() { maxPrime - 1}, New Integer(){ 2 }) 
      ' Use Sieve of Erathsthenes to determine prime numbers. 
      For ctr As Integer = values.GetLowerBound(0) To _
                           CInt(Math.Ceiling(Math.Sqrt(values.GetUpperBound(0))))
         If CInt(values.GetValue(ctr)) = 1 Then Continue For 

         For multiplier As Integer = ctr To maxPrime \ 2
            If ctr * multiplier <= maxPrime Then values.SetValue(1, ctr * multiplier)
         Next    
      Next       

      Dim primes As New List(Of String)
      For ctr As Integer = values.GetLowerBound(0) To values.GetUpperBound(0)
         If CInt(values.GetValue(ctr)) = 0 Then primes.Add(ctr.ToString() + " ")
      Next             
      Return primes
   End Function    
End Module 
' The example displays the following output: 
'    Primes less than 100: 
'       2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
using System;
using System.Collections.Generic;

public class Example
{
   public static void Main()
   {
      int maxPrime = 100;
      IEnumerable<String> primeList = GetPrimes(maxPrime);
      Console.WriteLine("Primes less than {0}:", maxPrime);
      Console.WriteLine("   {0}", String.Concat(primeList));
   }

   private static IEnumerable<String> GetPrimes(int maxPrime)
   {
      Array values = Array.CreateInstance(typeof(int), 
                              new int[] { maxPrime - 1}, new int[] { 2 }); 
      // Use Sieve of Erathsthenes to determine prime numbers. 
      for (int ctr = values.GetLowerBound(0); ctr <= (int) Math.Ceiling(Math.Sqrt(values.GetUpperBound(0))); ctr++)
      {

         if ((int) values.GetValue(ctr) == 1) continue;

         for (int multiplier = ctr; multiplier <=  maxPrime / 2; multiplier++)
            if (ctr * multiplier <= maxPrime)
               values.SetValue(1, ctr * multiplier);
      }      

      List<String> primes = new List<String>();
      for (int ctr = values.GetLowerBound(0); ctr <= values.GetUpperBound(0); ctr++)
         if ((int) values.GetValue(ctr) == 0) 
            primes.Add(ctr.ToString() + " ");
      return primes;
   }   
}
// The example displays the following output: 
//    Primes less than 100: 
//       2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
Version Information

.NET Framework

Supported in: 4.6, 4.5, 4

.NET Framework Client Profile

Supported in: 4

Portable Class Library

Supported in: Portable Class Library

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8