C# switch 语句的默认标签如何处理可为空的枚举?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14950532/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 13:31:12  来源:igfitidea点击:

How will a C# switch statement's default label handle a nullable enum?

c#nullswitch-statementnullable

提问by Trey Mack

How will a C# switch statement's default label handle a nullable enum?

C# switch 语句的默认标签如何处理可为空的枚举?

Will the default label catch nulls and any unhandled cases?

默认标签会捕获空值和任何未处理的情况吗?

采纳答案by Trey Mack

If it's null, it will hit the default label.

如果它为空,它将命中默认标签。

public enum YesNo
{
    Yes,
    No,
}

public class Program
{
    public static void Main(string[] args)
    {
        YesNo? value = null;
        switch (value)
        {
            case YesNo.Yes:
                Console.WriteLine("Yes");
                break;
            case YesNo.No:
                Console.WriteLine("No");
                break;
            default:
                Console.WriteLine("default");
                break;
        }
    }
}

The program will print default.

该程序将打印default.

Unless null is handled.

除非处理 null。

public class Program
{
    public static void Main(string[] args)
    {
        YesNo? value = null;
        switch (value)
        {
            case YesNo.Yes:
                Console.WriteLine("Yes");
                break;
            case YesNo.No:
                Console.WriteLine("No");
                break;
            case null:
                Console.WriteLine("NULL");
                break;
            default:
                Console.WriteLine("default");
                break;
        }
    }
}

prints NULL.

打印NULL

If you have an unhandled enum value that was added later:

如果您有稍后添加的未处理的枚举值:

public enum YesNo
{
    Yes,
    No,
    FileNotFound,
}

public class Program
{
    public static void Main(string[] args)
    {
        YesNo? value = YesNo.FileNotFound;
        switch (value)
        {
            case YesNo.Yes:
                Console.WriteLine("Yes");
                break;
            case YesNo.No:
                Console.WriteLine("No");
                break;
            default:
                Console.WriteLine("default");
                break;
        }
    }
}

It still prints default.

它仍然打印default.

回答by Glenn Slayden

You can use the null-coalescing operator ??to route nullswitch values to a specific case label other than default:

您可以使用空合并运算符??null开关值路由到特定案例标签,而不是default

public static IEnumerable<String> AsStrings(this IEnumerable<Char[]> src)
{
    Char[] rgch;

    var e = src.GetEnumerator();
    while (e.MoveNext())
    {
        switch ((rgch = e.Current)?.Length ?? -1)
        {
            case -1:    // <-- value when e.Current is 'null'
                yield return null;
                break;
            case 0:
                yield return String.Empty;
                break;
            case 1:
                yield return String.Intern(new String(rgch[0], 1));
                break;
            default:   // 2...n
                yield return new String(rgch);
                break;
        }
    }
}

回答by Przemek Struciński

It's worth to mention that C# 8.0 introduced a new Property Pattern for a switch expression. Now you can implement default logic to switch by using floor:

值得一提的是,C# 8.0 为 switch 表达式引入了一个新的属性模式。现在您可以使用 floor 实现默认逻辑来切换:

public double Calculate(int left, int right, Operator op) =>
    op switch 
{
    Operator.PLUS => left + right,
    Operator.MINUS => left - right,
    Operator.MULTIPLY => left * right,
    Operator.DIVIDE => left / right,
    _    =>  0 // default
}

Ref. https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-8

参考 https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-8