c# – string ToUpper() function with ToString()

c# – string ToUpper() function with ToString()

No, youre calling ToString on the object returned by ToUpper. This is pointless, but its not a compilation error. If you did:


that will indeed give you an error, since you cant call a method (ToString) on a method group.

ToUpper() is a function that takes a string and returns another string, so youre OK just doing:

string upperString = txtCheck.Text.ToUpper();

No need to call ToString() at all.

c# – string ToUpper() function with ToString()

Think of it as:

string upperString = (lowerString   .ToUpper())   .ToString();

In other words, the thing that get returned from lowerString.ToUpper() is having ToString() applied to it. Thats redundant since its already a string but its by no means an error.

Its no different to some other languages where the equivalent would be:

upperString = toString (toUpper (lowerString));

In fact you can do all sorts of weird things like:

string upper = lower.ToUpper().ToLower().ToUpper().ToString().ToString();

although that monstrosity should never get past a code review 🙂

Leave a Reply

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