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

Java-17 - switch case - Unused method parameters should be removed

I have a simple method which takes an enum and returns a String: public static String enumToString(MyEnum type) { return switch (type) { case Enum1 -> "String_1"; case Enum2 -> "String_2"; case Enum3 -> "String_3"; …
Doesn't Matter
  • 1,061
  • 3
  • 11
  • 29
6
votes
2 answers

Java 12 intellij switch expressions doesn't work

I try to use Java 12 in IntelliJ but when I try to run My app occurs error Error:(57, 32) java: switch expressions are a preview feature and are disabled by default. (use --enable-preview to enable switch expressions) I added in app configuration…
5
votes
1 answer

java 17 "'switch' expression does not cover all possible input values" is not shown if there is no return

I'm developing with java 17 in IntelliJ 2022.2. In some cases 'switch' expression does not cover all possible input values is shown, but in some not. I'd like to figure out why. Let's assume that entityType is an enum with 3 values and I'm adding…
andriy
  • 4,074
  • 9
  • 44
  • 71
5
votes
2 answers

Does Swift have a Switch *expression* (as opposed to a Switch *statement*) like C#?

Hopefully by the title of this, it's clear I'm not asking does Swift support the Switch statement. I'm specifically asking if Swift supports Switch expressions, akin to what C# has. The difference is subtle, but important. A Switch statement is a…
Mark A. Donohoe
  • 28,442
  • 25
  • 137
  • 286
4
votes
2 answers

Is there a way to do switch expression fallthrough with lambda-like syntax for default case?

What I'm trying to do is something like this, where a specific value & the default case can both map to a single value. I should clarify that the purpose of this is to be as explicit as possible. I understand that just using default would achieve…
turbofood
  • 192
  • 9
4
votes
1 answer

Why can't return be expressed in one line when using a Switch Expression?

while (true) { console.mainMenu(); String inputCommand = console.input(); switch(inputCommand) { case "exit" -> return; case "create" -> { Voucher voucher = createVoucher(); …
yongc
  • 43
  • 3
4
votes
0 answers

Dereference of a possible null reference (CS8602) after matching with object pattern in a switch expression

I have the following code in a definition of a custom IEqualityComparer: public bool Equals(Uri? x, Uri? y) => (x, y) switch { (null, _) => false, (_, null) => false, ({}, {}) => x.Equals(y) }; I'm getting a CS8602 warning on the…
V0ldek
  • 9,623
  • 1
  • 26
  • 57
4
votes
1 answer

Use lambda function in new switch c# 8.0 to return value

I want to use new switch in my code, that for method result make log and return IActionResult. I try to do something like this: var response = (this._coreRepository.Write(value.Content, data.Id.ToString())); \\return bool return response switch { …
Claus Stolz
  • 358
  • 4
  • 18
4
votes
2 answers

What is the operator precedence for the new C# 8.0 switch expressions?

I just upgraded my current project to the newly released .NET Standard 2.1 and C# 8.0, and decided to convert some large switch statements into the new, much more compact expression syntax. Since the returned values are further used in some…
janw
  • 8,758
  • 11
  • 40
  • 62
3
votes
3 answers

How to use indexers in switch expression?

How to access indexers in switch expression? There is a nice property pattern syntax in switch expressions, but I can't figure out or find any information about using indexers. Given following code: var a = "123"; if(a.Length == 3 && a[0] == '1') …
Sinatr
  • 20,892
  • 15
  • 90
  • 319
3
votes
1 answer

Is this a C# switch expression exhaustiveness check bug?

public enum Test { Yes, No } I have these two switch expressions. The one below gives a CS8509 warning: Test? test = null; var result = test switch { null => "Null", Test.Yes => "Yes", Test.No => "No", }; But moving the null case to…
Xiaoguo Ge
  • 2,177
  • 20
  • 26
3
votes
4 answers

Why Switch requires statement but accepts expressions

I'm studying switch expressions and I think I found a weird behaviour: public static boolean isTimeToParty(Day day) { switch (day) { case MONDAY -> dog(); //expression allowed case TUESDAY -> 2 + 2; //error: not a statement …
3
votes
2 answers

How to make an empty default case in switch expression in C#?

How to make an empty default case in switch expression in C#? I am talking about this language feature. Here is what I am trying: using System; public class Program { public static void Main() { int i = -2; …
qqqqqqq
  • 1,831
  • 1
  • 18
  • 48
3
votes
1 answer

Pattern Matching Enhancement: Switch Pattern

Just had a look at the "new" C# 8.0 Features So I tried to rewrite the following code private static void RunExample(ExampleCode exampleCode) { switch(exampleCode) { case ExampleCode.DefaultInterfaceMethod: …
Fabian Bigler
  • 10,403
  • 6
  • 47
  • 70
3
votes
1 answer

How to deal with optional arguments when wanting to enable nullable reference types?

I see the great advantage of turning on (non-)nullable reference types, but I have quite a few methods with optional parameters and I am wondering what the right way to correct the warnings yielded by the compiler is. Making the parameter nullable…
David
  • 2,426
  • 3
  • 24
  • 36