gpt4 book ai didi

c# - 工厂模式 C# 中缺少 return 语句

转载 作者:行者123 更新时间:2023-12-02 07:37:56 27 4
gpt4 key购买 nike

我的代码有以下代码,但它总是说缺少 return 语句,即使我已将它们放入 switch 列表中。

public IMap Map(string oldtheme) 
{
switch (oldtheme)
{
case "archer": return new Archer();
case "craftyblue": return new CraftyBlue();
case "minimal": return new Minimal();
case "mintalicious": return new Mintalicious();
case "misfit": return new Misfit();
case "peach": return new Peach();
case "queen": return new Queen();
case "sketch": return new Sketch();
case "takeaway": return new TakeAwayLemonFresh();
case "lemonfresh": return new TakeAwayLemonFresh();
case "vanilla": return new Vanilla();
case "velvet": return new Velvet();
case "victoriana": return new Victoriana();
case "writer": return new Writer();
}
}

最佳答案

您需要处理 oldtheme 不是您检查的值的情况。

根据您的情况,我建议抛出 ArgumentException这样您就知道它何时发生。我添加了default case 到您的 switch 语句:

public IMap Map(string oldtheme)
{
switch ( oldtheme )
{
case "archer": return new Archer();
case "craftyblue": return new CraftyBlue();
case "minimal": return new Minimal();
case "mintalicious": return new Mintalicious();
case "misfit": return new Misfit();
case "peach": return new Peach();
case "queen": return new Queen();
case "sketch": return new Sketch();
case "takeaway": return new TakeAwayLemonFresh();
case "lemonfresh": return new TakeAwayLemonFresh();
case "vanilla": return new Vanilla();
case "velvet": return new Velvet();
case "victoriana": return new Victoriana();
case "writer": return new Writer();
default: throw new ArgumentException("unexpected value of oldtheme");
}
}

关于c# - 工厂模式 C# 中缺少 return 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29389081/

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