Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generic Key/Value pair collection in that preserves insertion order?

I'm looking for something like a Dictionary<K,V> however with a guarantee that it preserves insertion order. Since Dictionary is a hashtable, I do not think it does.

Is there a generic collection for this, or do I need to use one of the old .NET 1.1 collections?

like image 252
FlySwat Avatar asked Sep 08 '09 22:09

FlySwat


People also ask

What is key-value pair collection?

A key-value pair (KVP) is a set of two linked data items: a key, which is a unique identifier for some item of data, and the value, which is either the data that is identified or a pointer to the location of that data. Key-value pairs are frequently used in lookup tables, hash tables and configuration files.

Which method uses key-value pair?

Method 2: Using the map() method A map is a collection of elements where each element is stored as a key, value pair. The objects of map type can hold both objects and primitive values as either key or value. On traversing through the map object, it returns the key, value pair in the same order as inserted.

How do you create a key-value pair?

To add key-value pair in C# Dictionary, firstly declare a Dictionary. IDictionary<int, string> d = new Dictionary<int, string>(); Now, add elements with KeyValuePair. d.


1 Answers

There is not. However, System.Collections.Specialized.OrderedDictionary should solve most need for it.

EDIT: Another option is to turn this into a Generic. I haven't tested it but it compiles (C# 6) and should work. However, it will still have the same limitations that Ondrej Petrzilka mentions in comments below.

    public class OrderdDictionary<T, K>     {         public OrderedDictionary UnderlyingCollection { get; } = new OrderedDictionary();          public K this[T key]         {             get             {                 return (K)UnderlyingCollection[key];             }             set             {                 UnderlyingCollection[key] = value;             }         }          public K this[int index]         {             get             {                 return (K)UnderlyingCollection[index];             }             set             {                 UnderlyingCollection[index] = value;             }         }         public ICollection<T> Keys => UnderlyingCollection.Keys.OfType<T>().ToList();         public ICollection<K> Values => UnderlyingCollection.Values.OfType<K>().ToList();         public bool IsReadOnly => UnderlyingCollection.IsReadOnly;         public int Count => UnderlyingCollection.Count;         public IDictionaryEnumerator GetEnumerator() => UnderlyingCollection.GetEnumerator();         public void Insert(int index, T key, K value) => UnderlyingCollection.Insert(index, key, value);         public void RemoveAt(int index) => UnderlyingCollection.RemoveAt(index);         public bool Contains(T key) => UnderlyingCollection.Contains(key);         public void Add(T key, K value) => UnderlyingCollection.Add(key, value);         public void Clear() => UnderlyingCollection.Clear();         public void Remove(T key) => UnderlyingCollection.Remove(key);         public void CopyTo(Array array, int index) => UnderlyingCollection.CopyTo(array, index);     } 
like image 58
McAden Avatar answered Sep 29 '22 10:09

McAden