Practice mode C#

What is the output of: string s = “abc”; s.ToUpper(); Console.WriteLine(s);?

Correct answer C. abc in small letters

Explanation

Strings in C# are immutable: no method changes an existing string object. ToUpper returns a new string with the converted text, but the code ignores that return value, so s still refers to "abc". To keep the result you must assign it, as in s = s.ToUpper();. This immutability is also why repeated concatenation in a loop is better done with StringBuilder.

Report a problem

Answer checked How we write and check questions

Tip: press A, B, C or D to answer, and N for the next question.

Advertisement

Have a question about this answer?

Your email address will not be published. Required fields are marked *

Links and website addresses are not allowed in comments.