String.Concat Method (IEnumerable<String>)
Concatenates the members of a constructed IEnumerable<T> collection of type String.
Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Parameters
- values
- Type: System.Collections.Generic.IEnumerable<String>
A collection object that implements IEnumerable<T> and whose generic type argument is String.
| Exception | Condition |
|---|---|
| ArgumentNullException | values is null. |
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, IEnumerable<String>) method.
An Empty string is used in place of any null argument.
Concat(IEnumerable<String>) 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 Enumerable.Where method is passed to the Concat(IEnumerable<String>) method to display the result as a single string.
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
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 String, which it then passes to the Concat(IEnumerable<String>) method.
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
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.