How to make C# Switch Statement use IgnoreCase

后端 未结 10 1079
孤独总比滥情好
孤独总比滥情好 2020-11-27 16:56

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:

string s =         


        
10条回答
  •  醉梦人生
    2020-11-27 17:24

    In some cases it might be a good idea to use an enum. So first parse the enum (with ignoreCase flag true) and than have a switch on the enum.

    SampleEnum Result;
    bool Success = SampleEnum.TryParse(inputText, true, out Result);
    if(!Success){
         //value was not in the enum values
    }else{
       switch (Result) {
          case SampleEnum.Value1:
          break;
          case SampleEnum.Value2:
          break;
          default:
          //do default behaviour
          break;
       }
    }
    

提交回复
热议问题