gpt4 book ai didi

C#:比组合 StartsWith 和两个 ToUpperInvariant 调用更好的方法

转载 作者:太空狗 更新时间:2023-10-29 20:59:25 26 4
gpt4 key购买 nike

在我脑后的某个地方,一个微小的声音告诉我“下面的 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

关于C#:比组合 StartsWith 和两个 ToUpperInvariant 调用更好的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8789393/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com