Addition and Concatenation in C#

CsharpServer Side ProgrammingProgramming

To add and concatenate strings in C#, use the string. Concat method. The plus operator can also be used for the same purpose of concatenation.

Plus Operator

string str2 = "Hanks" + str1;

Example

Let us see an example of + operator to concatenate strings −

Live Demo

using System;

class Program {
   static void Main() {
      string str1 = "Tom";

      // concatenation
      string str2 = "Hanks" + str1;

      Console.WriteLine(str2);
   }
}

Output

HanksTom

String.concat

string str2 = string.Concat("Hanks", str1);

Example

Let us see an example of string.concat to concatenate strings in C# −

Live Demo

using System;

class Program {
   static void Main() {
      string str1 = "Tom";

      // concatenation
      string str2 = string.Concat("Hanks", str1);

      Console.WriteLine(str2);
   }
}

Output

HanksTom
raja
Published on 24-Jul-2018 11:36:39
Advertisements