Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between string.ToLower and TextInfo.ToLower

Tags:

c#

.net

What is the difference between the two? And when should I use each of them?

like image 650
Ivan Ivanov Avatar asked May 28 '12 14:05

Ivan Ivanov


Video Answer


2 Answers

There is none.

string.ToLower calls TextInfo.ToLower behind the scenes.

From String.cs:

    // Creates a copy of this string in lower case. 
    public String ToLower() {
        return this.ToLower(CultureInfo.CurrentCulture); 
    }

    // Creates a copy of this string in lower case.  The culture is set by culture.
    public String ToLower(CultureInfo culture) { 
        if (culture==null) {
            throw new ArgumentNullException("culture"); 
        } 
        return culture.TextInfo.ToLower(this);
    } 
like image 98
crdx Avatar answered Oct 24 '22 18:10

crdx


The ToLower and ToLowerInvariant methods on strings actually call into the TextInfo virtual property when invoked. For this reason, they always carry the overhead of this virtual property access. The string type methods have no difference in result values but are slower in some cases.

The full article + Benchmark

For the sake of simplicity use str.ToLower() and forget about the issue!

like image 43
gdoron is supporting Monica Avatar answered Oct 24 '22 19:10

gdoron is supporting Monica