Not
Åtkomst till den här sidan kräver auktorisering. Du kan prova att logga in eller ändra kataloger.
Åtkomst till den här sidan kräver auktorisering. Du kan prova att ändra kataloger.
TypeName |
AvoidUnnecessaryStringCreation |
CheckId |
CA1807 |
Category |
Microsoft.Performance |
Breaking Change |
NonBreaking |
Cause
An unnecessary string is created through a call to System.String.ToLower or System.String.ToUpper.
Rule Description
A System.String object is immutable. Any change to a string requires creating a new String object. Unnecessary string creation degrades performance. This rule flags the following operations that create unnecessary strings:
Multiple calls to ToLower, ToLowerInvariant, ToUpper, or ToUpperInvariant on the same string instance.
Calling System.String.Equals, System.String.op_Equality(System.String,System.String), or System.String.op_Inequality(System.String,System.String) on a string created by calling ToLower or ToUpper.
Passing a string created by calling ToLower or ToUpper to a member of System.Collections.Specialized.HybridDictionary.
How to Fix Violations
To fix a violation of this rule eliminate the unnecessary string creation by removing the call to ToLower or ToUpper. The following fixes correspond to the numbering in the Description section:
Assign the result of the first ToLower or ToUpper call to a new variable and use this variable instead of the remaining calls.
Replace the call to Equals, op_Equality, or op_Inequality with a case-insensitive call to System.String.Compare.
Use a case-insensitive HybridDictionary, which is created by passing true as the caseInsensitive argument to the constructor.
When to Exclude Warnings
It is safe to exclude a warning from this rule; however, performance might suffer.