.Concat()

Anonymous contributor's avatar
Anonymous contributor
Published Nov 15, 2024
Contribute to Docs

The .Concat() method combines strings by appending one string to the end of another. This method is similar in function to using the + or += operator to concatenate strings, though the compiler handles those operators differently for optimization in certain scenarios.

Syntax

string.Concat(string1, string2, ...)
  • string1, string2, ...: The strings that will be concatenated together.

Example

The following example uses the .Concat() method to combine three strings:

using System;
public class ConcatMethod
{
public static void Main()
{
string firstString = "Hello";
string secondString = "World!";
string fullSentence = string.Concat(firstString, " ", secondString);
Console.WriteLine(fullSentence);
}
}

This results in the following output:

Hello World!

Codebyte Example

The below codebyte example demonstrates how .Concat() can be used with a foreach loop to iterate through a list and output multiple concatenated strings:

Code
Output
Loading...

All contributors

Contribute to Docs

Learn C# on Codecademy