C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Tip: The main reason to use "+" instead of string.Concat is readability. Both expressions will compile into the same code.
However: We concat a string literal ("string1") with a string variable (s1). The reference is stored in the s2 local.
String LiteralC# program that concatenates strings
using System;
class Program
{
static void Main()
{
// ... Create a new string reference.
// It points to the literal.
string s1 = "string2";
// ... Add another string to the start.
string s2 = "string1" + s1;
Console.WriteLine(s2);
}
}
Output
string1string2
C# program that uses string.Concat method
using System;
class Program
{
static void Main()
{
string s1 = "string2";
string s2 = string.Concat("string1", s1);
Console.WriteLine(s2);
}
}
Output
string1string2
Info: Adding to the start is called prepending. You can append strings by using + or string.Concat.
C# program that concats 3 strings
using System;
class Program
{
static void Main()
{
string s1 = "string1";
string s2 = "string2";
// Combine 3 strings.
string s3 = s1 + s2 + "string3";
// Write the result to the screen.
Console.WriteLine(s3);
}
}
Output
string1string2string3
Note: The string.Concat method here is equivalent to the string.Join method invocation with an empty separator.
C# program that concats string list
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
// Create list of 3 strings.
var list = new List<string>();
list.Add("cat");
list.Add("dog");
list.Add("Codex");
// Concat the list.
string concat = string.Concat(list);
Console.WriteLine(concat);
// Join the list.
string join = string.Join("", list);
Console.WriteLine(join);
}
}
Output
catdogCodex
catdogCodex
Version 1: This version of the code uses Concat to combine 3 strings together into 1 string.
Version 2: Here we use string.Format, and a format string with 3 substitution markers, to combine the strings.
Result: The plus operator (which is the Concat method) is faster. We should avoid Format unless more advanced features are needed.
C# program that benchmarks Concat, Format
using System;
using System.Diagnostics;
class Program
{
const int _max = 1000000;
static void Main()
{
string value1 = "bird";
string value2 = "frog";
string value3 = "dog";
var s1 = Stopwatch.StartNew();
// Version 1: use string.Concat.
for (int i = 0; i < _max; i++)
{
string result = value1 + value2 + value3;
if (result == null)
{
return;
}
}
s1.Stop();
var s2 = Stopwatch.StartNew();
// Version 2: use string.Format.
for (int i = 0; i < _max; i++)
{
string result = string.Format("{0}{1}{2}",
value1,
value2,
value3);
if (result == null)
{
return;
}
}
s2.Stop();
Console.WriteLine(((double)(s1.Elapsed.TotalMilliseconds * 1000000) /
_max).ToString("0.00 ns"));
Console.WriteLine(((double)(s2.Elapsed.TotalMilliseconds * 1000000) /
_max).ToString("0.00 ns"));
}
}
Output
34.33 ns +
124.46 ns string.Format
Info: Looking into IL Disassembler, we see a list of many Concat methods. When 5 arguments are needed, the params version is used.
IL DisassemblerThus: It may be faster to perform concatenations of at most 4 strings at a time. But usually using StringBuilder is a better choice.
Also: Microsoft provides information on the string.Concat overloads. Some accept arrays, and some do not.
String.Concat Method: Microsoft DocsNote: Appending an empty string is fast, but not doing so is even faster, so the string.Concat method would be superior here.
JoinTip: The string.Concat method in the .NET Framework 4.0 has an overload that receives an IEnumerable collection of type string.
IEnumerable