Possible unintended reference comparison

前端 未结 3 858
我寻月下人不归
我寻月下人不归 2020-11-28 16:15

I have the following code which gives a warning

Possible unintended reference comparison; to get a value comparison, cast the left hand side to type \

3条回答
  •  有刺的猬
    2020-11-28 17:07

    I prefer to stick the string.Equals(string,string,StringComparison) method, like the following:

        string contentStr = (lblStatus.Content ?? string.Empty).ToString();
        if (string.Equals("ACTIVE", contentStr, StringComparison.OrdinalIgnoreCase))
        { 
            // stuff
        } 
    

    because it explicitely states what it does + it doesn't give a warning you've mentioned.

提交回复
热议问题