Strings
A string in C# is a sequence of characters, and it's immutable — once created, its contents never change. Every method that appears to "modify" a string is actually handing you back a brand-new one.
Building strings: concatenation vs. interpolation
You can glue strings together with +, but once numbers get mixed in, it gets noisy fast. String interpolation — a $ before the opening quote, with expressions inside { } — reads much closer to the sentence you're actually trying to produce:
string name = "Diego"; int orders = 4; string message1 = "Hello, " + name + "! You have " + orders + " orders."; string message2 = $"Hello, {name}! You have {orders} orders."; Console.WriteLine(message1); Console.WriteLine(message2);
Hello, Diego! You have 4 orders. Hello, Diego! You have 4 orders.
Both lines print identical output. message2 just took less effort to write and, more importantly, less effort to read back later — there's no hunting for a missing space between quote marks.
Useful string methods
Because strings are immutable, methods like Trim and ToUpper don't change the original — they return a new string that you need to capture (or chain straight onto the next call):
string title = " the great gatsby ";
Console.WriteLine(title.Trim());
Console.WriteLine(title.Trim().ToUpper());
Console.WriteLine(title.Length);
the great gatsby THE GREAT GATSBY 20
Note that title.Length is 20, counting the original leading and trailing spaces — Trim() never touched title itself, it only affected the new string returned to Console.WriteLine on the earlier lines.
Searching, slicing, replacing
string email = "maria@example.com"; Console.WriteLine(email.Contains("@")); Console.WriteLine(email.Substring(0, 5)); Console.WriteLine(email.Replace("example", "mystore"));
True maria maria@mystore.com
Substring(0, 5) means "starting at index 0, take 5 characters." Replace swaps every occurrence of the first argument with the second, returning the new full string — again, email itself is untouched after this runs.
StringBuilder exists and is worth reaching for once you're past simple concatenation.