Why avoid string.ToLower() when doing case-insensitive string comparisons?

后端 未结 5 1164
温柔的废话
温柔的废话 2021-01-19 05:38

I have read that when in your application you do a lot of string comparison and using ToLower method, this method is quite costly. I was wondering of anyone could explain to

5条回答
  •  無奈伤痛
    2021-01-19 06:01

    There is another advantage to using the String.Compare(String, String, StringComparison) method, besides those mentioned in the other answers:

    You can pass null values and still get a relative comparison value. That makes it a whole lot easier to write your string comparisons.

    String.Compare(null, "some StrinG", StringComparison.InvariantCultureIgnoreCase);
    

    From the documentation:

    One or both comparands can be null. By definition, any string, including the empty string (""), compares greater than a null reference; and two null references compare equal to each other.

提交回复
热议问题