Strings are immutable
A C# string is a sequence of char values, and once created it can never change. Every method that looks like it modifies a string — ToUpper, Replace, Trim — actually returns a new string and leaves the original alone. If you call one and throw away the result, nothing happens. Immutability is what makes strings safe to share between variables, threads and dictionary keys: no one can change the text under you.
string name = " ada lovelace ";
name.Trim(); // result thrown away!
Console.WriteLine($"[{name}]");
name = name.Trim(); // keep the new string
Console.WriteLine($"[{name}]");
string upper = name.ToUpperInvariant();
Console.WriteLine($"{name} / {upper}");
string a = "hello";
string b = a; // both refer to the same text
a += " world"; // a now refers to a NEW string
Console.WriteLine($"a={a}, b={b}");
Console.WriteLine(name[0]); // reading a char is fine
Console.WriteLine("abc" == "ab" + "c"); // == compares the text[ ada lovelace ]
[ada lovelace]
ada lovelace / ADA LOVELACE
a=hello world, b=hello
a
TrueCapitalise just the first letter of name by combining char.ToUpperInvariant(name[0]) with name[1..].
CS0200: trying to change a character in a string
string word = "hello";
word[0] = 'H';
Console.WriteLine(word);Program.cs(2,1): error CS0200: Property or indexer 'string.this[int]' cannot be assigned to -- it is read onlyThe string indexer only has a getter. Allowing word[0] = 'H' would mutate a string that other variables may share, which immutability forbids.
Build a new string, or copy to a char[], change it, and make a string from it.
string word = "hello";
word = char.ToUpperInvariant(word[0]) + word[1..];
char[] chars = "hello".ToCharArray();
chars[0] = 'J';
Console.WriteLine(word + " " + new string(chars));string is the C# keyword alias for the .NET type System.String, just as int is System.Int32. They are identical. Convention: use string for variables and string.Join/string.IsNullOrEmpty for the static methods.