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
3
votes
2 answers

C# 8 switch expression: Handle multiple cases at once?

C# 8 introduced pattern matching, and I already found good places to use it, like this one: private static GameType UpdateGameType(GameType gameType) { switch (gameType) { case GameType.RoyalBattleLegacy: case…
Ray
  • 7,940
  • 7
  • 58
  • 90
2
votes
1 answer

Java 17 : switch expressions and statements

I don't understand why the first snippet compiles fine : var res = getResult(s); public static double getResult(Shape s) { switch(s) { case Rectangle r : return 2* r.largeur() + 2* r.longueur(); case Circle c :…
AntonBoarf
  • 1,239
  • 15
  • 31
2
votes
0 answers

Why does C# switch expression over ushort? cause massive compiler errors?

I have the following code which fails to compile. It isn't the usual compiler error, this is something I have never seen before: Failing code: static class Domains { static DomainRole GetRole(ushort? role) { return role switch …
Thomas Eyde
  • 3,820
  • 2
  • 25
  • 32
2
votes
1 answer

Changing a chain of If-statements into a Switch-expression

I'm trying to convert the below if statement into a switch expression. public static int parseDays(String durationString) { String[] parts = durationString.split(" "); if (parts.length == 2) { return unitValueCalculation(parts[1],…
rds80
  • 631
  • 1
  • 10
  • 23
2
votes
1 answer

C# Pattern match arrays

var x = new int[] { 1, 2 }; var y = x switch { { 1, 2 } => "yea", _ => "nay" }; fails to compile. How can I pattern-match arrays?
theonlygusti
  • 11,032
  • 11
  • 64
  • 119
2
votes
0 answers

Can someone explain this type inference "glitch" in C# switch expression?

I was messing around with C# switch expression and I encountered something quite weird. Here is the code: static void Main(string[] args) { Console.WriteLine(Test('a')); Console.ReadLine(); } public static string Test(char x) { return…
DereckM
  • 274
  • 2
  • 11
2
votes
1 answer

Delegating Switch not considered in Code Coverage

I have found the following problem, while working with switch cases. R# showed me a simplification of the switch statement, which basically binds a delegate to the case label when you assign a variable. var shape = shapeType switch { …
user5350479
2
votes
2 answers

C# switch expressions null case

I've done some research, but have not found an answer to this. Is there a to represent the null case in the c# 8 switch expressions in such a way that the compiler will recognize and not trigger a warning for the reference x in the base case when…
Jesse
  • 915
  • 1
  • 11
  • 20
2
votes
2 answers

How do I declare parameters returned from a C# 8 switch expression?

I am looking at this code: public enum MO { Learn, Practice, Quiz } public enum CC { H } public class SomeSettings { public MO Mode { get; set; } public CC Cc { get; set; } } static void Main(string[] args) { var Settings =…
Alan2
  • 23,493
  • 79
  • 256
  • 450
2
votes
1 answer

c# 8 switch expression not "smart" enough

This code is simple, just a normal switch: bool? isSomething = strSomething switch { "I" => true, "D" => false, _ => null, }; However, the compiler gives me the following error: CS0037 Cannot convert null to 'bool' because it is a…
Nean Der Thal
  • 3,189
  • 3
  • 22
  • 40
2
votes
3 answers

Is there a way to squeeze "throw new Exception()" into an object?

Some other advanced languages like Haskell and Perl 6 provide syntactic sugar that lets exceptions be thrown, even in a place where the syntax demands an object. It is acts as though it becomes a thrown exception when the value is used (which would…
piojo
  • 6,351
  • 1
  • 26
  • 36
1
vote
3 answers

Why does yield keyword introduced for switch expressions? (not just use return keyword)

When a return statement occurs within a lambda expression, it simply causes a return from the lambda. It does not cause an enclosing method to return. So, for switch expressions why the keyword yield had introduced while return keyword could have…
Mehdi Rahimi
  • 1,453
  • 5
  • 20
  • 31
1
vote
1 answer

How to tell quarkus to use java 17?

I keep gettring build error saing that I'm using a preview feature (switch expressions): Service.java:28: error: patterns in switch statements are a preview feature and are disabled by default. case CalssA sr -> sr.getMeterReadings(); …
acanthite
  • 71
  • 6
1
vote
0 answers

Java is not accepting "break" in switch statement

I trying to insert "break" code in switch statement in java for stop looping output. I insert the break in switch statement, but the java is giving the "break is unusual statement" and break is greyed out. switch (Stardose) { case "Student",…
Harish M
  • 11
  • 1
1
vote
2 answers

How exit from method in a switch expression?

I'm studying switch expression, and I'd like to know how to pass control to the invoker method because "yield" just exits from the switch expression. I can't find any way to make it behave like the classic switch statement. Is it even…