How to define & access a nested dictionary data across multiple namespace C# -


this question has answer here:

i have following data, want define in elegant way , fast access.

dictionary<string, string> mydictionary =  new dictionary<string, string> {    {"a", "1"},    {"b"  "2"} }; 

now "1" , "2" defined in 2 different modules x , y. i'm thinking of nested dictionary here. i'm looking elegant way define.

my thinking:

    //filea -  namespace1     dictionary<string, string> dca = new dictionary<string, string>     {         {"loc1", "addr1"},         {"loc2", "addr2"}     };      //fileb -  namespace1     dictionary<string, string> dcb = new dictionary<string, string>     {         {"loc3", "add3"},         {"loc4", "add4"}     };      //filex - namespace 2     static dictionary<string, string> dc1 = new dictionary<string, dictionary<string, string>>     {         {"loc1", dca.getvalue("loc1"},         {"loc2", dca.getvalue("loc2"},         {"loc3", dca.getvalue("loc3"},         {"loc4", dca.getvalue("loc4"},     };      string mystring;     string key = "loc1";     if (!dc1.trygetvalue(key, out mystring))     {         throw new invaliddataexception("can't find addr loc");     }     console.writeline("mystring : {0}", mystring)      //expected output      mystring : addr1 

yes want combine 2 dictionary new dictionary. problem can access value of new dictionary dca.getvalue("loc1"}. trying see if there better solution or data struct i'm not thinking @ all.

you can 2 options.

option 1. //filex - namespace 2

dictionary<string, string> dc1 = dca;  foreach (var item in dcb) {      dc1[item.key] = item.value;      } 

option 2.

dictionary<string, string> dc1 = new dictionary<string,string>();  foreach (var item in dca) {      dc1[item.key] = item.value; } foreach (var item in dcb) {     dc1[item.key] = item.value; } 

option 1 faster option 2. because in option 1 has 1 loop , option copy 1st dictionary during init.


Comments

Popular posts from this blog

java - Static nested class instance -

c# - Bluetooth LE CanUpdate Characteristic property -

JavaScript - Replace variable from string in all occurrences -