在我脑后的某个地方,一个小小的声音告诉我“下面的C#代码闻起来”.
private const string STR_ConnectionString = "ConnectionString"; private readonly string upperCaseConnectionString = STR_ConnectionString.ToUpperInvariant(); // a lot further on string keyAttributeValue = keyAttribute.Value; if (keyAttributeValue.ToUpperInvariant().StartsWith(upperCaseConnectionString)) { // some C# code handling a key that starts with "ConnectionString" }
常量STR_ConnectionString也用于代码中的其他位置.
如何摆脱气味?
解决方法
您可以使用
overloaded StartsWith method获取StringComparison枚举值:
keyAttributeValue.StartsWith(STR_ConnectionString,StringComparison.OrdinalIgnoreCase) // or use StringComparison.InvariantCultureIgnoreCase here