Edit

Share via


String.Concat Method

Definition

Concatenates one or more instances of String, or the String representations of the values of one or more instances of Object.

Overloads

Concat(String, String, String, String)

Concatenates four specified instances of String.

Concat(ReadOnlySpan<Char>, ReadOnlySpan<Char>, ReadOnlySpan<Char>, ReadOnlySpan<Char>)

Concatenates the string representations of four specified read-only character spans.

Concat(Object, Object, Object, Object)

Concatenates the string representations of four specified objects and any objects specified in an optional variable length parameter list.

Concat(String, String, String)

Concatenates three specified instances of String.

Concat(ReadOnlySpan<Char>, ReadOnlySpan<Char>, ReadOnlySpan<Char>)

Concatenates the string representations of three specified read-only character spans.

Concat(Object, Object, Object)

Concatenates the string representations of three specified objects.

Concat(String, String)

Concatenates two specified instances of String.

Concat(IEnumerable<String>)

Concatenates the members of a constructed IEnumerable<T> collection of type String.

Concat(Object, Object)

Concatenates the string representations of two specified objects.

Concat(String[])

Concatenates the elements of a specified String array.

Concat(ReadOnlySpan<String>)

Concatenates the elements of a specified span of String.

Concat(ReadOnlySpan<Object>)

Concatenates the string representations of the elements in a specified span of objects.

Concat(Object[])

Concatenates the string representations of the elements in a specified Object array.

Concat(Object)

Creates the string representation of a specified object.

Concat(ReadOnlySpan<Char>, ReadOnlySpan<Char>)

Concatenates the string representations of two specified read-only character spans.

Concat<T>(IEnumerable<T>)

Concatenates the members of an IEnumerable<T> implementation.

Remarks

Note

You can also use your language's string concatenation operator, such as + in C# and F#, or & and + in Visual Basic, to concatenate strings. Both compilers translate the concatenation operator into a call to one of the overloads of String.Concat.

Concat(String, String, String, String)

Source:
String.Manipulation.cs
Source:
String.Manipulation.cs
Source:
String.Manipulation.cs

Concatenates four specified instances of String.

C#
public static string Concat(string str0, string str1, string str2, string str3);
C#
public static string Concat(string? str0, string? str1, string? str2, string? str3);

Parameters

str0
String

The first string to concatenate.

str1
String

The second string to concatenate.

str2
String

The third string to concatenate.

str3
String

The fourth string to concatenate.

Returns

The concatenation of str0, str1, str2, and str3.

Examples

The following example defines an array of four-letter words and stores their individual letters to a string array in order to scramble them. It then calls the Concat(String, String, String, String) method to reassemble the scrambled words.

C#
using System;
using System.Collections;

public class Example
{
   public static void Main()
   {
      const int WORD_SIZE = 4;
      
      // Define some 4-letter words to be scrambled.
      string[] words = { "home", "food", "game", "rest" };
      // Define two arrays equal to the number of letters in each word.
      double[] keys = new double[WORD_SIZE];
      string[] letters = new string[WORD_SIZE];
      // Initialize the random number generator.
      Random rnd = new Random();
      
      // Scramble each word.
      foreach (string word in words)
      {
         for (int ctr = 0; ctr < word.Length; ctr++)
         {
            // Populate the array of keys with random numbers.
            keys[ctr] = rnd.NextDouble();
            // Assign a letter to the array of letters.
            letters[ctr] = word[ctr].ToString();
         }   
         // Sort the array. 
         Array.Sort(keys, letters, 0, WORD_SIZE, Comparer.Default);      
         // Display the scrambled word.
         string scrambledWord = String.Concat(letters[0], letters[1], 
                                              letters[2], letters[3]);
         Console.WriteLine("{0} --> {1}", word, scrambledWord);
      } 
   }
}
// The example displays output like the following:
//       home --> mheo
//       food --> oodf
//       game --> aemg
//       rest --> trse

Remarks

The method concatenates str0, str1, str2, and str3; it does not add any delimiters.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Concat(ReadOnlySpan<Char>, ReadOnlySpan<Char>, ReadOnlySpan<Char>, ReadOnlySpan<Char>)

Source:
String.Manipulation.cs
Source:
String.Manipulation.cs
Source:
String.Manipulation.cs

Concatenates the string representations of four specified read-only character spans.

C#
public static string Concat(ReadOnlySpan<char> str0, ReadOnlySpan<char> str1, ReadOnlySpan<char> str2, ReadOnlySpan<char> str3);

Parameters

str0
ReadOnlySpan<Char>

The first read-only character span to concatenate.

str1
ReadOnlySpan<Char>

The second read-only character span to concatenate.

str2
ReadOnlySpan<Char>

The third read-only character span to concatenate.

str3
ReadOnlySpan<Char>

The fourth read-only character span to concatenate.

Returns

The concatenated string representations of the values of str0, str1, str2 and str3.

Applies to

.NET 9 and other versions
Product Versions
.NET Core 3.0, Core 3.1, 5, 6, 7, 8, 9

Concat(Object, Object, Object, Object)

Important

This API is not CLS-compliant.

Concatenates the string representations of four specified objects and any objects specified in an optional variable length parameter list.

C#
[System.CLSCompliant(false)]
public static string Concat(object arg0, object arg1, object arg2, object arg3);

Parameters

arg0
Object

The first object to concatenate.

arg1
Object

The second object to concatenate.

arg2
Object

The third object to concatenate.

arg3
Object

The fourth object to concatenate.

Returns

The concatenated string representation of each value in the parameter list.

Attributes

Examples

The following example illustrates the use of the Concat(Object, Object, Object, Object) method to concatenate a list of variable parameters. In this case, the method is called with nine parameters.

C#
using System;
using System.Collections;

public class Example
{
   public static void Main()
   {
      const int WORD_SIZE = 4;
      
      // Define some 4-letter words to be scrambled.
      string[] words = { "home", "food", "game", "rest" };
      // Define two arrays equal to the number of letters in each word.
      double[] keys = new double[WORD_SIZE];
      string[] letters = new string[WORD_SIZE];
      // Initialize the random number generator.
      Random rnd = new Random();
      
      // Scramble each word.
      foreach (string word in words)
      {
         for (int ctr = 0; ctr < word.Length; ctr++)
         {
            // Populate the array of keys with random numbers.
            keys[ctr] = rnd.NextDouble();
            // Assign a letter to the array of letters.
            letters[ctr] = word[ctr].ToString();
         }   
         // Sort the array. 
         Array.Sort(keys, letters, 0, WORD_SIZE, Comparer.Default);      
         // Display the scrambled word.
         string scrambledWord = String.Concat(letters[0], letters[1], 
                                              letters[2], letters[3]);
         Console.WriteLine("{0} --> {1}", word, scrambledWord);
      } 
   }
}
// The example displays output like the following:
//       home --> mheo
//       food --> oodf
//       game --> aemg
//       rest --> trse

Remarks

Note

This API is not CLS-compliant. The CLS-compliant alternative is String.Concat(Object[]). The C# and Visual Basic compilers automatically resolve a call to this method as a call to String.Concat(Object[]).

The method concatenates each object in the parameter list by calling its parameterless ToString method; it does not add any delimiters.

String.Empty is used in place of any null argument.

Note

The last parameter of the Concat method is an optional comma-delimited list of one or more additional objects to concatenate.

Notes to Callers

This method is marked with the vararg keyword, which means that it supports a variable number of parameters. The method can be called from Visual C++, but it cannot be called from C# or Visual Basic code. The C# and Visual Basic compilers resolve calls to Concat(Object, Object, Object, Object) as calls to Concat(Object[]).

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

Concat(String, String, String)

Source:
String.Manipulation.cs
Source:
String.Manipulation.cs
Source:
String.Manipulation.cs

Concatenates three specified instances of String.

C#
public static string Concat(string str0, string str1, string str2);
C#
public static string Concat(string? str0, string? str1, string? str2);

Parameters

str0
String

The first string to concatenate.

str1
String

The second string to concatenate.

str2
String

The third string to concatenate.

Returns

The concatenation of str0, str1, and str2.

Examples

The following example uses the Concat method to concatenate three strings and displays the result.

C#
using System;

public class Example
{
   public static void Main()
   {
      String s1 = "We went to a bookstore, ";
      String s2 = "a movie, ";
      String s3 = "and a restaurant.";

      var s = String.Concat(s1, s2, s3);
      Console.WriteLine(s);
   }
}
// The example displays the following output:
//      We went to a bookstore, a movie, and a restaurant.

Remarks

The method concatenates str0, str1, and str2; it does not add any delimiters.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Concat(ReadOnlySpan<Char>, ReadOnlySpan<Char>, ReadOnlySpan<Char>)

Source:
String.Manipulation.cs
Source:
String.Manipulation.cs
Source:
String.Manipulation.cs

Concatenates the string representations of three specified read-only character spans.

C#
public static string Concat(ReadOnlySpan<char> str0, ReadOnlySpan<char> str1, ReadOnlySpan<char> str2);

Parameters

str0
ReadOnlySpan<Char>

The first read-only character span to concatenate.

str1
ReadOnlySpan<Char>

The second read-only character span to concatenate.

str2
ReadOnlySpan<Char>

The third read-only character span to concatenate.

Returns

The concatenated string representations of the values of str0, str1 and str2.

Applies to

.NET 9 and other versions
Product Versions
.NET Core 3.0, Core 3.1, 5, 6, 7, 8, 9

Concat(Object, Object, Object)

Source:
String.Manipulation.cs
Source:
String.Manipulation.cs
Source:
String.Manipulation.cs

Concatenates the string representations of three specified objects.

C#
public static string Concat(object arg0, object arg1, object arg2);
C#
public static string Concat(object? arg0, object? arg1, object? arg2);

Parameters

arg0
Object

The first object to concatenate.

arg1
Object

The second object to concatenate.

arg2
Object

The third object to concatenate.

Returns

The concatenated string representations of the values of arg0, arg1, and arg2.

Examples

The following example demonstrates the Concat method.

C#
using System;

class stringConcat5 {
    public static void Main() {
    int i = -123;
    Object o = i;
    Object[] objs = new Object[] {-123, -456, -789};

    Console.WriteLine("Concatenate 1, 2, and 3 objects:");
    Console.WriteLine("1) {0}", String.Concat(o));
    Console.WriteLine("2) {0}", String.Concat(o, o));
    Console.WriteLine("3) {0}", String.Concat(o, o, o));

    Console.WriteLine("\nConcatenate 4 objects and a variable length parameter list:");
    Console.WriteLine("4) {0}", String.Concat(o, o, o, o));
    Console.WriteLine("5) {0}", String.Concat(o, o, o, o, o));

    Console.WriteLine("\nConcatenate a 3-element object array:");
    Console.WriteLine("6) {0}", String.Concat(objs));
    }
}
// The example displays the following output:
//    Concatenate 1, 2, and 3 objects:
//    1) -123
//    2) -123-123
//    3) -123-123-123
//
//    Concatenate 4 objects and a variable length parameter list:
//    4) -123-123-123-123
//    5) -123-123-123-123-123
//
//    Concatenate a 3-element object array:
//    6) -123-456-789

Remarks

The method concatenates arg0, arg1, and arg2 by calling the parameterless ToString method of each object; it does not add any delimiters.

String.Empty is used in place of any null argument.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Concat(String, String)

Source:
String.Manipulation.cs
Source:
String.Manipulation.cs
Source:
String.Manipulation.cs

Concatenates two specified instances of String.

C#
public static string Concat(string str0, string str1);
C#
public static string Concat(string? str0, string? str1);

Parameters

str0
String

The first string to concatenate.

str1
String

The second string to concatenate.

Returns

The concatenation of str0 and str1.

Examples

The following example concatenates a person's first, middle, and last name.

C#
using System;

public class ConcatTest {
    public static void Main() {

        // we want to simply quickly add this person's name together
        string fName = "Simon";
        string mName = "Jake";
        string lName = "Harrows";

        // because we want a name to appear with a space in between each name,
        // put a space on the front of the middle, and last name, allowing for
        // the fact that a space may already be there
        mName = " " + mName.Trim();
        lName = " " + lName.Trim();

        // this line simply concatenates the two strings
        Console.WriteLine("Welcome to this page, '{0}'!", string.Concat( string.Concat(fName, mName), lName ) );
    }
}
// The example displays the following output:
//        Welcome to this page, 'Simon Jake Harrows'!

Remarks

The method concatenates str0 and str1; it does not add any delimiters.

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

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Concat(IEnumerable<String>)

Source:
String.Manipulation.cs
Source:
String.Manipulation.cs
Source:
String.Manipulation.cs

Concatenates the members of a constructed IEnumerable<T> collection of type String.

C#
public static string Concat(System.Collections.Generic.IEnumerable<string> values);
C#
public static string Concat(System.Collections.Generic.IEnumerable<string?> values);
C#
[System.Runtime.InteropServices.ComVisible(false)]
public static string Concat(System.Collections.Generic.IEnumerable<string> values);

Parameters

values
IEnumerable<String>

A collection object that implements IEnumerable<T> and whose generic type argument is String.

Returns

The concatenated strings in values, or Empty if values is an empty IEnumerable(Of String).

Attributes

Exceptions

values is null.

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

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

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, IEnumerable<String>) method.

An Empty string is used in place of any null element in values.

If values is an empty IEnumerable(Of String), the method returns String.Empty. If values is null, the method throws an ArgumentNullException exception.

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.

C#
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(((char)(charValue + ctr)).ToString());
      return alphabet; 
   }
}
// The example displays the following output:
//      MNOPQRSTUVWXYZ

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Concat(Object, Object)

Source:
String.Manipulation.cs
Source:
String.Manipulation.cs
Source:
String.Manipulation.cs

Concatenates the string representations of two specified objects.

C#
public static string Concat(object arg0, object arg1);
C#
public static string Concat(object? arg0, object? arg1);

Parameters

arg0
Object

The first object to concatenate.

arg1
Object

The second object to concatenate.

Returns

The concatenated string representations of the values of arg0 and arg1.

Examples

The following example demonstrates the Concat method.

C#
using System;

class stringConcat5 {
    public static void Main() {
    int i = -123;
    Object o = i;
    Object[] objs = new Object[] {-123, -456, -789};

    Console.WriteLine("Concatenate 1, 2, and 3 objects:");
    Console.WriteLine("1) {0}", String.Concat(o));
    Console.WriteLine("2) {0}", String.Concat(o, o));
    Console.WriteLine("3) {0}", String.Concat(o, o, o));

    Console.WriteLine("\nConcatenate 4 objects and a variable length parameter list:");
    Console.WriteLine("4) {0}", String.Concat(o, o, o, o));
    Console.WriteLine("5) {0}", String.Concat(o, o, o, o, o));

    Console.WriteLine("\nConcatenate a 3-element object array:");
    Console.WriteLine("6) {0}", String.Concat(objs));
    }
}
// The example displays the following output:
//    Concatenate 1, 2, and 3 objects:
//    1) -123
//    2) -123-123
//    3) -123-123-123
//
//    Concatenate 4 objects and a variable length parameter list:
//    4) -123-123-123-123
//    5) -123-123-123-123-123
//
//    Concatenate a 3-element object array:
//    6) -123-456-789

Remarks

The method concatenates arg0 and arg1 by calling the parameterless ToString method of arg0 and arg1; it does not add any delimiters.

String.Empty is used in place of any null argument.

If either of the arguments is an array reference, the method concatenates a string representing that array, instead of its members (for example, "System.String[]").

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Concat(String[])

Source:
String.Manipulation.cs
Source:
String.Manipulation.cs
Source:
String.Manipulation.cs

Important

This API is not CLS-compliant.

Concatenates the elements of a specified String array.

C#
public static string Concat(params string[] values);
C#
public static string Concat(params string?[] values);
C#
[System.CLSCompliant(false)]
public static string Concat(params string[] values);

Parameters

values
String[]

An array of string instances.

Returns

The concatenated elements of values.

Attributes

Exceptions

values is null.

Out of memory.

Examples

The following example demonstrates the use of the Concat method with a String array.

C#
using System;

public class Example
{
    public static void Main()
    {
        // Make an array of strings. Note that we have included spaces.
        string [] s = { "hello ", "and ", "welcome ", "to ",
                        "this ", "demo! " };

        // Put all the strings together.
        Console.WriteLine(string.Concat(s));

        // Sort the strings, and put them together.
        Array.Sort(s);
        Console.WriteLine(string.Concat(s));
    }
}
// The example displays the following output:
//       hello and welcome to this demo!
//       and demo! hello this to welcome

Remarks

The method concatenates each object in values; it does not add any delimiters.

An Empty string is used in place of any null object in the array.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Concat(ReadOnlySpan<String>)

Concatenates the elements of a specified span of String.

C#
public static string Concat(scoped ReadOnlySpan<string?> values);

Parameters

values
ReadOnlySpan<String>

A span of String instances.

Returns

The concatenated elements of values.

Applies to

.NET 9
Product Versions
.NET 9

Concat(ReadOnlySpan<Object>)

Concatenates the string representations of the elements in a specified span of objects.

C#
public static string Concat(scoped ReadOnlySpan<object?> args);

Parameters

args
ReadOnlySpan<Object>

A span of objects that contains the elements to concatenate.

Returns

The concatenated string representations of the values of the elements in args.

Applies to

.NET 9
Product Versions
.NET 9

Concat(Object[])

Source:
String.Manipulation.cs
Source:
String.Manipulation.cs
Source:
String.Manipulation.cs

Concatenates the string representations of the elements in a specified Object array.

C#
public static string Concat(params object[] args);
C#
public static string Concat(params object?[] args);

Parameters

args
Object[]

An object array that contains the elements to concatenate.

Returns

The concatenated string representations of the values of the elements in args.

Exceptions

args is null.

Out of memory.

Examples

The following example demonstrates the use of the Concat method with an Object array.

C#
using System;

public class ConcatTest {
    public static void Main() {
        // Create a group of objects.
        Test1 t1 = new Test1();
        Test2 t2 = new Test2();
        int i = 16;
        string s = "Demonstration";

        // Place the objects in an array.
        object [] o = { t1, i, t2, s };

        // Concatenate the objects together as a string. To do this,
        // the ToString method of each of the objects is called.
        Console.WriteLine(string.Concat(o));
    }
}

// Create two empty test classes.
class Test1 {
}

class Test2 {
}
// The example displays the following output:
//       Test116Test2Demonstration

Remarks

The method concatenates each object in args by calling the parameterless ToString method of that object; it does not add any delimiters.

String.Empty is used in place of any null object in the array.

Notes to Callers

This method is not called by C++ code. The C++ compiler resolves calls to Concat that have four or more object parameters as a call to Concat(Object, Object, Object, Object).

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Concat(Object)

Source:
String.Manipulation.cs
Source:
String.Manipulation.cs
Source:
String.Manipulation.cs

Creates the string representation of a specified object.

C#
public static string Concat(object arg0);
C#
public static string Concat(object? arg0);

Parameters

arg0
Object

The object to represent, or null.

Returns

The string representation of the value of arg0, or Empty if arg0 is null.

Examples

The following example demonstrates the Concat method.

C#
using System;

class stringConcat5 {
    public static void Main() {
    int i = -123;
    Object o = i;
    Object[] objs = new Object[] {-123, -456, -789};

    Console.WriteLine("Concatenate 1, 2, and 3 objects:");
    Console.WriteLine("1) {0}", String.Concat(o));
    Console.WriteLine("2) {0}", String.Concat(o, o));
    Console.WriteLine("3) {0}", String.Concat(o, o, o));

    Console.WriteLine("\nConcatenate 4 objects and a variable length parameter list:");
    Console.WriteLine("4) {0}", String.Concat(o, o, o, o));
    Console.WriteLine("5) {0}", String.Concat(o, o, o, o, o));

    Console.WriteLine("\nConcatenate a 3-element object array:");
    Console.WriteLine("6) {0}", String.Concat(objs));
    }
}
// The example displays the following output:
//    Concatenate 1, 2, and 3 objects:
//    1) -123
//    2) -123-123
//    3) -123-123-123
//
//    Concatenate 4 objects and a variable length parameter list:
//    4) -123-123-123-123
//    5) -123-123-123-123-123
//
//    Concatenate a 3-element object array:
//    6) -123-456-789

Remarks

The Concat(Object) method represents arg0 as a string by calling its parameterless ToString method.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Concat(ReadOnlySpan<Char>, ReadOnlySpan<Char>)

Source:
String.Manipulation.cs
Source:
String.Manipulation.cs
Source:
String.Manipulation.cs

Concatenates the string representations of two specified read-only character spans.

C#
public static string Concat(ReadOnlySpan<char> str0, ReadOnlySpan<char> str1);

Parameters

str0
ReadOnlySpan<Char>

The first read-only character span to concatenate.

str1
ReadOnlySpan<Char>

The second read-only character span to concatenate.

Returns

The concatenated string representations of the values of str0 and str1.

Applies to

.NET 9 and other versions
Product Versions
.NET Core 3.0, Core 3.1, 5, 6, 7, 8, 9

Concat<T>(IEnumerable<T>)

Source:
String.Manipulation.cs
Source:
String.Manipulation.cs
Source:
String.Manipulation.cs

Concatenates the members of an IEnumerable<T> implementation.

C#
public static string Concat<T>(System.Collections.Generic.IEnumerable<T> values);
C#
[System.Runtime.InteropServices.ComVisible(false)]
public static string Concat<T>(System.Collections.Generic.IEnumerable<T> values);

Type Parameters

T

The type of the members of values.

Parameters

values
IEnumerable<T>

A collection object that implements the IEnumerable<T> interface.

Returns

The concatenated members in values.

Attributes

Exceptions

values is null.

Examples

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<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<T>(IEnumerable<T>) method and displayed to the console.

C#
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.Concat(animals.Where( animal => 
                      (animal.Order == "Rodent")));
      Console.WriteLine(output);  
   }
}
// The example displays the following output:
//      SquirrelCapybara

Remarks

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<T>(IEnumerable<T>) is a convenience method that lets you concatenate each element in an IEnumerable<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<T> collection is derived by calling that object's ToString method.

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0