I have a method retrieving a value from a dictionary by key, or returning a default value if not found
public static TValue GetValueOrDefault<TKey, TValue>(this IDictionary<TKey, TValue> dict, TKey key, TValue defaultValue)
{
if (dict.TryGetValue(key, out TValue existingValue))
{
return existingValue;
}
else
{
return defaultValue;
}
}
When used in a generic method, the default value is set to default(T) because it can be a value or reference type
T myGenericVar = myDict.GetValueOrDefault(key, default(T));
Now I want to compare this var in order to understand if the value is found in the dictionary, but the operator == cannot be used since the compiler doesn't know if it's a value or reference type. So I used Equals
bool found = myGenericVar.Equals(default(T))
It happens that if T is a reference type, default(T) is actually null and calling Equals on null produces a NullReferenceException
How can I avoid the exception?
As hinted in the comments, if you want to know whether the default is returned or not create another method TryGetValueOrDefault:
public static bool TryGetValueOrDefault<TKey, TValue>(this IDictionary<TKey, TValue> dict, TKey key, TValue defaultValue, out TValue value)
{
if (dict.TryGetValue(key, out TValue existingValue))
{
value = existingValue
return true;
}
else
{
value = defaultValue;
return false
}
}
I am using a concurrent dictionary as a thread-safe static cache and noticed the following behavior:
From the MSDN docs on GetOrAdd:
If you call GetOrAdd simultaneously on different threads,
addValueFactory may be called multiple times, but its key/value pair
might not be added to the dictionary for every call.
I would like to be able to guarantee that the factory is only called once. Is there any way to do this with the ConcurrentDictionary API without resorting to my own separate synchronization (e. g. locking inside valueFactory)?
My use case is that valueFactory is generating types inside a dynamic module so if two valueFactories for the same key run concurrently I hit:
System.ArgumentException: Duplicate type name within an assembly.
You could use a dictionary that is typed like this: ConcurrentDictionary<TKey, Lazy<TValue>>, and then the your value factory would return a Lazy<TValue> object that has been initialized with LazyThreadSafetyMode.ExecutionAndPublication, which is the default option used by Lazy<TValue> if you don't specify it. By specifying the LazyThreadSafetyMode.ExecutionAndPublication you are telling Lazy only one thread may initialize and set the value of the object.
This results in the ConcurrentDictionary only using one instance of the Lazy<TValue> object, and the Lazy<TValue> object protects more than one thread from initializing its value.
i.e.
var dict = new ConcurrentDictionary<int, Lazy<Foo>>();
dict.GetOrAdd(key,
(k) => new Lazy<Foo>(valueFactory)
);
The downside then is you'll need to call *.Value every time you are accessing an object in the dictionary. Here are some extensions that'll help with that.
public static class ConcurrentDictionaryExtensions
{
public static TValue GetOrAdd<TKey, TValue>(
this ConcurrentDictionary<TKey, Lazy<TValue>> #this,
TKey key, Func<TKey, TValue> valueFactory
)
{
return #this.GetOrAdd(key,
(k) => new Lazy<TValue>(() => valueFactory(k))
).Value;
}
public static TValue AddOrUpdate<TKey, TValue>(
this ConcurrentDictionary<TKey, Lazy<TValue>> #this,
TKey key, Func<TKey, TValue> addValueFactory,
Func<TKey, TValue, TValue> updateValueFactory
)
{
return #this.AddOrUpdate(key,
(k) => new Lazy<TValue>(() => addValueFactory(k)),
(k, currentValue) => new Lazy<TValue>(
() => updateValueFactory(k, currentValue.Value)
)
).Value;
}
public static bool TryGetValue<TKey, TValue>(
this ConcurrentDictionary<TKey, Lazy<TValue>> #this,
TKey key, out TValue value
)
{
value = default(TValue);
var result = #this.TryGetValue(key, out Lazy<TValue> v);
if (result) value = v.Value;
return result;
}
// this overload may not make sense to use when you want to avoid
// the construction of the value when it isn't needed
public static bool TryAdd<TKey, TValue>(
this ConcurrentDictionary<TKey, Lazy<TValue>> #this,
TKey key, TValue value
)
{
return #this.TryAdd(key, new Lazy<TValue>(() => value));
}
public static bool TryAdd<TKey, TValue>(
this ConcurrentDictionary<TKey, Lazy<TValue>> #this,
TKey key, Func<TKey, TValue> valueFactory
)
{
return #this.TryAdd(key,
new Lazy<TValue>(() => valueFactory(key))
);
}
public static bool TryRemove<TKey, TValue>(
this ConcurrentDictionary<TKey, Lazy<TValue>> #this,
TKey key, out TValue value
)
{
value = default(TValue);
if (#this.TryRemove(key, out Lazy<TValue> v))
{
value = v.Value;
return true;
}
return false;
}
public static bool TryUpdate<TKey, TValue>(
this ConcurrentDictionary<TKey, Lazy<TValue>> #this,
TKey key, Func<TKey, TValue, TValue> updateValueFactory
)
{
if (!#this.TryGetValue(key, out Lazy<TValue> existingValue))
return false;
return #this.TryUpdate(key,
new Lazy<TValue>(
() => updateValueFactory(key, existingValue.Value)
),
existingValue
);
}
}
This is not uncommon with Non-Blocking Algorithms. They essentially test for a condition confirming there is no contention using Interlock.CompareExchange. They loop around though until the CAS succeeds. Have a look at ConcurrentQueue page (4) as a good intro to Non-Blocking Algorithms
Short answer is no, it's the nature of the beast that it will require multiple attempts to add to the collection under contention.
Other than using the other overload of passing a value, you'd need to protect against multiple calls inside your value factory, perhaps using a double lock / memory barrier.
I've created this extension method
public static void AddIfNullCreate<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, TValue value)
{
if (dictionary == null)
{
dictionary = new Dictionary<TKey, TValue>();
}
dictionary.Add(key, value);
}
But when I use it
public void DictionaryTest()
{
IDictionary<int, string> d = GetD();
d.AddIfNullCreate(1,"ss");
}
private IDictionary<int, string> GetD()
{
return null;
}
After calling AddIfNullCreate is d null. Why is that so ?
Just like any other method, a change to the parameter doesn't change the caller's argument unless it's a ref parameter (which it can't be for an extension method first parameter). The argument is passed by value, even if that value is a reference.
One option is to return the dictionary too:
public static IDictionary<TKey, TValue> AddIfNullCreate<TKey, TValue>
(this IDictionary<TKey, TValue> dictionary, TKey key, TValue value)
{
if (dictionary == null)
{
dictionary = new Dictionary<TKey, TValue>();
}
dictionary.Add(key, value);
return dictionary;
}
Then:
d = d.AddIfNullCreate(1, "ss");
However, I'm not sure I'd really do that. I think I'd just conditionally create the dictionary in the method itself:
public void DictionaryTest()
{
IDictionary<int, string> d = GetD() ?? new Dictionary<int, string>();
d[1] = "ss";
}
If this was done as a normal method you'd need to pass the dictionary as a ref parameter so that the memory pointer to the newly created object is set correctly. I don't think you'll be allowed to specify the first parameter of an extension method as a ref parameter though.
Lot of times I have to create a Dictionary<KeyType, List<ValueType>>
Before I can start using the dictionary I have to first verify that List has been created for that key.
//Can i remove these two lines?
if(!dict.ContainsKey(key))
dict[key]= new List<ValueType>;
//now use the key
dict[key].Add(value);
I know its only "2 lines" of code but it annoys me and I think it can be removed.
I can extend dictionary in someway but before I do it, I want to know if someone has found a clever way to remove the above if statement.
Basically i want to create a Dictionary<KeyType, Collection<ValueType>> and start using it right away like dict[key].Add(value).
You could create something like Google Java Collection's Multimap... or you could just add an extension method like this:
public static void AddValue<TKey, TValue>
(this IDictionary<TKey, List<TValue>> dictionary, TKey key, TValue value)
{
List<TValue> values;
if (!dictionary.TryGetValue(key, out values))
{
values = new List<TValue>();
dictionary.Add(key, values);
}
values.Add(value);
}
As Bevan says, Lookup can help as well - but you can only create one with the ToLookup method, and you can't modify it afterwards. In many cases that's a thoroughly good thing, but if you need a mutable map then you'll ned something like the above.
Have a look at the LookUp class introduced with Linq in .NET 3.5 - it might be just what you're looking for: a Dictionary like class that supports multiple items per key.
Perhaps the only significant downside is that you have to have all your elements available in one batch, as LookUp is immutable.
The ConcurrentDictionary<T,K>.GetOrAdd method is amazingly useful.
private ConcurrentDictionary<string, ICollection<int>> _dictionary;
private static ICollection<int> CreateEmptyList(string dummyKey)
{
return new List<int>();
}
private void AddValue(string key, int value)
{
ICollection<int> values = _dictionary.GetOrAdd(key, CreateEmptyList);
values.Add(value);
}
Edit: Here's an example of how to implement the feature as an extension method for IDictionary<T,K> (C# 3):
Note that IDictionary<TKey, TValue> is generally not thread safe, so if you want thread safety with this extension method you'll have to manually implement it just as for other operations.
public static TValue GetOrAdd<TKey, TValue>(
this IDictionary<TKey, TValue> dictionary,
TKey key,
Func<TKey, TValue> valueFactory)
{
TValue value;
if (!dictionary.TryGetValue(key, out value))
{
value = valueFactory(key);
dictionary.Add(key, value);
}
return value;
}
To add to the answers, you can also add a more general extension which accepts a delegate for instantiation:
public static TValue GetOrCreate<TKey, TValue>
(this IDictionary<TKey, TValue> dict,
TKey key,
Func<TKey, TValue> getValue)
{
TValue value;
if (!dict.TryGetValue(key, out value))
{
dict.Add(key, getValue(key));
}
return value;
}
and then you can provide whatever instantiation method you like:
Dictionary<int, string> dict = new Dictionary<int, string>();
string result = dict.GetOrCreate(5, i => i.ToString());
The indexer into Dictionary throws an exception if the key is missing. Is there an implementation of IDictionary that instead will return default(T)?
I know about the TryGetValue() method, but that's impossible to use with LINQ.
Would this efficiently do what I need?:
myDict.FirstOrDefault(a => a.Key == someKeyKalue);
I don't think it will as I think it will iterate the keys instead of using a Hash lookup.
Indeed, that won't be efficient at all.
As per comments, in .Net Core 2+ / NetStandard 2.1+ / Net 5, MS added the extension method GetValueOrDefault()
For earlier versions you can write the extension method yourself:
public static TValue GetValueOrDefault<TKey,TValue>
(this IDictionary<TKey, TValue> dictionary, TKey key)
{
TValue ret;
// Ignore return value
dictionary.TryGetValue(key, out ret);
return ret;
}
Or with C# 7.1:
public static TValue GetValueOrDefault<TKey,TValue>
(this IDictionary<TKey, TValue> dictionary, TKey key) =>
dictionary.TryGetValue(key, out var ret) ? ret : default;
That uses:
An expression-bodied method (C# 6)
An out variable (C# 7.0)
A default literal (C# 7.1)
If you're using .NET Core 2 or above (C# 7.x), the CollectionExtensions class is introduced and you can use the GetValueOrDefault method to get default value if key is not there in a dictionary.
Dictionary<string, string> colorData = new Dictionary<string, string>();
string color = colorData.GetValueOrDefault("colorId", string.Empty);
Carrying these extension methods can help..
public static V GetValueOrDefault<K, V>(this IDictionary<K, V> dict, K key)
{
return dict.GetValueOrDefault(key, default(V));
}
public static V GetValueOrDefault<K, V>(this IDictionary<K, V> dict, K key, V defVal)
{
return dict.GetValueOrDefault(key, () => defVal);
}
public static V GetValueOrDefault<K, V>(this IDictionary<K, V> dict, K key, Func<V> defValSelector)
{
V value;
return dict.TryGetValue(key, out value) ? value : defValSelector();
}
Collections.Specialized.StringDictionary provides a non-exception result when looking up a missing key's value. It is also case-insensitive by default.
Caveats
It is only valid for its specialized uses, and — being designed before generics — it doesn't have a very good enumerator if you need to review the whole collection.
If you're using .Net Core, you can use the CollectionExtensions.GetValueOrDefault method. This is the same as the implementation provided in the accepted answer.
public static TValue GetValueOrDefault<TKey,TValue> (
this System.Collections.Generic.IReadOnlyDictionary<TKey,TValue> dictionary,
TKey key);
public class DefaultIndexerDictionary<TKey, TValue> : IDictionary<TKey, TValue>
{
private IDictionary<TKey, TValue> _dict = new Dictionary<TKey, TValue>();
public TValue this[TKey key]
{
get
{
TValue val;
if (!TryGetValue(key, out val))
return default(TValue);
return val;
}
set { _dict[key] = value; }
}
public ICollection<TKey> Keys => _dict.Keys;
public ICollection<TValue> Values => _dict.Values;
public int Count => _dict.Count;
public bool IsReadOnly => _dict.IsReadOnly;
public void Add(TKey key, TValue value)
{
_dict.Add(key, value);
}
public void Add(KeyValuePair<TKey, TValue> item)
{
_dict.Add(item);
}
public void Clear()
{
_dict.Clear();
}
public bool Contains(KeyValuePair<TKey, TValue> item)
{
return _dict.Contains(item);
}
public bool ContainsKey(TKey key)
{
return _dict.ContainsKey(key);
}
public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
{
_dict.CopyTo(array, arrayIndex);
}
public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
{
return _dict.GetEnumerator();
}
public bool Remove(TKey key)
{
return _dict.Remove(key);
}
public bool Remove(KeyValuePair<TKey, TValue> item)
{
return _dict.Remove(item);
}
public bool TryGetValue(TKey key, out TValue value)
{
return _dict.TryGetValue(key, out value);
}
IEnumerator IEnumerable.GetEnumerator()
{
return _dict.GetEnumerator();
}
}
One could define an interface for the key-lookup function of a dictionary. I'd probably define it as something like:
Interface IKeyLookup(Of Out TValue)
Function Contains(Key As Object)
Function GetValueIfExists(Key As Object) As TValue
Function GetValueIfExists(Key As Object, ByRef Succeeded As Boolean) As TValue
End Interface
Interface IKeyLookup(Of In TKey, Out TValue)
Inherits IKeyLookup(Of Out TValue)
Function Contains(Key As TKey)
Function GetValue(Key As TKey) As TValue
Function GetValueIfExists(Key As TKey) As TValue
Function GetValueIfExists(Key As TKey, ByRef Succeeded As Boolean) As TValue
End Interface
The version with non-generic keys would allow code that was using code using non-structure key types to allow for arbitrary key variance, which would not be possible with a generic type parameter. One should not be allowed to use a mutable Dictionary(Of Cat, String) as a mutable Dictionary(Of Animal, String) since the latter would allow SomeDictionaryOfCat.Add(FionaTheFish, "Fiona"). But there's nothing wrong with using a mutable Dictionary(Of Cat, String) as an immutable Dictionary(Of Animal, String), since SomeDictionaryOfCat.Contains(FionaTheFish) should be considered a perfectly well-formed expression (it should return false, without having to search the dictionary, for anything that isn't of type Cat).
Unfortunately, the only way one will be able to actually use such an interface is if one wraps a Dictionary object in a class which implements the interface. Depending upon what you're doing, however, such an interface and the variance it allows might make it worth the effort.
If you are using ASP.NET MVC, you could leverage the RouteValueDictionary class that do the job.
public object this[string key]
{
get
{
object obj;
this.TryGetValue(key, out obj);
return obj;
}
set
{
this._dictionary[key] = value;
}
}
I used encapsulation to create an IDictionary with behavior very similar to an STL map, for those of you who are familiar with c++. For those who aren't:
indexer get {} in SafeDictionary below returns the default value if a key is not present, and adds that key to the dictionary with a default value. This is often the desired behavior, as you're looking up items that will appear eventually or have a good chance of appearing.
method Add(TK key, TV val) behaves as an AddOrUpdate method, replacing the value present if it exists instead of throwing. I don't see why m$ doesn't have an AddOrUpdate method and thinks throwing errors in very common scenarios is a good idea.
TL/DR - SafeDictionary is written so as to never throw exceptions under any circumstances, other than perverse scenarios, such as the computer being out of memory (or on fire). It does this by replacing Add with AddOrUpdate behavior and returning default instead of throwing NotFoundException from the indexer.
Here's the code:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
public class SafeDictionary<TK, TD>: IDictionary<TK, TD> {
Dictionary<TK, TD> _underlying = new Dictionary<TK, TD>();
public ICollection<TK> Keys => _underlying.Keys;
public ICollection<TD> Values => _underlying.Values;
public int Count => _underlying.Count;
public bool IsReadOnly => false;
public TD this[TK index] {
get {
TD data;
if (_underlying.TryGetValue(index, out data)) {
return data;
}
_underlying[index] = default(TD);
return default(TD);
}
set {
_underlying[index] = value;
}
}
public void CopyTo(KeyValuePair<TK, TD>[] array, int arrayIndex) {
Array.Copy(_underlying.ToArray(), 0, array, arrayIndex,
Math.Min(array.Length - arrayIndex, _underlying.Count));
}
public void Add(TK key, TD value) {
_underlying[key] = value;
}
public void Add(KeyValuePair<TK, TD> item) {
_underlying[item.Key] = item.Value;
}
public void Clear() {
_underlying.Clear();
}
public bool Contains(KeyValuePair<TK, TD> item) {
return _underlying.Contains(item);
}
public bool ContainsKey(TK key) {
return _underlying.ContainsKey(key);
}
public IEnumerator<KeyValuePair<TK, TD>> GetEnumerator() {
return _underlying.GetEnumerator();
}
public bool Remove(TK key) {
return _underlying.Remove(key);
}
public bool Remove(KeyValuePair<TK, TD> item) {
return _underlying.Remove(item.Key);
}
public bool TryGetValue(TK key, out TD value) {
return _underlying.TryGetValue(key, out value);
}
IEnumerator IEnumerable.GetEnumerator() {
return _underlying.GetEnumerator();
}
}
It could be a one-liner to check TryGetValue and return default value if it is false.
Dictionary<string, int> myDic = new Dictionary<string, int>() { { "One", 1 }, { "Four", 4} };
string myKey = "One"
int value = myDic.TryGetValue(myKey, out value) ? value : 100;
myKey = "One" => value = 1
myKey = "two" => value = 100
myKey = "Four" => value = 4
Try it online
Since .NET core 2.0 you can use:
myDict.GetValueOrDefault(someKeyKalue)
What about this one-liner that checks whether a key is present using ContainsKey and then returns either the normally retreived value or the default value using the conditional operator?
var myValue = myDictionary.ContainsKey(myKey) ? myDictionary[myKey] : myDefaultValue;
No need to implement a new Dictionary class that supports default values, simply replace your lookup statements with the short line above.
This question helped to confirm that the TryGetValue plays the FirstOrDefault role here.
One interesting C# 7 feature I would like to mention is the out variables feature, and if you add the null-conditional operator from C# 6 to the equation your code could be much more simple with no need of extra extension methods.
var dic = new Dictionary<string, MyClass>();
dic.TryGetValue("Test", out var item);
item?.DoSomething();
The downside of this is that you can't do everything inline like this;
dic.TryGetValue("Test", out var item)?.DoSomething();
If we'd need/want to do this we should code one extension method like Jon's.
Here is a version of #JonSkeet's for the world of C# 7.1 that also allows for an optional default to be passed in:
public static TV GetValueOrDefault<TK, TV>(this IDictionary<TK, TV> dict, TK key, TV defaultValue = default) => dict.TryGetValue(key, out TV value) ? value : defaultValue;
It may be more efficient to have two functions to optimize the case where you want to return default(TV):
public static TV GetValueOrDefault<TK, TV>(this IDictionary<TK, TV> dict, TK key, TV defaultValue) => dict.TryGetValue(key, out TV value) ? value : defaultValue;
public static TV GetValueOrDefault2<TK, TV>(this IDictionary<TK, TV> dict, TK key) {
dict.TryGetValue(key, out TV value);
return value;
}
Unfortunately C# doesn't (yet?) have a comma operator (or the C# 6 proposed semicolon operator) so you have to have an actual function body (gasp!) for one of the overloads.
Modern Answer
As of .NET Core 2.0, there is a built-in extension method with 2 overloads:
TValue GetValueOrDefault<TKey,TValue>(TKey)
TValue GetValueOrDefault<TKey,TValue>(TKey, TValue)
Usage:
var dict = new Dictionary<string, int>();
dict.GetValueOrDefault("foo"); // 0: the datatype's default
dict.GetValueOrDefault("foo", 2); // 2: the specified default
The first version returns null for nullable types, of course.
See the documentation for more detail.
In general I would support the answer from Jon Skeet, however I prefer an implementation where I can give the default value as parameter:
public static TValue GetValueOrDefault<TKey, TValue> (this IDictionary<TKey, TValue> dictionary, TKey key, TValue defaultValue)
{
if (dictionary.ContainsKey(key))
return dictionary[key];
else
return defaultValue;
}
No, because otherwise how would you know the difference when the key exists but stored a null value? That could be significant.