A key characteristic of the string type is that it is immutable. A string variable can be assigned an entirely new value, but there is no facility for modifying the contents of a string. It is not possible, therefore, to convert a string to all uppercase letters. It is trivial to create a new string that is composed of an uppercase version of the old string, but the old string is not modified in the process
//String is immutable
class Uppercase
{
static void Main()
{
string text;
System.Console.Write("Enter text: ");
text = System.Console.ReadLine();
// UNEXPECTED: Does not convert text to uppercase
text.ToUpper();
System.Console.WriteLine(text);
}
}
At a glance, it would appear that text.ToUpper() should convert the characters within text to uppercase. However, strings are immutable and, therefore, text.ToUpper() will make no such modification. Instead, text.ToUpper() returns a new string that needs to be saved into a variable or passed to System.Console.WriteLine() directly
class Uppercase
{
static void Main()
{
string text, uppercase;
System.Console.Write("Enter text: ");
text = System.Console.ReadLine();
// Return a new string in uppercase
uppercase = text.ToUpper();
System.Console.WriteLine(uppercase);
}
}
Please find the code in my Github: https://github.com/pbndru/StringImmutable