Community discussion forum

Multidimensional Hashtable....use generics?

  • 2 months ago

    Hi,

     I currently have many nested hashtables and find it annoying to have to explicitly cast each object as you go through the levels of each nested hashtable. Is there a way to avoid having to do something like:

    (cast type)(cast type)(cast type) hashtable[key1][key2][key3]....etc

    I believe "generics" may be involved...but I dont really know too much about any of that. If "generics" is the way to go, could someone please help me in finding the fastest way to convert from my nested hashtable format to the "generic" method (whatever that may be).

     THANKS!

  • Advertisement

    Simply the fastest line-level profiler for .NET ever

    “The low overhead means it has minimal impact on the execution of my program”
    Mark Everest, Development Team Leader, Renault F1 Team Ltd.

    Try out the new ANTS Profiler 4 for yourself. Download your 14-day trial now

  • 2 months ago

    Hi the_ro_show,

    You can use 'Dictionary<>' class, MSDN says: "Retrieving a value by using its key is very fast, close to O(1), because the Dictionary class is implemented as a hash table."

    Code :

    Dictionary<string, Dictionary<string, string>> dic = new Dictionary<string, Dictionary<string, string>>();

    dic["Key1"] = new Dictionary<string, string>();
    dic["Key1"]["Subkey1"] = "SubValue1";

    MessageBox.Show(dic["Key1"]["Subkey1"]); // shows "SubValue1"

Post a reply

Enter your message below

Sign in or Join us (it's free).