C# String Operators

C# provides several string operators that allow you to perform operations and comparisons on strings. The primary string operators include equality (==), inequality (!=), and the concatenation operator (+). These operators make string manipulation and comparison straightforward and intuitive.

Syntax

Following is the syntax for string comparison operators −

bool result = string1 == string2;  // Equality
bool result = string1 != string2;  // Inequality
string result = string1 + string2; // Concatenation

String Equality Operator (==)

The equality operator checks if two strings have the same content. It performs a value comparison, not a reference comparison for strings −

using System;

public class Demo {
   public static void Main() {
      string str1 = "Hello";
      string str2 = "Hello";
      string str3 = "World";
      
      Console.WriteLine("str1: " + str1);
      Console.WriteLine("str2: " + str2);
      Console.WriteLine("str3: " + str3);
      Console.WriteLine();
      
      Console.WriteLine("str1 == str2: " + (str1 == str2));
      Console.WriteLine("str1 == str3: " + (str1 == str3));
   }
}

The output of the above code is −

str1: Hello
str2: Hello
str3: World

str1 == str2: True
str1 == str3: False

String Inequality Operator (!=)

The inequality operator checks if two strings have different content. It returns true when strings are not equal −

using System;

public class Demo {
   public static void Main() {
      string str1 = "Apple";
      string str2 = "Orange";
      string str3 = "Apple";
      
      Console.WriteLine("str1: " + str1);
      Console.WriteLine("str2: " + str2);
      Console.WriteLine("str3: " + str3);
      Console.WriteLine();
      
      Console.WriteLine("str1 != str2: " + (str1 != str2));
      Console.WriteLine("str1 != str3: " + (str1 != str3));
   }
}

The output of the above code is −

str1: Apple
str2: Orange
str3: Apple

str1 != str2: True
str1 != str3: False

String Concatenation Operator (+)

The plus operator combines two or more strings into a single string. This is one of the most commonly used string operators −

using System;

public class Demo {
   public static void Main() {
      string firstName = "John";
      string lastName = "Doe";
      string space = " ";
      
      string fullName = firstName + space + lastName;
      string greeting = "Hello, " + fullName + "!";
      
      Console.WriteLine("First Name: " + firstName);
      Console.WriteLine("Last Name: " + lastName);
      Console.WriteLine("Full Name: " + fullName);
      Console.WriteLine("Greeting: " + greeting);
   }
}

The output of the above code is −

First Name: John
Last Name: Doe
Full Name: John Doe
Greeting: Hello, John Doe!

Comparison with Other String Methods

Operator/Method Purpose Case Sensitive
== operator String equality comparison Yes
!= operator String inequality comparison Yes
+ operator String concatenation N/A
String.Equals() Method-based equality with options Configurable

Conclusion

C# string operators provide simple and efficient ways to compare and concatenate strings. The equality (==) and inequality (!=) operators perform value-based comparisons, while the concatenation operator (+) combines strings seamlessly.

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

640 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements