C# KeyValuePair 的默认值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1641392/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 19:36:41  来源:igfitidea点击:

The default for KeyValuePair

c#key-value

提问by Graviton

I have an object of the type IEnumerable<KeyValuePair<T,U>> keyValueList, I am using

我有一个类型的对象IEnumerable<KeyValuePair<T,U>> keyValueList,我正在使用

 var getResult= keyValueList.SingleOrDefault();
 if(getResult==/*default */)
 {
 }
 else
 {
 } 

How can I check whether getResultis the default, in case I can't find the correct element?

getResult如果找不到正确的元素,如何检查是否为默认值?

I can't check whether it is nullor not, because KeyValuePairis a struct.

我无法检查它是否null存在,因为它KeyValuePair是一个结构体。

采纳答案by Andrew Hare

Try this:

尝试这个:

if (getResult.Equals(new KeyValuePair<T,U>()))

or this:

或这个:

if (getResult.Equals(default(KeyValuePair<T,U>)))

回答by ChaosPandion

if(getResult.Key.Equals(default(T)) && getResult.Value.Equals(default(U)))

回答by pholpar

You can create a general (and generic) extension method, like this one:

您可以创建一个通用(和通用)扩展方法,如下所示:

public static class Extensions
{
    public static bool IsDefault<T>(this T value) where T : struct
    {
        bool isDefault = value.Equals(default(T));

        return isDefault;
    }
}

Usage:

用法:

// We have to set explicit default value '0' to avoid build error:
// Use of unassigned local variable 'intValue'
int intValue = 0;
long longValue = 12;
KeyValuePair<String, int> kvp1 = new KeyValuePair<String, int>("string", 11);
KeyValuePair<String, int> kvp2 = new KeyValuePair<String, int>();
List<KeyValuePair<String, int>> kvps = new List<KeyValuePair<String, int>> { kvp1, kvp2 };
KeyValuePair<String, int> kvp3 = kvps.FirstOrDefault(kvp => kvp.Value == 11);
KeyValuePair<String, int> kvp4 = kvps.FirstOrDefault(kvp => kvp.Value == 15);

Console.WriteLine(intValue.IsDefault()); // results 'True'
Console.WriteLine(longValue.IsDefault()); // results 'False'
Console.WriteLine(kvp1.IsDefault()); // results 'False'
Console.WriteLine(kvp2.IsDefault()); // results 'True'
Console.WriteLine(kvp3.IsDefault()); // results 'False'
Console.WriteLine(kvp4.IsDefault()); // results 'True'

回答by Peter

Try this:

尝试这个:

KeyValuePair<string,int> current = this.recent.SingleOrDefault(r => r.Key.Equals(dialog.FileName) == true);

if (current.Key == null)
    this.recent.Add(new KeyValuePair<string,int>(dialog.FileName,0));

回答by Stephen Westlake - Infusion

From your original code it looks like what you want is to check if the list was empty:

从您的原始代码看来,您想要检查列表是否为空:

var getResult= keyValueList.SingleOrDefault();
if (keyValueList.Count == 0)
{
   /* default */
}
else
{
}

回答by Miroslav Holec

I recommend more understanding way using extension method:

我推荐使用扩展方法的更多理解方式:

public static class KeyValuePairExtensions
{
    public static bool IsNull<T, TU>(this KeyValuePair<T, TU> pair)
    {
        return pair.Equals(new KeyValuePair<T, TU>());
    }
}

And then just use:

然后只需使用:

var countries = new Dictionary<string, string>
{
    {"cz", "prague"},
    {"de", "berlin"}
};

var country = countries.FirstOrDefault(x => x.Key == "en");

if(country.IsNull()){

}

回答by Jonas Nyrup

To avoid the boxing of KeyValuePair.Equals(object)you can use a ValueTuple.

为避免拳击,KeyValuePair.Equals(object)您可以使用ValueTuple.

if ((getResult.Key, getResult.Value) == default)