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
1
vote
3 answers

Is there a way for switch to return a string value using C# 8 switch expressions?

I have this code where each part of the switch returns a value to ModeMessage2. Is it possible using the new C# switch expressions (or any other code optimization) to optimize the way this switch works? switch (Settings.Mode) { case MO.Learn: …
Alan2
  • 23,493
  • 79
  • 256
  • 450
1
vote
3 answers

c# 8.0 switch expression return type and null value

I'm trying to understand how C# 8.0 switch expressions work and have got several questions. Why is it not possible to use null value in the default case? The compiler throws Cannot convert null to 'int' because it is a non-nullable value type…
yaugenka
  • 2,602
  • 2
  • 22
  • 41
1
vote
2 answers

java - return String with switch case

i tried to use an enum for my first time. For some Tests i want to override the toString method of my enum and return a String with the chosen enum. Heres my code so far: @Override public String toString() { return "Fahrzeuge{" + …
0
votes
2 answers

Super weird behavior of C# switch expression

I encountered a very strange behavior of the C# switch expression, which I have no idea how to explain. So, I am practicing solving general interview problems, and today it was Kth Largest Element in an Array. I am posting the whole function in the…
Dmitry
  • 1,220
  • 2
  • 11
  • 18
0
votes
2 answers

How can this switch statement be changed to a switch expression?

public class ChineseZodiac { public static void Main(String[] args) { Console.Write("Enter a year: "); var year = Convert.ToInt64(Console.ReadLine()); switch (year % 12) { case 0: …
0
votes
3 answers

problem with "String line = switch (a) in java loop. Cant implement cases

I made a program of tic-tac-toe. There is a problem on the line String line = switch(a) ("illegal start of expression"). It is a window application of this game. I have no idea how to fix this. public void checkIfGameIsOver(){ for (int a = 0; a…
Satelita
  • 3
  • 1
0
votes
2 answers

C# 8 Switch Expression

Can I replace this code snippent with a C#8 switch expression? Note that if ObjectType is Computer, ObjectClass will contain "person" so ordering matters. Also, the question is academic and I am only interested in the switch expression and not how…
bob
  • 579
  • 1
  • 8
  • 22
0
votes
1 answer

C# 8.0 switch statement with void

I am getting the following error: Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement public void LogClientSideErrors(string message, ValidationType validationType) => validationType…
0
votes
1 answer

Switch expression in an Azure Function causes an exception

Writing a C# Azure function and trying to use C#8 switch expressions. Accoring to the docs, https://learn.microsoft.com/en-us/dotnet/api/system.runtime.compilerservices.switchexpressionexception?view=netcore-3.1, they show this should be available…
Darren Wainwright
  • 30,247
  • 21
  • 76
  • 127
0
votes
2 answers

Async all the way when using pipeline

If I have an application that uses a pipeline with many stages to be executed using foreach on all the stages and call: CanExecute Execute Interface is this: public interface IService { bool CanExecute(IContext subject); IContext…
0
votes
2 answers

Return generic value from switch expression in Java-12

I am wondering if there is an option to return a generic type from a Java 12 switch expression. The basic code can look like that: boolean result = switch(ternaryBool) { case TRUE -> true; case FALSE -> false; default -> throw new…
1 2 3
4