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

后端 未结 5 1130
温柔的废话
温柔的废话 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 05:45

    It's costly because a new string is "manufactured".

    Compare that to calling, say, Equals with an overload that asks for a case-insensitive comparison. This allows the comparison to terminate, without having to create a new string, as soon as a mismatch is identified.

提交回复
热议问题