为什么我得到“CS0472:表达式的结果总是为真,因为int类型的值永远不等于int类型的null?”

时间:2022-09-25 14:12:07
string[] arrTopics = {"Health", "Science", "Politics"};

I have an if statement like:

我有一个if语句,如:

 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?

警告CS0472:表达式的结果始终为true,因为int类型的值永远不等于int类型的null?

I am not able to figure out why it is saying so. Can anybody help me?

我无法弄清楚为什么这么说。有谁能够帮助我?

8 个解决方案

#1


21  

int can never be equal to null. int? is the nullable version, which can be equal to null.

int永远不能等于null。诠释?是可以为空的版本,可以等于null。

You should check if(arrTopics.Count() != 0) instead.

您应该检查是否(arrTopics.Count()!= 0)。

#2


5  

null represents the absence of any value, not the number 0. And as the message says an int can never be null since it's neither a reference type nor a nullable value type and thus always has some value.

null表示没有任何值,而不是数字0.并且正如消息所说,int永远不能为null,因为它既不是引用类型也不是可空值类型,因此总是有一些值。

#3


5  

It means what it says.

它意味着它所说的。

The "Count" method returns a value type. It's an integer. It will always have a value where it's default value is zero.

“Count”方法返回值类型。这是一个整数。它始终具有默认值为零的值。

Your check really should be:

你的检查应该是:

if (arrTopics.Count() != 0)

#4


4  

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

就像错误说的那样,int永远不会为null。我将代码更改为

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

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

或者甚至更高效,如果你知道arrTopics是一个数组,永远不会是null

arrTopics.Length != 0

#5


4  

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:

如果您想知道Array是否已初始化,那么:

   if(arrTopics !=null) ...

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

如果你想知道它是否已初始化但没有成员那么:

  if(arrTopics.Count() > 0) ...

#6


3  

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.

Null是一个特殊的指针值,而不是整数。有可空类型,它们是null或基类型的可能值之一,但int本身不可为空。

#7


3  

Because Count method always returns integer if no elements in array it will return 0 other wise it will return number of elements. So what you need to do is just instead != null make it != 0 or > 0

因为Count方法总是返回整数,如果数组中没有元素,它将返回0,否则它将返回元素数。所以你需要做的只是改为!= null make it!= 0或> 0

#8


3  

To figure it out, look into geclaration of Count() extension method.

要想出来,请查看Count()扩展方法的geclaration。

Puzzle of two pieces:

两件拼图:

  1. You are trying to compare non-nullable type (value of type int) with null, so non-nullable types is never being referenced to, because all non-nullable types are value types in C#, which can not be referenced.

    您正在尝试将非可空类型(int类型的值)与null进行比较,因此永远不会引用非可空类型,因为所有非可空类型都是C#中的值类型,无法引用。

  2. Inequality defined through equality oerator in object class, which is defined in base. so your code mentioned above can be perfectly valid. Unfortunately, to distinguish situation when this appearance of equality operator in not desired (not overridden) in base classes there are some compiler warinings about it, because you will really get a always-false condition for incompatible types (or get an always-true condition in inequality operator)

    通过对象类中的等式oerator定义的不等式,它在base中定义。所以你上面提到的代码完全有效。不幸的是,为了区分情况,当基类中不需要(未覆盖)的等式运算符出现时,会有一些编译器对它进行警告,因为对于不兼容的类型(或者获得始终为真的条件),您将真正得到一个始终为假的条件在不平等运算符中)

And value types (non-nullable) and nullable types (referenced types) are incompatible types in C#. For more info look into ECMA papers for standart of definition for type system being used in a C# language.

值类型(不可为空)和可空类型(引用类型)是C#中不兼容的类型。有关更多信息,请查看ECMA论文,了解C#语言中使用的类型系统的定义。

#1


21  

int can never be equal to null. int? is the nullable version, which can be equal to null.

int永远不能等于null。诠释?是可以为空的版本,可以等于null。

You should check if(arrTopics.Count() != 0) instead.

您应该检查是否(arrTopics.Count()!= 0)。

#2


5  

null represents the absence of any value, not the number 0. And as the message says an int can never be null since it's neither a reference type nor a nullable value type and thus always has some value.

null表示没有任何值,而不是数字0.并且正如消息所说,int永远不能为null,因为它既不是引用类型也不是可空值类型,因此总是有一些值。

#3


5  

It means what it says.

它意味着它所说的。

The "Count" method returns a value type. It's an integer. It will always have a value where it's default value is zero.

“Count”方法返回值类型。这是一个整数。它始终具有默认值为零的值。

Your check really should be:

你的检查应该是:

if (arrTopics.Count() != 0)

#4


4  

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

就像错误说的那样,int永远不会为null。我将代码更改为

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

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

或者甚至更高效,如果你知道arrTopics是一个数组,永远不会是null

arrTopics.Length != 0

#5


4  

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:

如果您想知道Array是否已初始化,那么:

   if(arrTopics !=null) ...

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

如果你想知道它是否已初始化但没有成员那么:

  if(arrTopics.Count() > 0) ...

#6


3  

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.

Null是一个特殊的指针值,而不是整数。有可空类型,它们是null或基类型的可能值之一,但int本身不可为空。

#7


3  

Because Count method always returns integer if no elements in array it will return 0 other wise it will return number of elements. So what you need to do is just instead != null make it != 0 or > 0

因为Count方法总是返回整数,如果数组中没有元素,它将返回0,否则它将返回元素数。所以你需要做的只是改为!= null make it!= 0或> 0

#8


3  

To figure it out, look into geclaration of Count() extension method.

要想出来,请查看Count()扩展方法的geclaration。

Puzzle of two pieces:

两件拼图:

  1. You are trying to compare non-nullable type (value of type int) with null, so non-nullable types is never being referenced to, because all non-nullable types are value types in C#, which can not be referenced.

    您正在尝试将非可空类型(int类型的值)与null进行比较,因此永远不会引用非可空类型,因为所有非可空类型都是C#中的值类型,无法引用。

  2. Inequality defined through equality oerator in object class, which is defined in base. so your code mentioned above can be perfectly valid. Unfortunately, to distinguish situation when this appearance of equality operator in not desired (not overridden) in base classes there are some compiler warinings about it, because you will really get a always-false condition for incompatible types (or get an always-true condition in inequality operator)

    通过对象类中的等式oerator定义的不等式,它在base中定义。所以你上面提到的代码完全有效。不幸的是,为了区分情况,当基类中不需要(未覆盖)的等式运算符出现时,会有一些编译器对它进行警告,因为对于不兼容的类型(或者获得始终为真的条件),您将真正得到一个始终为假的条件在不平等运算符中)

And value types (non-nullable) and nullable types (referenced types) are incompatible types in C#. For more info look into ECMA papers for standart of definition for type system being used in a C# language.

值类型(不可为空)和可空类型(引用类型)是C#中不兼容的类型。有关更多信息,请查看ECMA论文,了解C#语言中使用的类型系统的定义。