c# - 为什么 IDictionary<TKey, TValue> 扩展 ICollection<KeyValuePair<TKey, TValue>>?

标签 c# .net dictionary collections readonly

我正在尝试创建自定义 ReadOnlyDictionary<TKey, TValue>对于.NET 4.0。方法是保持私有(private) Dictionary<TKey, TValue>对象以及标志以确定是否允许添加/删除和项目分配。

这很好用,但我想实现 IDictionary<TKey, TValue>接口(interface)的完整性。但是,我注意到它扩展了 ICollection<KeyValuePair<TKey, TValue>>而其属性或方法均未出现在 Dictionary<TKey, TValue> 中类(class)。这怎么可能?如果实现了接口(interface),为什么是ICollection成员不暴露?

此外,为什么 Dictionary类需要实现 ICollection首先?

这是一个粗略的实现:

public sealed class ReadOnlyDictionary<TKey, TValue>:
    //IDictionary<TKey, TValue>,
    IEnumerable<KeyValuePair<TKey, TValue>>
{
    #region Members.

    public bool AllowListEdit { get; private set; }
    public bool AllowItemEdit { get; private set; }
    private Dictionary<TKey, TValue> Dictionary { get; set; }

    #endregion Members.

    #region Constructors.

    public ReadOnlyDictionary (bool allowListEdit, bool allowItemEdit) { this.AllowListEdit = allowListEdit; this.AllowItemEdit = allowItemEdit; this.Dictionary = new Dictionary<TKey, TValue>(); }
    public ReadOnlyDictionary (IEqualityComparer<TKey> comparer, bool allowListEdit, bool allowItemEdit) { this.AllowListEdit = allowListEdit; this.AllowItemEdit = allowItemEdit; this.Dictionary = new Dictionary<TKey, TValue>(comparer); }
    public ReadOnlyDictionary (IDictionary<TKey, TValue> dictionary, bool allowListEdit = false, bool allowItemEdit = false) : this(allowListEdit, allowItemEdit) { foreach (var pair in dictionary) { this.Dictionary.Add(pair.Key, pair.Value); } }
    public ReadOnlyDictionary (IDictionary<TKey, TValue> dictionary, IEqualityComparer<TKey> comparer, bool allowListEdit = false, bool allowItemEdit = false) : this(comparer, allowListEdit, allowItemEdit) { foreach (var pair in dictionary) { this.Dictionary.Add(pair.Key, pair.Value); } }

    #endregion Constructors.

    #region Properties.

    public int Count { get { return (this.Dictionary.Count); } }
    public IEqualityComparer<TKey> Comparer { get { return (this.Dictionary.Comparer); } }

    #endregion Properties.

    #region Methods.

    private void ThrowItemReadOnlyException () { if (this.AllowListEdit) { throw (new NotSupportedException("This collection does not allow editing items.")); } }
    private void ThrowListReadOnlyException () { if (this.AllowItemEdit) { throw (new NotSupportedException("This collection does not allow adding and removing items.")); } }
    public bool ContainsValue (TValue value) { return (this.Dictionary.ContainsValue(value)); }
    public void Clear () { this.ThrowListReadOnlyException(); this.Dictionary.Clear(); }

    #endregion Methods.

    #region Interface Implementation: IEnumerable<KeyValuePair<TKey, TValue>>.

    IEnumerator IEnumerable.GetEnumerator () { return (this.Dictionary.GetEnumerator()); }
    public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator () { return (this.Dictionary.GetEnumerator()); }

    #endregion Interface Implementation: IEnumerable<KeyValuePair<TKey, TValue>>.

    #region Interface Implementation: ICollection<KeyValuePair<TKey, TValue>>.

    //public int Count { get { return (this.Dictionary.Count); } }
    //public bool IsReadOnly { get { return (this.AllowListEdit); } }

    //public bool Contains (KeyValuePair<TKey, TValue> item) { throw (new NotImplementedException()); }
    //public void Clear () { throw (new NotImplementedException()); }
    //public void Add (KeyValuePair<TKey, TValue> item) { throw (new NotImplementedException()); }
    //public void CopyTo (KeyValuePair<TKey, TValue> [] array, int arrayIndex) { throw (new NotImplementedException()); }
    //public bool Remove (KeyValuePair<TKey, TValue> item) { throw (new NotImplementedException()); }

    #endregion Interface Implementation: ICollection<KeyValuePair<TKey, TValue>>.

    #region Interface Implementation: IDictionary<TKey, TValue>.

    //====================================================================================================
    // Interface Implementation: IDictionary<TKey, TValue>.
    //====================================================================================================

    public Dictionary<TKey, TValue>.KeyCollection Keys { get { return (this.Dictionary.Keys); } }
    public Dictionary<TKey, TValue>.ValueCollection Values { get { return (this.Dictionary.Values); } }
    public TValue this [TKey key] { get { return (this.Dictionary [key]); } set { this.ThrowItemReadOnlyException(); this.Dictionary [key] = value; } }

    public void Add (TKey key, TValue value) { this.ThrowListReadOnlyException(); this.Dictionary.Add(key, value); }
    public bool ContainsKey (TKey key) { return (this.Dictionary.ContainsKey(key)); }
    public bool Remove (TKey key) { this.ThrowListReadOnlyException(); return (this.Dictionary.Remove(key)); }
    public bool TryGetValue (TKey key, out TValue value) { return (this.Dictionary.TryGetValue(key, out value)); }

    #endregion Interface Implementation: IDictionary<TKey, TValue>.
}

最佳答案

Dictionary<TKey, TValue>实现 ICollection<KeyValuePair<TKey, TValue>>接口(interface)显式

正如您在 MSDN page for Dictionary 上看到的那样,这些方法列在“显式接口(interface)实现”下。

显式实现接口(interface)意味着这些方法将无法通过具体类型使用。您必须将字典转换为 ICollection<T>能够给他们打电话。

Dictionary<int, int> dict = new Dictionary<int, int>();
bool sync = dict.IsSynchronized; // not allowed

ICollection<KeyValuePair<int, int>> dict = new Dictionary<int, int>();
bool sync = dict.IsSynchronized; // allowed

关于显式接口(interface)实现的更多信息:http://msdn.microsoft.com/en-us/library/aa288461(v=vs.71).aspx

关于c# - 为什么 IDictionary<TKey, TValue> 扩展 ICollection<KeyValuePair<TKey, TValue>>?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21405415/

相关文章:

python - 如何将 python 字符串数据转换为列表或字典?

python - 如何在 Perl 中实现多键哈希作为 Python 中的嵌套字典?

c# - Web 服务 (.net) 返回 html - 限制?

c# - Jquery 隐藏/显示 svg 图像

c# - 找到与请求 (GET) 匹配的多个操作

c# - 变量声明应该总是放在循环之外吗?

c# - ORA-00604:递归SQL级别1发生错误ORA-01882:找不到时区区域

c# - EF4 代码首先与外部实体建立多对多关系

c# - 控制台应用程序和单元测试方法之间的不同垃圾收集行为

Python - hash() 和 dict