Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens if my C# switch expression is non-exhaustive?

In C# 8, switch expressions were introduced. What happens if a switch expression is not exhaustive? In other words, what happens if I don't test for every possible value?

static void Main(string[] args)
{
    int x = 1;
    int imExhaustive = x switch
    {
        3 => 4,
        _ => 0   // x = 1 matches this
    };
    int okFine = x switch
    {
        1 => 4   // x = 1 matches this
    };
    int noMatch = x switch
    {
        3 => 4   // No match
    };
}
like image 434
Josh Avatar asked May 09 '19 21:05

Josh


People also ask

What happen if my C drive is full?

In case the C drive memory space is full, then you have to move the unused data to a different drive and uninstall the installed applications which are not used frequently. You can also perform Disk Cleanup to reduce the number of unnecessary files on the drives, which can help the computer run faster.

What happens if I format my local disk C?

Format 'C' to delete everything on your primary hard drive To format C means to format the C drive, or the primary partition that Windows or your other operating system is installed on. When you format C, you erase the operating system and other information on that drive.

What happens if I format my hard drive?

A quick format will simply delete data on a drive, while a normal format will erase everything (wipe the drive). The drive can be your hard drive, or a removable drive such as a USB. A full format will also scan your disk for any bad sectors and remove them, ensuring that you don't end up with corrupted files later on.

Can I delete files from local disk?

Right-click your main hard drive (usually the C: drive) and select Properties. Click the Disk Cleanup button and you'll see a list of items that can be removed, including temporary files and more. For even more options, click Clean up system files. Tick the categories you want to remove, then click OK > Delete Files.


1 Answers

If the switch expression is not exhaustive, the compiler produces a warning. At runtime, if you pass a value that is not handled, a SwitchExpressionException is thrown.

This is documented in the speclet for new patterns feature in C# 8.0: https://github.com/dotnet/csharplang/blob/master/proposals/csharp-8.0/patterns.md#switch-expression

The compiler shall produce a warning if it proves (using those techniques) that some possible input value might not match some switch_expression_arm at runtime.

At runtime, the result of the switch_expression is the value of the expression of the first switch_expression_arm for which the expression on the left-hand-side of the switch_expression matches the switch_expression_arm's pattern, and for which the case_guard of the switch_expression_arm, if present, evaluates to true.

If there is no such switch_expression_arm, the switch_expression throws an instance of the exception System.Runtime.CompilerServices.SwitchExpressionException.

like image 91
Julien Couvreur Avatar answered Oct 01 '22 02:10

Julien Couvreur