This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

C# Programming
.NET 1.1+

C# Basic String Operators

The seventeenth part of the C# Fundamentals tutorial looks at the use of the basic operators used earlier in the tutorial and how they can be applied to the string data type. These operators form the starting knowledge of C# string manipulation.

Concatenation Operator

Previously in this tutorial we examined the arithmetic operators for the numeric data types. The string data type does not contain any numeric value and therefore cannot be used in arithmetic operations. However, the addition operator (+) is available for strings. It is used to concatenate two strings.

string start = "This is a ";
string end = "concatenated string!";

string concat = start + end;    // concat = "This is a concatenated string!"

The concatenation operator can be used in a compound assignment statement with the addition of an equals sign (+=).

string start = "This is a ";
string end = "concatenated string!";

start += end;                   // start = "This is a concatenated string!"

Relational Operators

The string data type supports the use of two relational operators. These are for equality (==) and inequality (!=) testing. The remainder of the relational operators are unavailable. It is important to note that the relational operators perform case-sensitive comparisons as shown below:

string s1 = "String to compare.";
string s2 = "String to compare.";
string s3 = "String to Compare.";   // Note the capital "C"
bool result;

result = s1 == s2;                  // result = true
result = s1 == s3;                  // result = false
result = s1 != s2;                  // result = false
result = s1 != s3;                  // result = true

Null Coalescing Operator

In the previous part of the tutorial we introduced the string data type as a class and explained that all strings are actually objects. It was also noted that all objects are nullable. This means that the null coalescing operator (??) can be used with strings when developing for .NET framework 2.0.

string notNullString = "A string";
string nullString = null;
string nullTestString;

nullTestString = notNullString ?? "Null";   // nullTestString = "A string"
nullTestString = nullString ?? "Null";      // nullTestString = "Null"

The null coalescing operator is not available in earlier versions of the .NET framework. However, similar functionality can be provided using the conditional and equality operators.

string notNullString = "A string";
string nullString = null;
string nullTestString;

// Mimic the functionality of the null coalescing operator
nullTestString = notNullString == null ? "Null" : notNullString;    // "A string"
nullTestString = nullString == null ? "Null" : nullString;          // "Null"


The number of basic operators available for use with strings appears to be low. However, the data type is based on a class that includes many more powerful methods for string manipulation. These will be examined in later articles.

28 October 2006