StringDictionary 作为常见的静态列表?
我想做的是拥有一个项目明智的静态字典,这样我就可以从代码中的任何位置访问单个列表。
到目前为止,我想出了这个解决方案,一个具有公共属性的公共静态类:
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;
}
}
}
我以这种方式使用(我用它来替换 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";
}
但我不知道这是否是一个有效的解决方案,和/或是否有其他(更好)的方法来做到这一点......有人可以给出提示吗?
预先感谢,安德里亚。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可能不希望每次访问该属性时都重新创建该列表。 将构造移动到静态构造函数中:
您应该知道,返回的字典不是只读的。 因此此类的客户端可以修改集合(例如添加/删除一些条目)。
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).
每次在
Domains
上执行 get 操作时,您的字典都会重新加载。配置一个静态构造函数,该构造函数将加载
list
实例字典的只读静态副本并将其存储在内存中。Your dictionary is going to be reloaded every time you do a get on
Domains
Configure a static constructor that will load and stash a readonly static copy of the
list
instance dictionary in memory.考虑使用单例模式:
http://en.wikipedia.org/wiki/Singleton_pattern
这不是必须的,因为每个程序都有自己的特殊需求,我只建议您阅读此模式并再次重新考虑您的设计。
如果您的程序是多线程的,您可能想检查 StringDictionary 是否是线程安全的,如果它不是线程安全的(并且您的程序是多线程的),您应该向您的类添加一些同步机制。
Consider using the Singleton pattern instead:
http://en.wikipedia.org/wiki/Singleton_pattern
It's not a must since every program has its own special needs , I only recommand you read about this pattern and rethink about your design again.
If your program is multithreaded you might wanna check whether StringDictionary is thread safe , if it isn't thread safe (and your program is multithreaded), you should add some synchronization mechanism to your class.
@Jeff Fritz 的回答是正确的,但我只是想添加更多有关多线程之间该字段的共享访问的信息。 StringDictionary 仅对于读取操作是线程安全的,对于写入操作不是线程安全的,因此您需要小心,不要在没有适当锁定的情况下修改此集合,并且您还需要在以下情况下锁定该集合:迭代它。
迭代:
在静态构造函数外部添加域:
一般来说,在通过静态字段共享非只读集合时我会非常小心,以免该字段被修改。 您可能希望将 StringDictionary 改为 ReadOnlyCollection 并将字段标记为“只读”:
您不会获得 O(1) 查找,但我假设此集合将非常小,所以无论如何都没关系。
@Jeff Fritz is correct in his answer, but I just wanted to add a little more about the shared access of this field among multiple threads. StringDictionary is only thread-safe for read operations, and not write operations, so you'll want to be careful not to modify this collection without proper locking, and you'll also want to lock the collection when iterating over it.
To iterate:
To add a domain outside the static constructor:
In general, I'd be very careful when sharing a non-readonly collection via a static field so that the field isn't modified. You may want to make StringDictionary a ReadOnlyCollection instead and mark the field "readonly":
You won't get O(1) lookups, but I'm assuming this collection will be very small, so that won't matter anyway.