Suppose this:
string texto = "Texto a convertir en mayúscula.";
MessageBox.Show("Texto con ToUpper: " + texto.ToUpper() +
"\nTexto con ToUpperInvariant: " + texto.ToUpperInvariant(),
"Mensaje en Mayúscula", MessageBoxButtons.OK, MessageBoxIcon.Information);
The result is the same:
with
ToUpper()
: "TEXT TO BE CONVERTED TO UPPERCASE."with
ToUpperInvariant()
: "TEXT TO BE CONVERTED TO UPPERCASE."
Edited:
According to the description of the
ToUpperInvariant
: Applies the culture case rules of all languages.
What is the difference, any EXAMPLE where the results may be different?
ToUpper()
: Take into account the rules of your system's default culture to determine how to convert a string to uppercase.ToUpperInvariant()
: Uses what might be called culture neutral to determine the rules for converting a string to uppercase.In practice, for the vast majority of letters that we commonly use in English or Spanish, there is really no difference between the 2 methods.
But in theory, if we were working on a system with a very peculiar culture and with more special characters, then you could notice differences.
For example, adapting the example from this SO answer , we can see that if we were working on a site where the default culture is Turkish, then we would see a difference in how the
i
uppercase a is converted:Demo
Result:
So it is usually preferable to use
ToUpperInvariant()
to guarantee stable results, unless we have a special need to take into account the rules of a certain culture.I leave you a link explaining the difference, the only thing is that StackOverFlow is in English, but if you translate it you will understand what it is about:
https://stackoverflow.com/questions/3550213/in-c-sharp-what-is-the-difference-between-toupper-and-toupperinvariant
Hope this can help you.
Cheers