gpt4 book ai didi

c# - 简化 if if if 以减少代码大小和可读性

转载 作者:行者123 更新时间:2023-11-30 13:26:04 25 4
gpt4 key购买 nike

我需要一些帮助来简化这个包含数百行的庞大代码,但我真的不知道该怎么做。代码看起来真的很乱,我需要的是返回具有预定义文本颜色的模型。有什么简单的方法吗?

我必须多解释一点:- 有一个包含许多型号的手机列表,如果有任何型号可用,我需要打印出型号和颜色。该列表每小时/每天都在变化……这就是它的工作原理。所以我需要用所有模型填写我的代码,然后搜索可用的模型。此外,所有型号都以特定代码“结尾”(例如 A23X),我认为这样搜索速度更快。

if (text.EndsWith("model-1")) model.Add(new Line { text = "Sony, Smartphone(model-1)", color = () => Sony.Color1 });
if (text.EndsWith("model-2")) model.Add(new Line { text = "Sony, Smartphone(model-2)", color = () => Sony.Color2 });
if (text.EndsWith("model-3")) model.Add(new Line { text = "Sony, Smartphone(model-3)", color = () => Sony.Color3 });
if (text.EndsWith("model-4")) model.Add(new Line { text = "Sony, Smartphone(model-4)", color = () => Sony.Color4 });
if (text.EndsWith("model-5")) model.Add(new Line { text = "Sony, Smartphone(model-5)", color = () => Sony.Color5 });
if (text.EndsWith("model-6")) model.Add(new Line { text = "Sony, Smartphone(model-6)", color = () => Sony.Color6 });
if (text.EndsWith("model-7")) model.Add(new Line { text = "Sony, Smartphone(model-7)", color = () => Sony.Color7 });
if (text.EndsWith("model-8")) model.Add(new Line { text = "Sony, Smartphone(model-8)", color = () => Sony.Color8 });
if (text.EndsWith("model-9")) model.Add(new Line { text = "Sony, Smartphone(model-9)", color = () => Sony.Color9 });

if (text.EndsWith("model-10")) model.Add(new Line { text = "Nokia, Smartphone(model-10)", color = () => Nokia.Color10 });
if (text.EndsWith("model-11")) model.Add(new Line { text = "Nokia, Smartphone(model-11)", color = () => Nokia.Color11 });
if (text.EndsWith("model-12")) model.Add(new Line { text = "Nokia, Smartphone(model-12)", color = () => Nokia.Color12 });
if (text.EndsWith("model-13")) model.Add(new Line { text = "Nokia, Smartphone(model-13)", color = () => Nokia.Color13 });
if (text.EndsWith("model-14")) model.Add(new Line { text = "Nokia, Smartphone(model-14)", color = () => Nokia.Color14 });
if (text.EndsWith("model-15")) model.Add(new Line { text = "Nokia, Smartphone(model-15)", color = () => Nokia.Color15 });
if (text.EndsWith("model-16")) model.Add(new Line { text = "Nokia, Smartphone(model-16)", color = () => Nokia.Color16 });
if (text.EndsWith("model-17")) model.Add(new Line { text = "Nokia, Smartphone(model-17)", color = () => Nokia.Color17 });
if (text.EndsWith("model-18")) model.Add(new Line { text = "Nokia, Smartphone(model-18)", color = () => Nokia.Color18 });

最佳答案

您可以创建这样一个字典来从模型名称中查找 Line 实例:

Dictionary<string, Line> ModelNames = new Dictionary<string, Line>
{
{"model-1", new Line { text = "Sony, Smartphone(model-1)", color = Sony.Color1 }},
{"model-2", new Line { text = "Sony, Smartphone(model-2)", color = Sony.Color2 }},
{"model-3", new Line { text = "Sony, Smartphone(model-3)", color = Sony.Color3 }},
// ...
{"model-10", new Line { text = "Nokia, Smartphone(model-10)", color = Nokia.Color10 }},
{"model-11", new Line { text = "Nokia, Smartphone(model-11)", color = Nokia.Color11 }},
{"model-12", new Line { text = "Nokia, Smartphone(model-12)", color = Nokia.Color12 }},
// ...
};

现在您可以查看是否有已知模型:

Line matchingModelNameLine = ModelNames
.Where(kv => text.EndsWith(kv.Key, StringComparison.InvariantCultureIgnoreCase))
.Select(kv => kv.Value)
.FirstOrDefault();
if (matchingModelNameLine != null)
{
model.Add(matchingModelNameLine);
}

关于c# - 简化 if if if 以减少代码大小和可读性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33496441/

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