Three options:
First, you can write an extension method as follows:
public static TValue GetValueOrDefault<TKey, TValue>(
this IDictionary<TKey, TValue> dictionary,
TKey key)
{
TValue value;
dictionary.TryGetValue(dictionary, out value);
return value;
}
Then name it like:
var result = userlist.GetValueOrDefault("b");
if (result != null)
{
...
}
Secondly, you can use varwith out, specifying a dummy value:
var value = new { surname = "", age = 20 };
if (userlist.TryGetValue("b", out value))
{
...
}
Or according to the comments:
var value = userlist.Values.FirstOrDefault();
if (userlist.TryGetValue("b", out value))
{
...
}
Thirdly, first you can use ContainsKey:
if (userlist.ContainsKey("b"))
{
var result = userlist["b"];
...
}
source
share