Switch statement with static fields

前端 未结 7 1984
执念已碎
执念已碎 2021-01-11 14:28

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 st         


        
相关标签:
7条回答
  • 2021-01-11 14:42

    I'm assuming there's a reason you didn't declare those variables as const. That said:

    The switch statement is just shorthand for a bunch of if / else if statements. So 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
    }
    
    0 讨论(0)
  • 2021-01-11 14:42

    Why you don't use enum ?
    Enum keyword:
    http://msdn.microsoft.com/en-us/library/sbbt4032%28v=vs.80%29.aspx

    In your case it can be easily handled over enum:

    public enum MyPidType
    {
      PID_1 = 12,
      PID_2 = 14,
      PID_3 = 18     
    }
    
    0 讨论(0)
  • 2021-01-11 14:43

    Case argument should be constant on compile-time.

    Try to use const instead:

    public const string PID_1 = "12";
    public const string PID_2 = "13";
    public const string PID_3 = "14";
    
    0 讨论(0)
  • 2021-01-11 14:45

    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;
    }
    
    0 讨论(0)
  • 2021-01-11 14:50

    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.

    0 讨论(0)
  • 2021-01-11 14:50

    ... 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](); 
    }
    
    0 讨论(0)
提交回复
热议问题