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

C#NullSwitch StatementNullable

C# Problem Overview


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

Will the default label catch nulls and any unhandled cases?

C# Solutions


Solution 1 - C#

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.

Unless null is handled.

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.

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.

Solution 2 - C#

You can use the null-coalescing operator ?? to route null switch values to a specific case label other than 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;
        }
    }
}

Solution 3 - C#

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 underscore:

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTrey MackView Question on Stackoverflow
Solution 1 - C#Trey MackView Answer on Stackoverflow
Solution 2 - C#Glenn SlaydenView Answer on Stackoverflow
Solution 3 - C#Przemek StrucińskiView Answer on Stackoverflow