Export (0) Print
Expand All

String.Concat Method (Object, Object, Object, Object, ...)

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

This API is not CLS-compliant. The CLS-compliant alternative is Concat.

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

C# does not support methods that use variable length arguments (varargs).
The compiler automatically resolves calls to this method to the same method that uses a parameter array.

Parameters

arg0
Type: System.Object

The first object to concatenate.

arg1
Type: System.Object

The second object to concatenate.

arg2
Type: System.Object

The third object to concatenate.

arg3
Type: System.Object

The fourth object to concatenate.

Return Value

Type: System.String
The concatenated string representation of each value in the parameter list.

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.

NoteNote

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[]).

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.

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

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

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.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft