How to make C# Switch Statement use IgnoreCase

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

    It should be sufficient to do this:

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

    The switch comparison is thereby culture invariant. As far as I can see this should achieve the same result as the C#7 Pattern-Matching solutions, but more succinctly.

提交回复
热议问题