Reputation: 83254
I have an object of the type IEnumerable<KeyValuePair<T,U>> keyValueList
, I am using
var getResult= keyValueList.SingleOrDefault();
if(getResult==/*default */)
{
}
else
{
}
How can I check whether getResult
is the default, in case I can't find the correct element?
I can't check whether it is null
or not, because KeyValuePair
is a struct.
Upvotes: 500
Views: 254491
Reputation: 3366
using pattern match in C# 8.0
or above or .NET Core 3.x
, .NET 5.x
or above
//
// assume key is string and value is object
//
var getResult = keyValueList.SingleOrDefault();
// if clause
if (getResult is { Key: default(string), Value: default(object) })
{
do_both_key_value_are_default();
}
// if clause negate
if (getResult is not { Key: default(string), Value: default(object) })
{
do_both_key_value_found();
}
// switch style
switch (getResult)
{
case { Key: default(string), Value: default(object) }: // this is default case where cannot find the single
do_both_key_value_are_default();
break;
default:
do_something_else();
break;
}
Upvotes: 0
Reputation: 74605
It may suffice, for your application, to check if either the Key, or the Value are default
.. For example if you have a KeyValuePair<int,string>
mapping positive nonzero integers to strings, and the strings are never null, then either:
if(getResult.Key == default)
if(getResult.Value == default)
Would be an indicator that a default KVP has been returned
In essence, there may be no need to test the whole KVP for being default, if just one test will do; consider it for your use case
Upvotes: 3
Reputation: 2586
To avoid the boxing of KeyValuePair.Equals(object)
you can use a ValueTuple
.
if ((getResult.Key, getResult.Value) == default)
Upvotes: 8
Reputation: 78272
if(getResult.Key.Equals(default(T)) && getResult.Value.Equals(default(U)))
Upvotes: 10
Reputation: 3217
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()){
}
Upvotes: 10
Reputation: 1785
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'
Upvotes: 119
Reputation: 101
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
{
}
Upvotes: 9
Reputation: 329
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));
Upvotes: 32
Reputation: 351526
Try this:
if (getResult.Equals(new KeyValuePair<T,U>()))
or this:
if (getResult.Equals(default(KeyValuePair<T,U>)))
Upvotes: 683