C# Exception Handling
C# File I/O
C# Delegates and Events
C# Generics
C# Async Programming
C# String Basics

C# String Basics

In C#, string is an immutable sequence of Unicode characters. It is an alias for System.String.

1 - Creating Strings

string name    = "Alice";
string empty   = "";
string nullStr = null;

// Verbatim string — backslashes are literal
string path = @"C:\Users\Alice\Documents";

// Multi-line verbatim
string multiLine = @"Line 1
Line 2
Line 3";

2 - String Interpolation (C# 6+)

string firstName = "Alice";
int age = 30;
string msg = $"Hello, {firstName}! You are {age} years old.";
Console.WriteLine(msg); // Hello, Alice! You are 30 years old.

// Expressions inside {}
Console.WriteLine($"Next year: {age + 1}");
Console.WriteLine($"Upper: {firstName.ToUpper()}");

3 - Escape Sequences

Console.WriteLine("Tab:\there");     // Tab:    here
Console.WriteLine("New\nLine");       // new line
Console.WriteLine("Quote: \"hi\"");   // Quote: "hi"
Console.WriteLine("Backslash: \");   // Backslash: \

4 - String Comparison

string a = "hello";
string b = "HELLO";

Console.WriteLine(a == b);                                           // false
Console.WriteLine(a.Equals(b, StringComparison.OrdinalIgnoreCase)); // true
Console.WriteLine(string.Compare(a, b, ignoreCase: true) == 0);     // true

Note: Strings in C# are immutable — every operation that appears to "modify" a string actually creates a new string object. For repeated concatenation in loops, use StringBuilder instead.

-Tip-