简体   繁体   English

我有一个 C# 开关,我需要在每个案例返回之前调用一个方法。 有什么办法可以简化这个吗?

[英]I have a C# switch and I need to call a method before each case returns. Is there any way I could simplify this?

I have this code which I refactored to use a switch but it still looks very clumsy.我有这段代码,我将其重构为使用开关,但它看起来仍然很笨拙。 Now I am looking for a way to simplify it.现在我正在寻找一种简化它的方法。

    public async Task CheckAvailability()
    {
        switch (Settings.Mode)
        {
            case Enums.MO.Learn:
                if (await IsLearn())
                {
                    await ShowFirstMessageAsync();
                    return;
                }
                break;
            case Enums.MO.Practice:
                if (await IsPractice())
                {
                    await ShowFirstMessageAsync();
                    return;
                }
                break;
            case Enums.MO.Quiz:
                if (await IsQuiz())
                {
                    await ShowFirstMessageAsync();
                    return;
                }
                break;
            default:
                break;
        }

        await PickCard();
    }

Is there anyone that can think of a simpler way to implement this without the need for multiple calls to await ShowFirstMessageAsync?有没有人可以想出一种更简单的方法来实现这一点,而无需多次调用 await ShowFirstMessageAsync?

This is a good opportunity to use switch expressions in C# 8:这是在 C# 8 中使用switch表达式的好机会:

var shouldShowFirstMessage = Settings.Mode switch
{
    Enums.MO.Learn => await IsLearn(),
    Enums.MO.Practice => await IsPractice(),
    Enums.MO.Quiz => await IsQuiz(),
    _ => false
}
if (shouldShowFirstMessage) {
    await ShowFirstMessageAsync();
} else {
    await PickCard();
}

Maybe this:也许是这样:

public async Task CheckAvailability()
{
    bool showFirstMessage = false;
    switch (Settings.Mode)
    {
        case Enums.MO.Learn:
            showFirstMessage = await IsLearn();
            break;
        case Enums.MO.Practice:
            showFirstMessage = await IsPractice();
            break;
        case Enums.MO.Quiz:
            showFirstMessage = await IsQuiz();
            break;
        default:
            break;
    }

    if (showFirstMessage)
    {
        await ShowFirstMessageAsync();
        return;
    }

    await PickCard();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 c# switch 关键字的使用方式与我以前见过的不同 - c# switch keyword used in a different way than I have seen before C#:在不锁定UI线程的情况下,如何让方法等待2秒才能返回? - C#: How can I have a method wait 2 seconds before it returns, without locking up the UI thread? 有什么办法可以简化吗? - Is there any way I can simplify this? 有什么办法可以使用更多的 generics 来简化此代码? - Is there any way I could simplify this code using more generics? 有没有一种方法可以简化(使眼睛更容易)使用开关表达式和 && 的值设置? - Is there a way I could simplify (make it easier on the eyes) the setting of a value using a switch expression and &&? 我有 3 个不同的类,具有相同的方法名称。 需要在使用前检查并应用它们的正确实例c# - I have 3 different classes, with the same method names. Need to check before usage and apply the right instance of them c# 如何使用C#或Java查找每个Switch Case Item的值? - How do I find the value of each Switch Case Item in C# or Java? 在C#中,我如何评估一个类的任何成员是否包含switch case或if else构造? - In C# how can I evaluate if any members of a class contain switch case or if else constructs? 我需要在C#程序的VB文件中使用一种方法,该如何调用该方法? - I need to use a method in a VB file in my C# program, how can I call this method? 当我在 C# 中继承一个方法并调用继承的方法时。 我需要使用 base 还是 this - When I inherit a method in C# and call the inherited method. Do I need to use base or this
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM