Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Switch statement with static fields

Suppose I have a bunch of static fields and I want to use them in switch:

public static string PID_1 = "12";
public static string PID_2 = "13";
public static string PID_3 = "14";

switch(pid)
{
    case PID_1:
        //Do something 1
        break;
    case PID_2:
        //Do something 2
        break;
    case PID_3:
        //Do something 3
        break;
    default:
        //Do something default
        break;
}

Since C# doesn't allow non-const statement inside switch. I want to understand what is the intention of this kind of design. How should I do something like above in c#?

like image 904
Ashutosh Avatar asked Sep 14 '12 18:09

Ashutosh


People also ask

Can you change static fields?

Static methods cannot access or change the values of instance variables or the this reference (since there is no calling object for them), and static methods cannot call non-static methods.

How does switch statement works?

A statement in the switch block can be labeled with one or more case or default labels. The switch statement evaluates its expression, then executes all statements that follow the matching case label.

Can you have a switch statement within a switch statement C#?

It is possible to have a switch as part of the statement sequence of an outer switch. Even if the case constants of the inner and outer switch contain common values, no conflicts will arise.

What is switch statement in C# with example?

The switch statement evaluates the expression (or variable ) and compare its value with the values (or expression) of each case ( value1 , value2 , …). When it finds the matching value, the statements inside that case are executed.


3 Answers

It looks like those string values should simply be constant.

public const string PID_1 = "12";
public const string PID_2 = "13";
public const string PID_3 = "14";

If that's not an option (they are actually changed at runtime), then you can refactor that solution into a series of if/else if statements.

As to why the case statements need to be constant; by having them be constant it allows the statement to be much more heavily optimized. It is actually more efficient than a series of if/else if statements (although not dramatically so if you don't have lots of conditional checks that take a long time). It will generate the equivalent of a hash table with the case statement values as keys. That approach couldn't be used if the values can change.

like image 71
Servy Avatar answered Oct 02 '22 17:10

Servy


I know this is an old question but there is a way that wasnt covered in the other answers that doesnt involve changing the approach:

switch(pid)
{
   case var _ when pid == PID_1:
      //Do something 1
   break;
}
like image 24
Mr Wood Avatar answered Oct 02 '22 17:10

Mr Wood


... C# doesn't allow non-const statement inside switch...

If you can't use:

public const string PID_1 = "12";
public const string PID_2 = "13";
public const string PID_3 = "14";

You can use a dictionary :)

....
public static string PID_1 = "12";
public static string PID_2 = "13";
public static string PID_3 = "14";



// Define other methods and classes here

void Main()
{
   var dict = new Dictionary<string, Action>
   {
    {PID_1, ()=>Console.WriteLine("one")},
    {PID_2, ()=>Console.WriteLine("two")},
    {PID_3, ()=>Console.WriteLine("three")},
   };
   var pid = PID_1;
   dict[pid](); 
}
like image 4
OscarRyz Avatar answered Oct 02 '22 18:10

OscarRyz