string[] arrTopics = {"Health", "Science", "Politics"};
I have an if statement like:
if (arrTopics.Count() != null)
When I hover my mouse over the above statement, it says:
Warning CS0472: The result of the expression is always true since a value of type int is never equal to null of type int?
I am not able to figure out why it is saying so. Can anybody help me?
In other words, null can be cast to Integer without a problem, but a null integer object cannot be converted to a value of type int.
Characteristics of Nullable TypesNullable types can only be used with value types. The Value property will throw an InvalidOperationException if value is null; otherwise it will return the value. The HasValue property returns true if the variable contains a value, or false if it is null. You can only use == and !=
No, because int is a value type. int is a Value type like Date , double , etc. So there is no way to assigned a null value.
int
can never be equal to null. int?
is the nullable version, which can be equal to null.
You should check if(arrTopics.Count() != 0)
instead.
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) ...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With