Combine return and switch

后端 未结 12 2062
长情又很酷
长情又很酷 2021-02-03 22:15

How can I combine return and switch case statements?

I want something like

return switch(a)
       {
          case 1:\"lalala         


        
相关标签:
12条回答
  • 2021-02-03 22:38

    This is the closest I can think of:

    return    a==1 ? "lalala"
            : a==2 ? "blalbla"
            : a==3 ? "lolollo"
            : "default";
    
    0 讨论(0)
  • 2021-02-03 22:38

    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);
    
    0 讨论(0)
  • 2021-02-03 22:39

    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.

    0 讨论(0)
  • 2021-02-03 22:39

    My mapping solution looks like Jordão's solution but it is more flexible and shorter.

    return a
      .Map(1,"lalala")
      .Map(2,"blabla")
      .Map(3,"lololo")
      .Else(string.Empty);
    

    Both arguments can also be a function:

    return a
        .Map(x => x <= 0, "lalala")
        .Map(2, "blabla")
        .Map(x => x >= 3, x => "lololo" + x.ToString()); // lololo3 etc.
    
    0 讨论(0)
  • 2021-02-03 22:47
    switch(a)
    {
        case 1: return "lalala";
        case 2: return "blalbla";
        case 3: return "lolollo";
        default: return "default";
    }
    
    0 讨论(0)
  • 2021-02-03 22:49

    I've created a Nuget package (FluentSwitch) that should do what you want. So you can do the following:

    var result = myValue.Switch()
        .When(1, "lalala")
        .When(2, "blalbla")
        .When(3, "lolollo")
        .Else("default")
        .Value();
    
    0 讨论(0)
提交回复
热议问题