如何公开私人字典< K,V>作为只读集合? [英] How do I expose a private dictionary<K, V> as a read-only collection?
问题描述
我怀疑没有快速回答这个问题,但无论如何我都会问。
我的班级有:
I suspect there is no quick answer to this, but I'll ask anyway.
I have a class that has:
private Dictionary<string,string> _records = new Dictionary<string,string>();
我想将此公开给另一个类但不希望这个其他类能够以任何方式修改字典 - 即我不希望它能够分配不同的字典,我不希望它能够修改其中的内容现有词典。
词典中的条目数量很少 - 从不超过低十 - 因此性能不是问题。
我不知道如何获得编译器版本,但我的目标是.NET 4.5.2。我似乎已经阅读了所有现代语法,但有一些例外,例如命名的元组字段,所以我认为这使我成为C#5(或者我可以说垃圾)?
这样做的最佳方法是什么?
顺便说一句,在这种特殊情况下,我对这两个类如何交互有相当大的灵活性。特别是,如果我只能返回字典中键的不可修改的 List< string>
,那就没问题了。然后我可以添加另一个函数来返回指定键的值。
我也可以放弃字典< string,string>
一共。
我尝试过:
我已经在这个主题上做了很多谷歌搜索,但似乎有一些相互矛盾的信息。
允许某种形式的私有迭代维护集合似乎是一个常见的要求,我很惊讶没有找到一个可接受的模式或一组最佳实践。
I would like to expose this to another class but don't want this other class to be able to modify the dictionary in any way - i.e. I don't want it to be able to assign a different dictionary and I don't want it to be able to modify the contents of the existing dictionary.
The number of entries in the dictionary is low - never more than the low tens - so performance is not an issue.
I'm not sure how to get the compiler version, but I am targeting .NET 4.5.2. I seem to have all modern syntax I read about with some exceptions, such as named tuple fields, so I think that puts me on C# 5 (or I could be talking rubbish)?
What is the best way of doing this?
Incidentally, in this particular case, I have a fair amount of flexibility about how these two classes interact. In particular, it would be fine if I could just return an unmodifiable List<string>
of the keys in the dictionary. Then I could just add another function to return the value for a specified key.
I could also ditch the Dictionary<string,string>
altogether.
What I have tried:
I have done quite a bit of Googling on this subject, but there just seems to be a tonne of contradictory information.
Allowing some form of iteration over a privately maintained collection seems like such a common requirement that I am surprised not to have found an accepted pattern or set of best practices for doing this.
推荐答案
参见Object.MemberwiseClone Method(System) [ ^ ]。
特别是,如果我可以返回一个不可修改的List< string>字典中的键。然后我可以添加另一个函数来返回指定键的值。
In particular, it would be fine if I could just return an unmodifiable List<string> of the keys in the dictionary. Then I could just add another function to return the value for a specified key.
这听起来像是要走的路,因为它会避免在每次访问时创建字典的副本。
我倾向于拥有容器类实现 IReadOnlyDictionary(TKey,TValue) [ ^ ]:
That sounds like the way to go, since it will avoid creating a copy of the dictionary on every access.
I'd be inclined to have the container class implement IReadOnlyDictionary(TKey, TValue)[^]:
public class Foo : IReadOnlyDictionary<string, string>
{
private readonly Dictionary<string,string> _records = new Dictionary<string,string>();
public int Count => _records.Count;
// The Dictionary.Keys and Dictionary.Values collections are read-only, so this is OK:
public IEnumerable<string> Keys => _records.Keys;
public IEnumerable<string> Values => _records.Values;
public string this[string key] => _records[key];
public bool ContainsKey(string key) => _records.ContainsKey(key);
public bool TryGetValue(string key, out string value) => _records.TryGetValue(key, out value);
public IEnumerator<KeyValuePair<string, string>> GetEnumerator() => _records.GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}
注意:您可能想要投出 _records
字段到 IReadOnlyDictionary< string,string>
并返回。但这不会阻止调用代码将其转换回 Dictionary< string,string>
并进行修改。
根据您的要求,您可能还需要查看不可变集合 [ ^ ]包。
NB: You might be tempted to just cast the _records
field to an IReadOnlyDictionary<string, string>
and return that. But that wouldn't prevent the calling code from casting it back to a Dictionary<string, string>
and modifying it.
Depending on your requirements, you might also want to take a look at the Immutable Collections[^] package.
>
这篇关于如何公开私人字典< K,V>作为只读集合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!