Setting a Variable to a Switch's Result

2020-08-09 08:28发布

Am I crazy or is there a way, in C#, to set a variable to a switch's result? Something like:

 var a = switch(b)
    {
     case c:
     d;
     case e:
     f;
     default:
     g;
    };

Is it possible in any other language? I just thought it was, but I'm not getting anything to compile. Thanks in advance.

8条回答
劳资没心,怎么记你
2楼-- · 2020-08-09 08:57

If you weren't especially concerned about efficiency, it would be pretty easy to cook up a little class using generics that allowed you to use a "fluent" chain of method calls such as this:

var a=Switch(b).Case(c).Then(d).Case(e).Then(f).Default(g);

or, if you prefer, the more compact

var a=Switch(b).Case(c, d).Case(e, f).Default(g);

If this interests you, I'd be happy to cook up an example

查看更多
【Aperson】
3楼-- · 2020-08-09 08:57

I wanted the same thing because I use Ruby quite a lot, and that allows you to do that kind of thing. So I created a nuget package called FluentSwitch.

查看更多
可以哭但决不认输i
4楼-- · 2020-08-09 09:04

You can do it since c# 8.0, it is called "Switch expressions":

const int c = 1;
int d = 2;
const int e = 3;
int f = 4;
int g = 5;

int b = 3;

var a = b switch
{
    c => d,
    e => f,
    _ => g,
};

Console.WriteLine(a);

a will contain value 4

c and e should be constants as per switch syntax, _ corresponds to the default switch branch

查看更多
Luminary・发光体
5楼-- · 2020-08-09 09:05

switch in C# is a statement that doesn't have a "return" value. If you're interested in having a "switch" expression, you can find the little fluent class that I wrote here in order to have code like this:

a = Switch.On(b)
  .Case(c).Then(d)
  .Case(e).Then(f)
  .Default(g);

You could go one step further and generalize this even more with function parameters to the Then methods.

查看更多
甜甜的少女心
6楼-- · 2020-08-09 09:06

This is not possible in C#.

The closest would be to either move this into a method, or do the assignment in each case individual, ie:

int a;
switch(b)
{
 case c:
     a = d; break;
 case e:
     a = f; break;
 default:
     a = g; break;
};

Is it possible in any other language?

Yes. For example, most functional languages support something similar. For example, F#'s pattern matching provides a (much more powerful) version of this.

查看更多
▲ chillily
7楼-- · 2020-08-09 09:11

From C# 8 onwards:

Yes, switch expressions were introduced in C# 8. In terms of syntax, the example would be:

var a = b switch
{
    c => d,
    e => f,
    _ => g
}

... where c and e would have to be valid patterns to match against b.

Before C# 8:

No, switch is a statement rather than an expression which can be evaluated.

Of course, you can extract it into another method:

int x = DoSwitch(y);

...

private int DoSwitch(int y)
{
    switch (y)
    {
        case 0: return 10;
        case 1: return 20;
        default: return 5;
    }
}

Alternatively, you could use a Dictionary if it's just a case of simple, constant mappings. If you can give us more information about what you're trying to achieve, we can probably help you work out the most idiomatic way of getting there.

查看更多
登录 后发表回答