Using InvariantCultureIgnoreCase instead of ToUpper for case-insensitive string comparisons

前端 未结 2 716
無奈伤痛
無奈伤痛 2020-12-15 08:04

On this page, a commenter writes:

Do NOT ever use .ToUpper to insure comparing strings is case-insensitive.

Instead of this:

2条回答
  •  暖寄归人
    2020-12-15 08:11

    In short, it's optimized by the CLR (less memory as well).

    Further, uppercase comparison is more optimized than ToLower(), if that tiny degree of performance matters.

    In response to your example there is a faster way yet:

    String.Equals(type.Name, controllerName + "Controller", 
                  StringComparison.InvariantCultureIgnoreCase);
    

提交回复
热议问题