How to make C# Switch Statement use IgnoreCase

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

    10 years later, with C# pattern matching, you can do something like:

    private string NormalisePropertyType(string propertyType) => true switch
    {
        true when string.IsNullOrWhiteSpace(propertyType) => propertyType,
        true when "house".Equals(propertyType, StringComparison.OrdinalIgnoreCase) => "house",
        true when "window".Equals(propertyType, StringComparison.OrdinalIgnoreCase) => "window",
        true when "door".Equals(propertyType, StringComparison.OrdinalIgnoreCase) => "door",
        true when "roof".Equals(propertyType, StringComparison.OrdinalIgnoreCase) => "roof",
        true when "chair".Equals(propertyType, StringComparison.OrdinalIgnoreCase) => "chair",
        _ => propertyType
    };
    

提交回复
热议问题