Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Dictionary Extensions: Define Useful Extensions to Play Safe

0.00/5 (No votes)
25 Jan 2012 1  
Useful extensions to play safe
if (searchCriteria.ContainsKey(key) &&
       !string.IsNullOrEmpty(searchCriteria[key]))
    searchTerm = searchCriteria[key];

Ever have a dictionary or similar data structure and your code has many repeated checks to pull the value when in reality you’d be happy with a default value like null or string.Empty? Well, consider the following extension to Dictionary:

public static class DictionaryExtensions
{
    public static TValue GetSafeValue<TKey, TValue>(this Dictionary<TKey, 
                         TValue> dictionary, TKey key)
    {
        TValue result = default(TValue);
        dictionary.TryGetValue(index, out result);
        return result;
    }
}

Let’s say you do...

Dictionary bob = new Dictionary();
string safe = bob.GetSafeValue(100);
System.Diagnostics.Trace.WriteLine(safe);

...where safe defaults to “” as it hasn’t been added. Stop! I know what you’re going to say and I thought of that too. You can control the default value as well:

public static class DictionaryExtensions
{
    /// <summary>
    /// Gets the safe value associated with the specified key.
    /// </summary>
    /// <typeparam name="TKey">The type of the key.</typeparam>
    /// <typeparam name="TValue">The type of the value.</typeparam>
    /// <param name="dictionary">The dictionary.</param>
    /// <param name="key">The key of the value to get.</param>
    public static TValue GetSafeValue<TKey, TValue>(this Dictionary<TKey, 
                         TValue> dictionary, TKey key)
    {
        return dictionary.GetSafeValue(key, default(TValue));
    }

    /// <summary>
    /// Gets the safe value associated with the specified key.
    /// </summary>
    /// <typeparam name="TKey">The type of the key.</typeparam>
    /// <typeparam name="TValue">The type of the value.</typeparam>
    /// <param name="dictionary">The dictionary.</param>
    /// <param name="key">The key of the value to get.</param>
    /// <param name="defaultValue">The default value.</param>
    public static TValue GetSafeValue<TKey, TValue>(this Dictionary<TKey, 
           TValue> dictionary, TKey key, TValue defaultValue)
    {
        TValue result;
        if (key == null || !dictionary.TryGetValue(key, out result))
            result = defaultValue;
        return result;
    }
}

Let’s say you do...

Dictionary bob = new Dictionary();
string safe = bob.GetSafeValue(100, null);
System.Diagnostics.Trace.WriteLine(safe);

...where safe is null.

There’s obviously something wrong with me because I still think this stuff is cool.

I’m developing a nice little set of extensions at this point. Often, it seems like overkill to encapsulate handy functions like these in a class. I had started by deriving a class from Dictionary<TKey, TValue> but changed over to the above.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here