Shortcut to Make Case/Switch Return a Value

Shortcut to make case/switch return a value

This works:

return case
when guess > @answer ; :high
when guess < @answer ; :low
else ; :correct
end

Combine return and switch

Note: As of C#8 (ten years later!) this is now possible, please see the answer below.


switch and return can't combine that way, because switch is a statement, not an expression (i.e., it doesn't return a value).

If you really want to use just a single return, you could make a Dictionary to map the switch variable to return values:

var map = new Dictionary<int, string>() 
{
{1, "lala"},
{2, "lolo"},
{3, "haha"},
};
string output;
return map.TryGetValue(a, out output) ? output : "default";

How can I easily create a case (switch case) for all enum items?

Set the cursor after the first brace within the switch statement and press Alt + Enter. You will see the option to generate switch labels.

What is wrong with this Ruby case control code?

In Ruby there are two types of case expression, and you seem to be confusing them. The first is basically the same as an if...elsif...end expression, and looks like this:

case
when boolean_expression
# code executed if boolean_expression is truthy
when other_expression
# code executed if other_expression is truthy
# other whens ...
end

Note how there isn’t anything after case before the first when. In this type of case statement the code associated with the first expression that evaluates to true will be executed, non of the rest will.

The other kind of case statement takes an argument and compares that argument with each of the when clauses:

case obj
when type1
# some code if type1 === obj
when other_type
# some code if other_type === obj
# other whens ...
end

Here there is an argument to case (obj in this example). This is compared to the argument to each when clause in turn using ===, and the associated code is run for the first one where that comparison is true.

Note that this type of case can be written like the first type:

case
when type1 === obj
# etc...

In your case you start with case obj, which indicates the second type. You then use obj in each when clause like you would in the first kind.

(The actual error you are getting is actually just a syntax error caused by a bit of an ambiguity in the Ruby syntax, but even if you fixed that your code wouldn’t work as you’d expect.)

Module overrides === to check if the arg is an instance, so for your use you can just do this:

result = case obj
when Hacker then "It's a Hacker!"
when Submission then "It's a Submission!"
when TestCase then "It's a TestCase!"
when Contest then "It's a Contest!"
end

C# 8 switch expression with multiple cases with same result

I got around to installing it, but I have not found a way to specify multiple, separate case labels for a single switch section with the new syntax.

However, you can create a new variable that captures the value and then use a condition to represent the cases that should have the same result:

var resultText = switchValue switch
{
var x when
x == 1 ||
x == 2 ||
x == 3 => "one to three",
4 => "four",
5 => "five",
_ => "unknown",
};

This is actually more concise if you have many cases to test, because you can test a range of values in one line:

var resultText = switchValue switch
{
var x when x > 0 && x < 4 => "one to three",
4 => "four",
5 => "five",
_ => "unknown",
};

Inline switch / case statement in C#

If you want to condense things you could just put things on one line (let's imagine that "do one process is a call to Console.WriteLine):

switch (FIZZBUZZ)
{
case "Fizz": Console.WriteLine("Fizz"); break;
case "Buzz": Console.WriteLine("Buzz"); break;
case "FizzBuzz": Console.WriteLine("FizzBuzz"); break;
}

If you want to get fancy you could create a map of strings to actions like this:

var map = new Dictionary<String, Action>
{
{ "Fizz", () => Console.WriteLine("Fizz") },
{ "Buzz", () => Console.WriteLine("Fizz") },
{ "FizzBuzz", () => Console.WriteLine("FizzBuzz") }
};

And then you could invoke the method like this:

map[FIZZBUZZ].Invoke(); // or this: map[FIZZBUZZ]();

Switch statement equivalent in Windows batch file

I ended up using label names containing the values for the case expressions as suggested by AjV Jsy. Anyway, I use CALL instead of GOTO to jump into the correct case block and GOTO :EOF to jump back. The following sample code is a complete batch script illustrating the idea.

@ECHO OFF

SET /P COLOR="Choose a background color (type red, blue or black): "

2>NUL CALL :CASE_%COLOR% # jump to :CASE_red, :CASE_blue, etc.
IF ERRORLEVEL 1 CALL :DEFAULT_CASE # If label doesn't exist

ECHO Done.
EXIT /B

:CASE_red
COLOR CF
GOTO END_CASE
:CASE_blue
COLOR 9F
GOTO END_CASE
:CASE_black
COLOR 0F
GOTO END_CASE
:DEFAULT_CASE
ECHO Unknown color "%COLOR%"
GOTO END_CASE
:END_CASE
VER > NUL # reset ERRORLEVEL
GOTO :EOF # return from CALL


Related Topics



Leave a reply



Submit