Why you should use IDictionary, IList, etc

Summary

When returning objects from a method try to use IList, IDictionary, etc instead of List and Dictionary. This is especially important when the method is inside a class library which you distribute.

Details

I had to edit a method that was returning a Dictionary. Inside of which some complex work was being done to populate the dictionary. The changes needed were going to be much easier if I could use and return a ConcurrentDictionary. See here for what I was doing.

But because the method was returning a simple Dictionary it was not as straight forward as I hoped. More work needed to be done and I had the choice of:

1. Doing the processing in a more difficult way, not involving a `ConcurrentDictionary`.
2. Moving the entries from the new `ConcurrentDictionary` to a simple `Dictionary` just before calling `return`.
3. Changing the return type of the method (and the associated interface).

I went with option three, this happened to be the easiest, but it was also the best. If the code had initially been written to return IDictionary I would only have had to concern myself with required logic changes inside the method.

Here is a quick, but contrived, example of the flexibility gained by using IDictionary.

The class has a single public method which returns an IDictionary. Depending on the boolean value of getConcurrentDictionary it does its work using either as a simple Dictionary or a ConcurrentDictionary, but the caller does not need be aware of this. This gives great flexibility, the method GetDataDictionary can change its internal implementation and even the type of dictionary it returns without any negative impact on the caller.

The caller also has the flexibility to cast the IDictionary to a ConcurrentDictionary as needed.

 1    class GetDataWithInterfaceCollections : IGetDataWithInterfaceCollections
 2    {
 3        public IDictionary<int, int> GetDataDictionary(int start, int end, bool getConcurrentDictionary)
 4        {
 5            IDictionary<int, int> dictionaryToReturn;
 6
 7            if (getConcurrentDictionary)
 8            {
 9                dictionaryToReturn = GetConcurrentDictionary(start, end);
10            }
11            else
12            {
13                dictionaryToReturn = GetDictionary(start, end);
14            }
15
16            return dictionaryToReturn;
17        }
18
19        private IDictionary<int, int> GetConcurrentDictionary(int start, int end)
20        {
21            //ConcurrentDictionary offers a lot of features not available in Dictionary
22            ConcurrentDictionary<int, int> dictionary = new ConcurrentDictionary<int, int>();
23            for (int loop = start; loop <= end; loop++)
24            {
25                dictionary.AddOrUpdate(loop, loop* 10, (i, i1) => loop *  10 ); // AddOrUpdate is specific to a ConcurrentDictionary
26            }
27
28            return dictionary;
29        }
30
31        private IDictionary<int, int> GetDictionary(int start, int end)
32        {
33            IDictionary<int, int> dictionary = new Dictionary<int, int>();
34            for (int loop = start; loop <= end; loop++)
35            {
36                dictionary.Add(loop, loop *  10);
37            }
38
39            return dictionary;
40        }
41    }

Here is how to call the method shown above.

1            IDictionary<int, int> concurrentDictionary = _getDataWithInterfaceCollections.GetDataDictionary(1, 10, true);
2
3            IDictionary<int, int> simpleDictionary = _getDataWithInterfaceCollections.GetDataDictionary(11, 20, false);
4
5            ConcurrentDictionary<int, int> explicitConcurrentDictionary = _getDataWithInterfaceCollections.GetDataDictionary(21, 30, true) as ConcurrentDictionary<int, int>; 

Using the Watch window in Visual Studio we can see more info.

Dictionary Types

comments powered by Disqus

Related