Utilisation of a Dictionary like class

94 Views Asked by At

For the purpose of XML serialisation I had to disband a Dictionary collection I was using. I wrote a very straightforward alternative which consists of 2 classes:

  • NameValueItem: contains Name (Key) and Value
  • NameValueCollection: derived from CollectionBase and maintains a collection of NameValueItem objects.

I've included some standard methods to help maintain the collection (Add, Contains and Remove). So just like most Dictionary types, the Name (or Key) is unique:

    public bool Contains(NameValueItem item)
    {
        foreach (NameValueItem lItem in List)
            if(lItem.Name.Equals(item.Name))
                return true;

        return false;
    }

Add uses this Contains method to determine whether to include a given item into the collection:

    public void Add(NameValueItem item)
    {
        if (!Contains(item))
            List.Add(item);
    }

As bog standard, straightforward and easy as this code appears it's proving to be a little sluggish. Is there anything that can be done to improve the performance of this? Or alternatives I could use?

I was considering creating a NameValueHashSet, which is derived from HashSet.

Optional...:

I had a question which I was going to ask in a separate thread, but I'll leave it up to you as to whether you'd like to address it or not.

I wanted to add 2 properties to the NameValueCollection, Names and Values, which return a List of strings from the Collection of NameValueItem objects. Instead I built them into methods GetNames() and GetValues(), as I have to build the collection (i.e. create a List (names/values), iterate over collection adding names/value to List and return List).

Is this a better alternative? In terms of good coding practise, performance, etc.? As my thoughts regarding properties has always been to have it as stripped back as possible, that only references, arithmetic, etc. should exist, with no layers of processes. If that is the case, then it should be built into a method. Thoughts?

1

There are 1 best solutions below

2
On

Perhaps you shouldn't try to rebuild what the framework already provides? Your implementation of a dictionary is going to perform poorly as it does not scale. The built in Dictionary<TKey, TValue> has O(1) access performance and for most insert and delete operations (unless there are collisions or the internal storage must be expanded).

You can extend the existing dictionary to provide XML serialization support; see this question and answers: Serialize Class containing Dictionary member

As for your second question - Dictionary already provides methods for getting an IEnumerable of the keys and values. This enumerates the keys and/or values as requested by the caller; that is delayed execution and is likely the preferred method over building a full List every time (which requires iterating through all the elements in the dictionary). If the caller wants a list then they just do dictionary.Values.ToList().