C# 如何不能将对象与 null 进行比较?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/648115/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can an object not be compared to null?
提问by ProfK
I have an 'optional' parameter on a method that is a KeyValuePair. I wanted an overload that passes null to the core method for this parameter, but in the core method, when I want to check if the KeyValuePair is null, I get the following error:
我在 KeyValuePair 方法上有一个“可选”参数。我想要一个将 null 传递给该参数的核心方法的重载,但是在核心方法中,当我想检查 KeyValuePair 是否为 null 时,出现以下错误:
Operator '!=' cannot be applied to operands of type System.Collections.Generic.KeyValuePair<string,object>' and '<null>.
How can I not be allowed to check if an object is null?
如何不允许检查对象是否为空?
采纳答案by Jon Skeet
KeyValuePair<K,V>
is a struct, not a class. It's like doing:
KeyValuePair<K,V>
是一个结构,而不是一个类。这就像做:
int i = 10;
if (i != null) ...
(Although that is actually legal, with a warning, due to odd nullable conversion rules. The important bit is that the if
condition will never be true.)
(尽管这实际上是合法的,但由于奇怪的可为空转换规则而发出警告。重要的一点是if
条件永远不会为真。)
To make it "optional", you can use the nullable form:
要使其成为“可选”,您可以使用可为空的形式:
static void Foo(KeyValuePair<object,string>? pair)
{
if (pair != null)
{
}
// Other code
}
Note the ? in KeyValuePair<object,string>?
注意 ? 在KeyValuePair<object,string>?
回答by AnthonyWJones
Because KeyValuePair is structure (a value type), you can only compare nulls on reference types.
因为 KeyValuePair 是结构(一种值类型),所以您只能比较引用类型的空值。
I'm guessing you haven't written that extra overload yet. It will fail when you attempt to pass null as the value of a KeyValuePair.
我猜你还没有写额外的重载。当您尝试将 null 作为 KeyValuePair 的值传递时,它将失败。
回答by JaredPar
KeyValuePair<K,V> is a struct and hence can't be null under any circumstances. Only a reference type can be null.
KeyValuePair<K,V> 是一个结构体,因此在任何情况下都不能为空。只有引用类型可以为空。
If you're trying to catch an incorrect value you'll have to validate the Key and Value members individually.
如果您试图捕获不正确的值,则必须分别验证 Key 和 Value 成员。
回答by Jeremy
You can actually use KeyValuePair.IsNull() to determine if the KeyValuePair has been set.
您实际上可以使用 KeyValuePair.IsNull() 来确定是否已设置 KeyValuePair。
回答by eudaimos
I'm answering this despite its age because it is the 1st Google result for "test keyvaluepair to null"
尽管它的年龄我正在回答这个问题,因为它是“test keyvaluepair to null”的第一个谷歌结果
The answer specified is correct, but it doesn't completely answer the problem, at least the one I was having, where I need to test the existence of the KeyValuePair
and move on to another check of the Dictionary
if it doesn't exist the way I'm expecting.
指定的答案是正确的,但它并没有完全回答问题,至少是我遇到的问题,在那里我需要测试 的存在KeyValuePair
并继续进行另一次检查,Dictionary
如果它不以我的方式存在我期待
Using the method above didn't work for me because the compiler chokes on getting KeyValuePair.Value
of KeyValuePair<>?
, so it is better to utilize default(KeyValuePair<>)
as seen in this question + answers. The default for KeyValuePair
使用上面的方法对我不起作用,因为编译器在获取KeyValuePair.Value
时窒息KeyValuePair<>?
,所以最好使用default(KeyValuePair<>)
这个问题+答案中所见。KeyValuePair 的默认值