C# .Concat()
Anonymous contributor
Published Nov 15, 2024
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:
All contributors
- Anonymous contributor
Learn C# on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours
- Learn Microsoft's popular C# programming language, used to make websites, mobile apps, video games, VR, and more.
- Beginner Friendly.15 hours