Why am I getting “CS0472: The result of the expression is always true since a value of type int is never equal to null of type int?”

后端 未结 9 1387
感动是毒
感动是毒 2020-12-20 12:09
string[] arrTopics = {\"Health\", \"Science\", \"Politics\"};

I have an if statement like:

 if (arrTopics.Count() != null)
<         


        
相关标签:
9条回答
  • 2020-12-20 12:28

    Like the error says, int can never be null. I'd change the code to

    if (arrTopics != null && arrTopics.Any())
    

    Or even more efficient if you know arrTopics is an array and never null is

    arrTopics.Length != 0
    
    0 讨论(0)
  • 2020-12-20 12:32

    What are you trying to ask here?

       Array.Count() returns an int which will never be null.
    

    If you want to know if the Array has been initialised then:

       if(arrTopics !=null) ...
    

    if you want to know if it's been initialised but has no members then:

      if(arrTopics.Count() > 0) ...
    
    0 讨论(0)
  • 2020-12-20 12:41

    Null is a special pointer value, and not an integer. There are nullable types, that are either null or one of the possible values for the base type, but int itself is not nullable.

    0 讨论(0)
提交回复
热议问题