Export (0) Print
Expand All

String.Join<T> Method (String, IEnumerable<T>)

Concatenates the members of a collection, using the specified separator between each member.

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

[ComVisibleAttribute(false)]
public static string Join<T>(
	string separator,
	IEnumerable<T> values
)

Type Parameters

T

The type of the members of values.

Parameters

separator
Type: System.String

The string to use as a separator. separator is included in the returned string only if values has more than one element.

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

A collection that contains the objects to concatenate.

Return Value

Type: System.String
A string that consists of the members of values delimited by the separator string. If values has no members, the method returns String.Empty.

ExceptionCondition
ArgumentNullException

values is null.

If separator is null, an empty string (String.Empty) is used instead. If any member of values is null, an empty string is used instead.

Join<T>(String, IEnumerable<T>) is a convenience method that lets you concatenate each member of an IEnumerable<T> collection without first converting them to strings. The string representation of each object in the IEnumerable<T> collection is derived by calling that object's ToString method.

This method is particular useful with Language-Integrated Query (LINQ) query expressions. For example, the following code 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 that contains 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 Join<T>(String, IEnumerable<T>) method.

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.Join(" ", animals.Where( animal => 
                      (animal.Order == "Rodent")));
      Console.WriteLine(output);  
   }
}
// The example displays the following output: 
//      Squirrel Capybara

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 List<T> object of type integer, which it then passes to the Join<T>(String, IEnumerable<T>) method.

using System;
using System.Collections.Generic;

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

   private static List<int> GetPrimes(int maxPrime)
   {
      Array values = Array.CreateInstance(typeof(int), 
                              new int[] { maxPrime - 1}, new int[] { 2 });
      // Use Sieve of Eratosthenes 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<int> primes = new List<int>();
      for (int ctr = values.GetLowerBound(0); ctr <= values.GetUpperBound(0); ctr++)
         if ((int) values.GetValue(ctr) == 0) 
            primes.Add(ctr);
      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

.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