28

I fail to find documentation addressing this issue. (perhaps I am just bad at using google...) My guess is that the answer is negative, however I didn't understand where this is addressed in the documentation. To be precise my question is the following.

Suppose, I want to execute something like this:

DirectoryInfo someDir = new DirectoryInfo(@".\someDir");
Console.WriteLine($"Would you like to delete the directory {someDir.FullName}?");
string response = Console.ReadLine().ToLower();

response switch
{
    "yes" => { someDir.Delete(); ... MoreActions},
     _ => DoNothing()
};

I understand that I can achieve the desired behavior by using the regular switch or if/else, however I was curious whether it is possible to use switch expression in this case.

1

3 Answers 3

34

however I didn't understand where this is addressed in the documentation

This is stated pretty clear here:

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.

{ someDir.Delete(); ... MoreActions} is not an expression.

However, you can abuse every feature, as they say :)

You can make the switch expression evaluate to an Action, and invoke that action:

Action a = response switch
{
    "yes" => () => { ... },
     _ => () => { .... }
};
a();

You can even reduce this to a single statement:

(response switch
{
    "yes" => (Action)(() => { ... }),
     _ => () => { ... }
})();

But just don't do this...

Sign up to request clarification or add additional context in comments.

7 Comments

Why "but just don't do this..."? It looks still better than old switch or many if statements.
@ca9163d9 do you actually think this looks better, or is it just because it’s a new feature and it’s shiny? I don’t like this because 1. The switch expression is designed for expressions, not blocks of statements. 2. You need the word Action somewhere in there, which can be considered noise and is unsightly. Note that I’m not saying that you shouldn’t use the switch expression at all (just in case you have misunderstood). There are situations where the switch statement is suitable, and situations where the switch expression is suitable.
The switch statement is bulky when there is only one statement for each case. Every case will have at least three lines, case:, the statement, and the easy to be forgotten break;. In the simple case, I would like to use switch expression unless there is any performance issue.
@ca9163d9 well, how do you know that each case will always only have one statement? There could be a change in requirements that requires you to add another line in one of the cases. Plus, you can write the case label, the statement, and break; all on the same line if you prefer. Conversely, you can also write each case of the switch expression across multiple lines. Number of lines doesn’t quite matter here. As for the performance, I haven’t done any benchmarks so I don’t know. My guess is that the expression will be slower but it likely isn’t significant.
Its better because the the cases are patterns vs a switch statement is not. So probably "just don't do this" isn't a great suggestion, since its perfectly reasonable and a good idea to do it that way. Though frankly they should just add support for statement blocks since it will still be an expression.
|
12

You can also introduce local function (C# 7.0 and above) and do something like:

response switch
{
    "yes" => DoSomething(),
     _ => DoNothing()
};

void DoSomething()
{
    someDir.Delete();
    ... MoreActions
}

Comments

7

As per documentation: The bodies are expressions, not statements.

You can do something like this though:

Action fn = response switch
{
    "yes" => () => { BlockTest(); },
     _ => () => { OldTest(); }
};

6 Comments

@Sweeper right, corrected. any reasoning behind your "but just don't do this"?
Because the switch expression is not designed to be used like this?
It works just fine - don't see the problem.
@lightw8 think of it in terms of the "recent" javascript craze: just because you can doesn't necessarily mean you should.
@ShaiCohen yeah...but besides the extra bloat, it's more functional/declarative and some might like that.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.