1 c#
我想让用户迭代我的字典而不修改它.
我考虑了两个解决方案:
1.ReadOnlyDictionary - 是否有可用的实现?
2.复制整个字典 - 复制它的最有效方法是什么?
谢谢
最简单的方法可能是实现你自己的集合,它是一个包含字典的包装器,类似于:
public class ReadOnlyDictionary<T, U>
{
private IDictionary<T, U> BackingStore;
public ReadOnlyDictionary<T, U>(IDictionary<T, U> baseDictionary) {
this.BackingStore = baseDictionary;
}
public U this[T index] { get { return BackingStore[index]; } }
// provide whatever other methods and/or interfaces you need
}
Run Code Online (Sandbox Code Playgroud)