tagged [switch-statement]

C# switch/break

C# switch/break It appears I need to use a break in each case block in my switch statement using C#. I can see the reason for this in other languages where you can fall through to the next case statem...

25 Nov at 05:9

in switch case if we write "default" as any word or single letter it does not throw an error

in switch case if we write "default" as any word or single letter it does not throw an error In a `switch`, if we write any word or single letter instead of `default` it does not throw an error. e.g. ...

15 Nov at 05:59

Switch case with conditions

Switch case with conditions Am I writing the correct switch case with conditions? For some reason, the alert does no

17 Sep at 06:41

C# switch on type

C# switch on type > [C# - Is there a better alternative than this to 'switch on type'?](https://stackoverflow.com/questions/298976/c-sharp-is-there-a-better-alternative-than-this-to-switch-on-type) ...

2 Apr at 02:18

using the 'is' keyword in a switch in c#

using the 'is' keyword in a switch in c# I'm currently adding some new extended classes to this code: and was curious if there is a way to use the `is` keywor

17 Mar at 17:20

Combine return and switch

Combine return and switch How can I combine `return` and `switch case` statements? I want something like I know about this solution ``` switch(a) { case 1: return "lalala"; case 2: return "blalbla...

Switch on ranges of integers in JavaScript

Switch on ranges of integers in JavaScript I want to do something like this What is the right syntax for this? Is it pos

11 Jul at 16:55

Compiler error for exhaustive switch

Compiler error for exhaustive switch Why do I get a "", for `VeryBoolToBool()` in the following code? ``` public enum VeryBool { VeryTrue, VeryFalse }; public bool VeryBoolToBool(VeryBool veryBool) { ...

24 Dec at 10:49

Add a additional condition to Case Statement in Switch

Add a additional condition to Case Statement in Switch Is it possible to add a additional Condition to Switch Statement like below in C# In above mentioned example if then

10 Jan at 07:8

How to make C# Switch Statement use IgnoreCase

How to make C# Switch Statement use IgnoreCase If I have a switch-case statement where the object in the switch is string, is it possible to do an ignoreCase compare? I have for instance: Will `s` get...

5 Nov at 23:44

SQL use CASE statement in WHERE IN clause

SQL use CASE statement in WHERE IN clause Is it posible to use in clause? Something like this: ``` DECLARE @Status VARCHAR(50); SET @Status='published'; SELECT * FROM Product P WHERE P.Status IN (CA...

C# Switch-case string starting with

C# Switch-case string starting with Is there any way to make a case condition in a switch statement where you say if a string begins with something? ex Other strings can be different length. abc.

4 Oct at 08:44

Switch case in C# - a constant value is expected

Switch case in C# - a constant value is expected My code is as follows: ``` public static void Output(IEnumerable dataSource) where T : class { dataSourceName = (typeof(T).Name); switch (dataSour...

26 Sep at 13:46

C# Switch with String.IsNullOrEmpty

C# Switch with String.IsNullOrEmpty Is it possible to have a switch in C# which checks if the value is null or empty not "" but `String.Empty`? I know i can do this: Is there something better, because...

7 Jun at 16:26

What is the purpose of the extra braces in Switch case?

What is the purpose of the extra braces in Switch case? I'm curious about this thing... see example: All my life I've done it like case b, but since C# allows me to use it, and Visual Studio allows me...

4 Oct at 17:24

Inline switch / case statement in C#

Inline switch / case statement in C# I am on a weird kick of seeing how few lines I can make my code. Is there a way to condense this to inline case statements? ``` switch (FIZZBUZZ) { case "Fizz...

5 Aug at 14:50

Is there any significant difference between using if/else and switch-case in C#?

Is there any significant difference between using if/else and switch-case in C#? What is the benefit/downside to using a `switch` statement vs. an `if/else` in C#. I can't imagine there being that big...

20 Jun at 09:12

c# : Why is a cast needed from an Enum to an INT when used in a switch statement?, enums are ints

c# : Why is a cast needed from an Enum to an INT when used in a switch statement?, enums are ints Can anyone tell me why i need to cast to Int from my enum If i remove the cast (int) it fails and says...

19 Nov at 12:15

Using continue in a switch statement

Using continue in a switch statement I want to jump from the middle of a `switch` statement, to the loop statement in the following code: ``` while (something = get_something()) { switch (something)...

10 Jul at 11:33

C# Switch statement with/without curly brackets.... what's the difference?

C# Switch statement with/without curly brackets.... what's the difference? Has C# always permitted you to omit curly brackets inside a `switch()` statement between the `case:` statements? What is the ...

21 May at 21:45

Why is break required after yield return in a switch statement?

Why is break required after yield return in a switch statement? Can somebody tell me why compiler thinks that `break` is necessary after `yield return` in the following code? ``` foreach (DesignerNode...

Refactor long switch statement

Refactor long switch statement I'm program in c# which you controlling by dictating command so now i have a long switch statement. Something like ``` switch (command) { case "Show commands": Pro...

14 Dec at 17:25

Is there a better alternative than this to 'switch on type'?

Is there a better alternative than this to 'switch on type'? Seeing as C# can't `switch` on a Type (which I gather wasn't added as a special case because `is` relationships mean that more than one dis...

16 Sep at 18:8

In C# is default case necessary on a switch on an enum?

In C# is default case necessary on a switch on an enum? I've seen posts [relating to C++](https://stackoverflow.com/questions/2201493/using-default-in-a-switch-statement-when-switching-over-an-enum), ...

23 May at 10:31

C# 7 Pattern Match with a tuple

C# 7 Pattern Match with a tuple Is it possible to use tuples with pattern matching in switch statements using c# 7 like so: I get an error that says `tObj does not exist in the current context`. I hav...