Addition and Concatenation in C#

In C#, addition refers to mathematical operations with numeric types, while concatenation refers to joining strings together. The + operator can perform both operations depending on the operand types, and C# provides several methods for string concatenation.

Syntax

Following is the syntax for numeric addition −

int result = number1 + number2;

Following is the syntax for string concatenation using the + operator −

string result = string1 + string2;

Following is the syntax for string concatenation using String.Concat() method −

string result = String.Concat(string1, string2);

Numeric Addition

Example

The following example demonstrates numeric addition with different data types −

using System;

class Program {
   static void Main() {
      int a = 10;
      int b = 20;
      double c = 15.5;
      double d = 25.3;
      
      int intResult = a + b;
      double doubleResult = c + d;
      double mixedResult = a + c;
      
      Console.WriteLine("Integer addition: " + a + " + " + b + " = " + intResult);
      Console.WriteLine("Double addition: " + c + " + " + d + " = " + doubleResult);
      Console.WriteLine("Mixed addition: " + a + " + " + c + " = " + mixedResult);
   }
}

The output of the above code is −

Integer addition: 10 + 20 = 30
Double addition: 15.5 + 25.3 = 40.8
Mixed addition: 10 + 15.5 = 25.5

String Concatenation Using Plus Operator

Example

The following example shows string concatenation using the + operator −

using System;

class Program {
   static void Main() {
      string str1 = "Tom";
      string str2 = "Hanks";
      
      // concatenation using + operator
      string result1 = str2 + str1;
      string result2 = str1 + " " + str2;
      
      Console.WriteLine("Without space: " + result1);
      Console.WriteLine("With space: " + result2);
      Console.WriteLine("With literal: " + "Hello " + str1);
   }
}

The output of the above code is −

Without space: HanksTom
With space: Tom Hanks
With literal: Hello Tom

String Concatenation Using String.Concat()

Example

The following example demonstrates the String.Concat() method for string concatenation −

using System;

class Program {
   static void Main() {
      string str1 = "Tom";
      string str2 = "Hanks";
      
      // concatenation using String.Concat
      string result1 = String.Concat(str2, str1);
      string result2 = String.Concat(str1, " ", str2);
      string result3 = String.Concat("Hello ", str1, "!");
      
      Console.WriteLine("Two strings: " + result1);
      Console.WriteLine("With space: " + result2);
      Console.WriteLine("Multiple strings: " + result3);
   }
}

The output of the above code is −

Two strings: HanksTom
With space: Tom Hanks
Multiple strings: Hello Tom!

Comparison of Concatenation Methods

Method Syntax Best For
Plus Operator (+) string1 + string2 Simple concatenation of few strings
String.Concat() String.Concat(string1, string2) Multiple strings, null-safe concatenation
StringBuilder sb.Append(string) Multiple concatenations in loops

Mixed Operations

Example

The following example shows how C# handles mixed numeric and string operations −

using System;

class Program {
   static void Main() {
      int x = 10;
      int y = 20;
      string text = "Result: ";
      
      // Addition first, then concatenation
      string result1 = text + (x + y);
      
      // Concatenation from left to right
      string result2 = text + x + y;
      
      Console.WriteLine(result1);
      Console.WriteLine(result2);
   }
}

The output of the above code is −

Result: 30
Result: 1020

Conclusion

The + operator performs numeric addition when used with numbers and string concatenation when used with strings. String.Concat() provides a more explicit approach to string concatenation and can handle multiple strings efficiently. Understanding the difference is crucial for proper C# programming.

Updated on: 2026-03-17T07:04:35+05:30

415 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements