gpt4 book ai didi

c# - 使用太多 bool 值

转载 作者:太空宇宙 更新时间:2023-11-03 17:13:47 25 4
gpt4 key购买 nike

有更好的方法吗?

foreach (var line in lines)
{
bool t01 = line.Model.ToLower() == model;
bool t02 = line.Authority.ToLower() != "unknown";
bool t101 = line.Type.ToLower() == "adcn";
bool t102 = line.Type.ToLower() == "adcn/adv";
bool t103 = line.Type.ToLower() == "bn";
bool t104 = line.Type.ToLower() == "book";
bool t105 = line.Type.ToLower() == "cancel";
bool t106 = line.Type.ToLower() == "cir";
bool t107 = line.Type.ToLower() == "coord sht";
bool t108 = line.Type.ToLower() == "cre";
bool t109 = line.Type.ToLower() == "ddr";
bool t110 = line.Type.ToLower() == "dl";

if (t01 && t02)
if ((t101 || t102 || t103 || t104 || t105 || t106 || t107 || t108 || t109 || t110))
Console.WriteLine(line);
}

它实际上上升到 t139。为简洁起见将其剪掉。

最佳答案

听起来你需要一个 HashSet<string>对于类型:

static readonly HashSet<string> ValidTypes = new HashSet<string>
(StringComparer.OrdinalIgnoreCase)
{
"adcn", "adcn/adv", "bn" ...
};

if (line.Model.Equals(model, StringComparison.OrdinalIgnoreCase) &&
!line.Authority.Equals("unknown", StringComparison.OrdinalIgnoreCase) &&
validTypes.Contains(line.Type))
{
Console.WriteLine(line);
}

这也比单独比较每个项目的字符串要快。请注意,尽管我使用了 OrdinalIgnoreCase在上面,可能不是您真正想要的 - 您可能想要 CurrentCultureIgnoreCaseInvariantCultureIgnoreCase .

(请注意,为了执行不区分大小写的比较而使用小写字符串是一个坏主意 - 特别是如果您只是使用默认语言环境来执行此操作。例如,如果您将“MAIL”小写并且您当前的语言环境是土耳其语,您不会收到“邮件”。)

关于c# - 使用太多 bool 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7476408/

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