views:

709

answers:

7

Hi,

What's the best way to keep a collection-object (List in example) in a Key/Value situation, where the key is a ID and the value is a collection of a type T?

Is this the only option or is there a better solution/another collection for this in .NET 3.5?

var x = new Dictionary<int, List<type>>();
A: 

I have no idea if this is what you need but I'll have chop at it.

public Dictionary<int,List<T>> myFunction<T>()
{
    var returnvalue = new Dictionary<int,List<T>>();
    //Do some stuff with the collection.
    return returnvalue;
}

which you can then call

public void Main()
{
  var functionreturn = myFunction<String>();
}

I'm not sure if this will help you or not but it may help you reword your question.

NOTE: The above is air code and is untested.

Nathan W
-1 for air code. Better to test then post than to provide something quickly.
Lucas B
A: 

Thanks Nathan W,

Not really an anwer on my question, but my question was not clearly enough. So I rewrite my question. :)

A: 

I don't think there is anything within the framework, but I think there is a MultiDictionary collection in the PowerCollections library. You could try that.

Martinho Fernandes
Thanks. I'll have a look.
A: 

I think you should probably write your wrapper class according to your needs. I mean, if you need to store a dictionary of pre-made lists, Dictionary<int, List<type>> should be fine as long as it's only a private property. You should not expose it though publicly as obviously it exposes too much information and you cannot cast it to IDictionary<int, IList<T>> or something similar because of the lack of covariance.

Your best bet would be something like that:

class MyWrapper<T>()
{
    private Dictionary<int, List<T>> dictionary { get; set; }
    public MyWrapper() { dictionary = new Dictionary<int, List<T>>(); }

    // Adds a new item to the collection
    public void Add(int key, T item)
    {
       List<T> list = null;
       if (!dictionary.TryGetValue(key, out list))
       {
          // If dictionary does not contain the key, we need to create a new list
          list = new List<T>();
          dictionary.Add(key, list);
       }
       list.Add(item);
    }

    public IEnumerable<T> this[int key]     
    {
       get
       {
           List<T> list = null;
           // We just return an empty list if the key is not found
           if (!dictionary.TryGetValue(key, out list)) return new List<T>();
           else return list;
       }
    }
}

Obviously, your needs might be different, you might need to implement a couple of interfaces, etc., but this is the general idea.

DrJokepu
Thank you for your advice and code.
+2  A: 

This is a good solution and will work quite well - you are effectively using a dictionary object of { key = int, value = 4 byte reference }.

When you retrieve a value by the key you will get back the reference to the List<T> on the heap and be able to use it. This will be a very efficent and compact solution to your apparent problem.

Andrew Hare
A: 

If your ID is a member of your type, you might consider implementing a System.Collections.ObjectModel.KeyedCollection<TKey, TItem>

Joel Coehoorn
A: 

Are you are looking for a multi-valued dictionary, i.e. a keyed collection, where each key can have multiple values? PowerCollections has such a MultiDictionary.

oefe