what I’m trying to do is have a project-wise static dictionary so I can access a single list from anywhere in my code.
Until now, I came up with this solution, a public static class with a public property:
public static class Common { public static StringDictionary Domains { get { StringDictionary list = new StringDictionary(); list.Add('212', 'Location A'); list.Add('555', 'Location B'); list.Add('747', 'Location C'); list.Add('000', 'Location D'); return list; } } }
That I use in this way (I use it to replace the content of a cell in a gridview):
if (Common.Domains.ContainsKey(e.Row.Cells[5].Text)) { e.Row.Cells[5].Text = Common.Domains[e.Row.Cells[5].Text]; } else { e.Row.Cells[5].Text = 'n/a'; }
But I don’t know if this is an efficient solution, and/or if there are other (better) ways to do this… Can somebody give a hint?
Thanks in advance, Andrea.
You probably don’t want to re-create the list every time the property is accessed. Move the construction into the static constructor:
And you should be aware, that the dictionary returned is not read-only. So a client of this class could modify the collection (e.g. add/remove some entries).