Skip to main content

Questions tagged [switch-expression]

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

switch-expression
-1 votes
0 answers
65 views

Why is my JSP code with a switch expression not working?

I'm trying to run this JSP code but it doesn't work, I get HTTP Status 500. Why? Aren't switch expressions supported? I do not want to use a switch statement, so please do not tell me to use this old ...
Ignis's user avatar
  • 19
0 votes
1 answer
50 views

Switch expression vs switch statement which one to use [closed]

Java 14 standardized switch expression and since Java 17 they have enhanced the switch expression to include pattern matching. Is there any guideline to when one should be using switch statement vs ...
JustACoder's user avatar
1 vote
3 answers
213 views

Can a Java 21 switch expression have multiple guards?

I have the following type for a command-line argument lexer (inspired by lexopt) private sealed interface Opt permits ShortOpt, LongOpt, MiscOpt {} record ShortOpt(char opt) implements Opt {} // the &...
quat's user avatar
  • 196
-3 votes
1 answer
85 views

using multiple statements in switch expression is giving CS0201 error [closed]

Below C# switch expression is giving Compiler Error CS0201 Only assignment, call, increment, decrement, and new object expressions can be used as a statement Not sure what is wrong here? In this ...
user10829672's user avatar
4 votes
3 answers
141 views

Why do I need an explicit downcast in an exhaustive switch expression?

Here's some dart code that seems interesting to me: void main() { A<num> someA = B(); A value = switch (someA) { B() => B(), C() => C(), }; } sealed class A<T extends ...
Taylor Brown's user avatar
2 votes
1 answer
193 views

Why Must Variables used in Guarded Pattern Case Labels be Final/Effectively Final? (Java 21)

With the new Java 21 features, we have better Pattern Matching. With these features, we have guarded pattern case labels, where one can define a pattern guard for case labels that looks like the ...
Idalas's user avatar
  • 73
2 votes
1 answer
179 views

CA1508: 'null => true' is always 'true'. Remove or refactor the condition(s) to avoid dead code

I'm encountering an interesting scenario with the CA1508 warning in C# when using a switch expression with a nullable DateTime property. Specifically, I have a property that determines if there are ...
Imperiestro Mongo's user avatar
0 votes
1 answer
65 views

Why does C# pattern matching return float for this?

Here is the source code bool v = false; object x = v switch { true => 0.0f, false => 100L, }; the pattern match returns different numeric types for the 2 branches, and the values is ...
John Smith's user avatar
0 votes
1 answer
103 views

How to use a switch expression to match a value where it equals another non-constant value [duplicate]

I want to write a method that converts a WPF System.Windows.FontWeight property to its equivalent CSS value using C#11. A switch expression seems the most appropriate construct. Here's my attempt: ...
Dan Stevens's user avatar
  • 6,688
0 votes
0 answers
100 views

SonarLint: Why does this condition always evaluates to true?

I'm writing a generic extension function which parses a string value to other types. The function basically looks like this: public static T Parse<T>(string input) { T result = (T)(typeof(T) ...
Daniel Schmid's user avatar
0 votes
2 answers
66 views

Super weird behavior of C# switch expression [duplicate]

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 ...
Dmitry's user avatar
  • 1,320
3 votes
2 answers
147 views

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: ...
Sinatr's user avatar
  • 21.6k
3 votes
3 answers
935 views

Why was the 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 ...
Mehdi Rahimi's user avatar
  • 2,124
1 vote
1 answer
414 views

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. ...
acanthite's user avatar
  • 133
3 votes
1 answer
129 views

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 =&...
Xiaoguo Ge's user avatar
  • 2,248

15 30 50 per page
1
2 3 4 5