String Fundamentals - Understanding Strings and Immutability in C#

Vaibhav • September 10, 2025

Strings are everywhere in programming: user names, messages, file paths, commands, and more. In C#, strings are a special type-string-that behaves differently from arrays and collections. This article introduces the basics of strings, how they work, and why their immutability matters for safe, predictable code.

This article assumes you’re comfortable with arrays and collections. We’ll focus on what makes strings unique, how to create and use them, and how to avoid common pitfalls.

What is a string?

A string is a sequence of characters-letters, digits, punctuation, or symbols. In C#, the string type is an alias for System.String. You can create strings using quotes:

string greeting = "Hello, world!";
string name = "Vaibhav";
string empty = ""; // an empty string
  • Strings can be empty ("") or contain any sequence of characters.
  • They are reference types, but behave like value types in many ways.

String immutability

In C#, strings are immutable: once created, their contents cannot be changed. Any operation that looks like it “modifies” a string actually creates a new string.

string original = "cat";
string upper = original.ToUpper(); // creates a new string "CAT"
Console.WriteLine(original); // "cat"
Console.WriteLine(upper);    // "CAT"
  • original is unchanged; upper is a new string.
  • This makes strings safe to share and pass around-no code can accidentally change your string.

Immutability means string operations can be expensive if you build up large strings by repeated concatenation. For heavy modifications, use StringBuilder (covered later in this chapter).

Common string operations

  • Length - number of characters in the string.
  • ToUpper(), ToLower() - change case.
  • Trim() - remove whitespace from start/end.
  • Substring(start, length) - extract part of a string.
  • Contains(), StartsWith(), EndsWith() - search for text.
string phrase = "  Hello, C#  ";
Console.WriteLine(phrase.Length);           // 13
Console.WriteLine(phrase.Trim());           // "Hello, C#"
Console.WriteLine(phrase.Contains("C#"));   // True
Console.WriteLine(phrase.Substring(2, 5));  // "Hello"

Accessing individual characters

You can access characters in a string by index, just like an array:

string word = "rock";
char first = word[0]; // 'r'
char last = word[word.Length - 1]; // 'k'

Remember: you cannot assign to word[0]-strings are immutable!

String comparison

Use == to compare strings for equality. C# compares the contents, not just the references.

string a = "test";
string b = "test";
Console.WriteLine(a == b); // True

For case-insensitive comparison, use Equals with a comparison type:

string x = "Hello";
string y = "hello";
bool same = x.Equals(y, StringComparison.OrdinalIgnoreCase); // True

Null and empty strings

A string can be null (no value) or empty (""). Always check for both when validating input.

string input = Console.ReadLine();
if (string.IsNullOrEmpty(input))
    Console.WriteLine("No input provided.");

Summary

Strings are a core part of every C# program. They are immutable, safe to share, and packed with useful methods for searching, formatting, and manipulating text. Understanding how strings work-and why immutability matters-will help you write safer, more reliable code. In the next article, we’ll explore how to create and assign strings in more detail.