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:

</> Program.cs
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);
Output
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):

</> Program.cs
string title = "  the great gatsby  ";

Console.WriteLine(title.Trim());
Console.WriteLine(title.Trim().ToUpper());
Console.WriteLine(title.Length);
Output
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

</> Program.cs
string email = "maria@example.com";

Console.WriteLine(email.Contains("@"));
Console.WriteLine(email.Substring(0, 5));
Console.WriteLine(email.Replace("example", "mystore"));
Output
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.

Why immutability matters: because a string can't change underneath you, you can safely pass the same string into ten different methods without worrying that one of them quietly altered it for the other nine. The tradeoff is that heavy string building in a loop — appending piece by piece thousands of times — creates a lot of throwaway strings along the way; for that specific case, StringBuilder exists and is worth reaching for once you're past simple concatenation.