How to make C# Switch Statement use IgnoreCase

后端 未结 10 1138
孤独总比滥情好
孤独总比滥情好 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:25

    A simpler approach is just lowercasing your string before it goes into the switch statement, and have the cases lower.

    Actually, upper is a bit better from a pure extreme nanosecond performance standpoint, but less natural to look at.

    E.g.:

    string s = "house"; 
    switch (s.ToLower()) { 
      case "house": 
        s = "window"; 
        break;
    }
    

提交回复
热议问题