join two strings in csharp

There are several ways to join two strings in C#:

  1. Using the plus (+) operator:
main.cs
string str1 = "Hello";
string str2 = "World";
string result = str1 + " " + str2; //result: "Hello World"
105 chars
4 lines
  1. Using the string.Concat method:
main.cs
string str1 = "Hello";
string str2 = "World";
string result = string.Concat(str1, " ", str2); //result: "Hello World"
118 chars
4 lines
  1. Using the string.Join method:
main.cs
string str1 = "Hello";
string str2 = "World";
string[] strArray = { str1, str2 };
string result = string.Join(" ", strArray); //result: "Hello World"
150 chars
5 lines

gistlibby LogSnag