In C#, I can have a Hashtable that accepts different data types for the values. But I want a datastructure that can maintain the insertion order.
I am coming to C# from Java background and I would like to know a clean way of solving it. I don't want the O(1) lookup complexity of an Hashtable.
Here is how my Hashtable would be.
public void foo() {
List<string> mylist1 = new List<string>(new string[] { "1", "2", "3" });
List<int> mylist2 = new List<int>(new int[] { 1, 2, 3 });
Hashtable hashTable = new Hashtable();
hashTable.Add("1", "One");
hashTable.Add("2", 2);
hashTable.Add("3", mylist1);
hashTable.Add("4", mylist2);
foreach(string key in hashTable.Keys)
{
Console.WriteLine(String.Format("{0}: {1}", key, hashTable[key]));
}
}
If I want to maintain the insertion order, List seems to be the right data structure. I am thinking of new List<Tuple<string, var>>
. But my values can be of any data type.
So I am looking for a data structure that can maintain insertion order and allow different datatypes for the values (like an Hashtable). How can I achieve this in C#?
Should I use a dictionary data structure that preserves the order instead?