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

The switch statement: Unlike the if/else if/else statement, a switch statement can have a number of possible execution paths. A switch works with the primitive types, byte, short, char, and int, their respective wrapper types (Byte, Short, Character, and Integer), enumerated types, and the String type1. While an if-else statement is used to test expressions … Read more

Capture makes remaining patterns unreachable

Cause of the problem A variable name in a case clause is treated as a name capture pattern. It always matches and tries to make an assignment to the variable name. This is almost certainly not what was intended. Because the first matching case wins and because case OKAY always matches, the other case clauses … Read more

Combine return and switch in C#

Actually this is possible using switch expression starting with C# 8. return a switch { 1 => “lalala”, 2 => “blalbla”, 3 => “lolollo”, _ => “default” }; Switch Expression There are several syntax improvements here: The variable comes before the switch keyword. The different order makes it visually easy to distinguish the switch expression … Read more

Eclipse bug? Switching on a null with only default case

This is a bug. Here’s the specified behavior for a switch statement according to the Java Language Specification, 3rd Edition: JLS 14.11 The switch Statement SwitchStatement: switch ( Expression ) SwitchBlock When the switch statement is executed, first the Expression is evaluated. If the Expression evaluates to null, a NullPointerException is thrown and the entire … Read more

SQL use CASE statement in WHERE IN clause

No you can’t use case and in like this. But you can do SELECT * FROM Product P WHERE @Status=”published” and P.Status IN (1,3) or @Status=”standby” and P.Status IN (2,5,9,6) or @Status=”deleted” and P.Status IN (4,5,8,10) or P.Status IN (1,3) BTW you can reduce that to SELECT * FROM Product P WHERE @Status=”standby” and P.Status … Read more