Combine return and switch

Viewed 112810

How can I combine return and switch case statements?

I want something like

return switch(a)
       {
          case 1:"lalala"
          case 2:"blalbla"
          case 3:"lolollo"
          default:"default" 
       };

I know about this solution

switch(a)
{
    case 1: return "lalala";
    case 2: return "blalbla";
    case 3: return "lolollo";
    default: return "default";
}

But I want to only use the return operator.

14 Answers

Actually this is possible using switch expressions starting with C# 8.

return a switch
    {
        1 => "lalala",
        2 => "blalbla",
        3 => "lolollo",
        _ => "default"
    };

Switch Expressions

There are several syntax improvements here:

  • The variable comes before the switch keyword. The different order makes it visually easy to distinguish the switch expression from the switch statement.
  • The case and : elements are replaced with =>. It's more concise and intuitive.
  • The default case is replaced with a _ discard.
  • The bodies are expressions, not statements.

For more information and examples check the Microsoft's C# 8 Whats New.

With the new C# 8, you can combine both return and switch. The new switch is so cute.

public static RGBColor FromRainbow(Rainbow colorBand) =>
    colorBand switch
    {
        Rainbow.Red    => new RGBColor(0xFF, 0x00, 0x00),
        Rainbow.Orange => new RGBColor(0xFF, 0x7F, 0x00),
        Rainbow.Yellow => new RGBColor(0xFF, 0xFF, 0x00),
        Rainbow.Green  => new RGBColor(0x00, 0xFF, 0x00),
        Rainbow.Blue   => new RGBColor(0x00, 0x00, 0xFF),
        Rainbow.Indigo => new RGBColor(0x4B, 0x00, 0x82),
        Rainbow.Violet => new RGBColor(0x94, 0x00, 0xD3),
        _              => throw new ArgumentException(message: "invalid enum value", paramName: nameof(colorBand))
    };

The equivalent old switch is as below.

public static RGBColor FromRainbowClassic(Rainbow colorBand)
{
    switch (colorBand)
    {
        case Rainbow.Red:
            return new RGBColor(0xFF, 0x00, 0x00);
        case Rainbow.Orange:
            return new RGBColor(0xFF, 0x7F, 0x00);
        case Rainbow.Yellow:
            return new RGBColor(0xFF, 0xFF, 0x00);
        case Rainbow.Green:
            return new RGBColor(0x00, 0xFF, 0x00);
        case Rainbow.Blue:
            return new RGBColor(0x00, 0x00, 0xFF);
        case Rainbow.Indigo:
            return new RGBColor(0x4B, 0x00, 0x82);
        case Rainbow.Violet:
            return new RGBColor(0x94, 0x00, 0xD3);
        default:
            throw new ArgumentException(message: "invalid enum value", paramName: nameof(colorBand));
    };
}

You can read about this feature here.

If you want switch to return value, you can use delegate:

int a = 2;
string result = new Func<string>(delegate ()
{
    switch (a)
    {
        case 1: return "lalala";
        case 2: return "blalbla";
        case 3: return "lolollo";
        default: return "default";
    }
})();

Or:

int a = 2;
string result = new Func<int,string>(delegate (int i)
{
    switch (i)
    {
        case 1: return "lalala";
        case 2: return "blalbla";
        case 3: return "lolollo";
        default: return "default";
    }
})(a);

Or just use lambda:

int a = 2;
string result = new Func<int,string>((int i) =>
{
    switch (i)
    {
        case 1: return "lalala";
        case 2: return "blalbla";
        case 3: return "lolollo";
        default: return "default";
    }
})(a);

As an extension to others' responses, i recommend using tuples together with return switch when a few parameters are involved in decision making. This combination with discard is pretty strong:

return (param1, param2, param3) switch
{
    (value1, value2, value3) => returnValue1,
    (value2, _, value3) => returnValue2
    (value3, _, _) => returnValue3

}

Another sample by Microsoft: https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-8#tuple-patterns

You can use (switch case) instead of (if statement).

    public InvoiceDeliveryStatus InvoiceDeliveryStatus { get; set; }

public string GetInvoiceDeliveryStatusTxt { get { return InvoiceDeliveryStatusSwitch(); } }


        private string InvoiceDeliveryStatusSwitch()
        {
           
            if (InvoiceDeliveryStatus == InvoiceDeliveryStatus.Canceled) return "Is Rejected";
            if (InvoiceDeliveryStatus == InvoiceDeliveryStatus.Completed) return "Is Completed";
       

                return InvoiceDeliveryStatus.ToString();
        }
Related