Questions tagged [switch-expression]

In C# and Java switch expressions are an alternative to the switch statement that returns a value.

C#

In C#, switch expressions support the same features offered by switch statements. This makes them similar to the match operator found in functional languages like and

They were introduced in .

See switch expression

Java

In java, switch expressions are a form of the switch label written with "case L ->" syntax. They are an alternative to switch statements.

They were added as a preview feature in Java 12 under JEP 325.

See JEP 325.

56 questions
223
votes
6 answers

C# 8 switch expression with multiple cases with same result

How can a switch expression be written to support multiple cases returning the same result? With C# prior to version 8, a switch may be written like so: var switchValue = 3; var resultText = string.Empty; switch (switchValue) { case 1: case…
huzle
  • 2,249
  • 2
  • 9
  • 8
110
votes
11 answers

C# how to use enum with switch

I can't figure out how to use switches in combination with an enum. Could you please tell me what I'm doing wrong, and how to fix it? I have to use an enum to make a basic calculator. public enum Operator { PLUS, MINUS, MULTIPLY,…
yesman
  • 7,165
  • 15
  • 52
  • 117
67
votes
15 answers

Combine return and switch

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…
Neir0
  • 12,849
  • 28
  • 83
  • 139
62
votes
13 answers

Multi-variable switch statement in C#

I would like use a switch statement which takes several variables and looks like this: switch (intVal1, strVal2, boolVal3) { case 1, "hello", false: break; case 2, "world", false: break; case 2, "hello", false: etc…
BanditoBunny
  • 3,658
  • 5
  • 32
  • 40
48
votes
3 answers

C# 8 switch expression for void methods

I'm aware of the C# 8 switch expression syntax for methods that return a value or for property matching. But if we just need to switch on a string value and execute a method that returns nothing (no return type/void), then how do we go about it? I…
Shiva Naru
  • 925
  • 3
  • 10
  • 21
34
votes
5 answers

Switch expression with void return type

Is there any way to force an exhaustive check of all enum values when the switch branches call methods with void return type? It's quite ugly to hard-code a yield just to coax the compiler to demand exhaustiveness. This is my current pattern (the…
Arboreal Shark
  • 2,221
  • 3
  • 17
  • 12
29
votes
4 answers

What are switch expressions and how are they different from switch statements?

As part of Java SE 12, switch expressions were introduced and since Java SE 14, they have been standardized. How are they different from switch statements?
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
27
votes
2 answers

Multiple statements in a switch expression: C# 8

Switch expressions were introduced in C# 8. There's plenty of places in codebases, which may be rewritten in this new style. For example, I have some code, which is used for parsing packets from a stream of bytes: switch (command) { case…
JL0PD
  • 3,698
  • 2
  • 15
  • 23
24
votes
3 answers

c# 8 switch expression: No best type was found for the switch expression

I have added a code in my startup class (.net core 3.1) to return the type based on parameter and I get compile-time errors. I have created a running example in sharplab. if switch expression contains the string or other objects it runs…
Kamran Pervaiz
  • 1,861
  • 4
  • 22
  • 42
19
votes
2 answers

Multiple cases in c# 8.0 switch expressions

In traditional C# switch we have a construction where we can aggregate multiple cases. How can it be done in new c# 8.0 switch expressions? Switch statement with multiple cases: switch (value) { case 1: case 2: case 3: …
alekoo73
  • 779
  • 2
  • 10
  • 18
16
votes
4 answers

Using blocks in C# switch expression?

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…
Yuval K
  • 301
  • 2
  • 11
10
votes
1 answer

Why does an incomplete switch expression compile successfully

Trying out JDK/12 EarlyAccess Build 20, where the JEP-325 Switch Expressions has been integrated as a preview feature. A sample code for the expressions (as in the JEP as well): Scanner scanner = new Scanner(System.in); Day day =…
Naman
  • 27,789
  • 26
  • 218
  • 353
9
votes
1 answer

Why does Visual Studio 2019 recommend a switch expression instead of a switch statement?

Visual Studio 2019 recommended converting a switch statement I had written to a switch expression (both included below for context). For a simple example such as this, is there any technical or performance advantage to writing it as an expression?…
AGB
  • 2,378
  • 21
  • 37
9
votes
4 answers

What happens if my C# switch expression is non-exhaustive?

In C# 8, switch expressions were introduced. What happens if a switch expression is not exhaustive? In other words, what happens if I don't test for every possible value? static void Main(string[] args) { int x = 1; int imExhaustive = x…
Josh
  • 2,958
  • 3
  • 16
  • 27
8
votes
1 answer

C#8: Switch ref expressions

I can't figure out how to make a switch expression yield a ref value. bool cond = true; int a = 1, b = 2; // This works ref int c = ref cond ? ref a : ref b; // But using a switch expression fails to compile. // Error CS1525 Invalid expression…
recursive
  • 83,943
  • 34
  • 151
  • 241
1
2 3 4