Switch statement with static fields

Suppose I have a bunch of static fields and I want to use them in a 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 # does not allow the intra contact operator inside. I want to understand what the intent of this design is. How do I do something like above in C #?

+8
c # static switch-statement
source share
6 answers

It looks like these string values ​​should just be constant.

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

If this is not an option (they are actually changed at run time), you can reorganize this solution into a series of if / else if commands.

Due to the fact that case arguments must be constant; due to the fact that they are constant, this allows you to significantly optimize the statement. This is actually more efficient than a series of if / else if statements (although this is not so dramatic if you don't have a lot of conditional checks that take a lot of time). It will generate a hash table equivalent with the values ​​of the case expression as keys. This approach cannot be used if values ​​can change.

+16
source share

... C # does not allow the contact operator inside the switch ...

If you can not use:

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

You can use the 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](); } 
+3
source share

The argument argument must be constant at compile time.

Use const instead:

 public const string PID_1 = "12"; public const string PID_2 = "13"; public const string PID_3 = "14"; 
+2
source share

I assume that you did not declare these variables as const . Nevertheless:

The switch is simply a shorthand for a bunch of if / else if . Therefore, if you can guarantee that PID_1 , PID_2 and PID_3 will never be equal, the above is equivalent to this:

 if (pid == PID_1) { // Do something 1 } else if (pid == PID_2) { // Do something 2 } else if (pid == PID_3) { // Do something 3 } else { // Do something default } 
+1
source share

The canonical way to approach this β€” if your static fields are not actually constants β€” is to use Dictionary<Something, Action> :

 static Dictionary<string, Action> switchReplacement = new Dictionary<string, Action>() { { PID_1, action1 }, { PID_2, action2 }, { PID_3, action3 }}; // ... Where action1, action2, and action3 are static methods with no arguments // Later, instead of switch, you simply call switchReplacement[pid].Invoke(); 
+1
source share

Why aren't you using an enumeration?
Keyword Enum:
http://msdn.microsoft.com/en-us/library/sbbt4032%28v=vs.80%29.aspx

In your case, it can be easily processed over the enumeration:

 public enum MyPidType { PID_1 = 12, PID_2 = 14, PID_3 = 18 } 
0
source share

All Articles