0

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?

SyncMaster
  • 9,754
  • 34
  • 94
  • 137
  • What is the actual requirement? Why are you trying to insert values of different types in the first place? Non-generic types are rarely used. `Dictionary` is used when you don't care about the order. `SortedDictionary` is used when you do. If you really don't care about the type you can use `object` as the value type but in this case you'd have to check the value type and cast every time you wanted to use it. That's what generics try to avoid in the first place – Panagiotis Kanavos Feb 24 '22 at 18:47
  • Mixing up types like this is *very* unusual though. If you explain the actual requirement, there's probably a better way to solve it – Panagiotis Kanavos Feb 24 '22 at 18:48
  • I have an existing system that uses the Hashtable. But the problem with the existing system is that the ordering is not maintained. I get different types of values from other systems which I want to store in a collection based on the insertion order. – SyncMaster Feb 24 '22 at 18:48
  • You definitely should read https://stackoverflow.com/questions/355060/c-sharp-vs-java-generics (or similar) if you are coming from Java. – Alexei Levenkov Feb 24 '22 at 18:51
  • use the structures that you referenced in the last line of your question. If you want to store any type then user 'object' as the type in the declaration – pm100 Feb 24 '22 at 18:52
  • I totally agree with you on mixing of datatypes. But I am constrained by the existing systems mixing up of Datatypes. If this is something I can't change, what is the cleanest way of solving my issue. – SyncMaster Feb 24 '22 at 18:52
  • Picking data type feels very opinion-based to me... `List>`, or `List>`, or `OrderedDictionary` or other non-generic lists, or LinkedList variants... with your requirements pretty much all imaginable data types would do... – Alexei Levenkov Feb 24 '22 at 18:53

0 Answers0