C# 为什么IDictionary<;TKey,TValue>;扩展ICollection<;KeyValuePair<;TKey,TValue>>;?

C# 为什么IDictionary<;TKey,TValue>;扩展ICollection<;KeyValuePair<;TKey,TValue>>;?,c#,.net,dictionary,collections,readonly,C#,.net,Dictionary,Collections,Readonly,我正在尝试为.NET 4.0创建一个自定义的ReadOnlyDictionary。方法是保留一个私有的字典对象以及标志,以确定是否允许添加/删除和项目分配 这很好,但为了完整性,我想实现IDictionary接口。但是,我注意到它扩展了ICollection,而Dictionary类中没有出现它的属性或方法。这怎么可能?如果实现了接口,为什么不公开ICollection成员 此外,为什么Dictionary类首先需要实现ICollection 下面是一个粗略的实现: public sealed

我正在尝试为.NET 4.0创建一个自定义的
ReadOnlyDictionary
。方法是保留一个私有的
字典
对象以及标志,以确定是否允许添加/删除和项目分配

这很好,但为了完整性,我想实现
IDictionary
接口。但是,我注意到它扩展了
ICollection
,而
Dictionary
类中没有出现它的属性或方法。这怎么可能?如果实现了接口,为什么不公开
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>.
}
公共密封类只读字典:
//词典,
数不清
{
#区域成员。
public bool AllowListEdit{get;private set;}
public bool AllowItemEdit{get;private set;}
专用字典{get;set;}
#endregion成员。
#区域构造函数。
public ReadOnlyDictionary(bool allowListEdit,bool allowItemEdit){this.allowListEdit=allowListEdit;this.allowItemEdit=allowItemEdit;this.Dictionary=new Dictionary();}
公共只读词典(IEqualityComparer comparer comparer,bool allowListEdit,bool allowItemEdit){this.allowListEdit=allowListEdit;this.allowItemEdit=allowItemEdit;this.Dictionary=new Dictionary(comparer);}
public ReadOnlyDictionary(IDictionary dictionary,bool allowListEdit=false,bool allowitemdit=false):this(allowListEdit,allowitemdit){foreach(dictionary中的var对){this.dictionary.Add(pair.Key,pair.Value);}
公共只读字典(IDictionary dictionary,IEqualityComparer comparer comparer,bool allowListEdit=false,bool AllowWitemedit=false):这个(comparer,allowListEdit,AllowWitemedit){foreach(字典中的变量对){this.dictionary.Add(pair.Key,pair.Value);}
#endregion构造函数。
#区域属性。
公共整数计数{get{return(this.Dictionary.Count);}
公共IEqualityComparer比较器{get{return(this.Dictionary.Comparer);}
#endregion属性。
#区域方法。
私有void ThrowItemReadOnlyException(){if(this.AllowListEdit){throw(new NotSupportedException(“此集合不允许编辑项”);}
私有void ThrowListReadOnlyException(){if(this.allowitmeedit){throw(new NotSupportedException(“此集合不允许添加和删除项”);}
public bool ContainsValue(TValue value){return(this.Dictionary.ContainsValue(value));}
public void Clear(){this.ThrowListReadOnlyException();this.Dictionary.Clear();}
#端域方法。
#区域接口实现:IEnumerable。
IEnumerator IEnumerable.GetEnumerator(){return(this.Dictionary.GetEnumerator());}
公共IEnumerator GetEnumerator(){return(this.Dictionary.GetEnumerator());}
#endregion接口实现:IEnumerable。
#区域接口实现:ICollection。
//公共整数计数{get{return(this.Dictionary.Count);}
//公共bool是readonly{get{return(this.AllowListEdit);}
//public bool包含(KeyValuePair项){throw(new NotImplementedException());}
//public void Clear(){throw(new NotImplementedException());}
//public void Add(KeyValuePair项){throw(new NotImplementedException());}
//public void CopyTo(KeyValuePair[]数组,int-arrayIndex){throw(new-NotImplementedException());}
//public bool Remove(KeyValuePair项){throw(new NotImplementedException());}
#endregion接口实现:ICollection。
#区域接口实现:IDictionary。
//====================================================================================================
//接口实现:IDictionary。
//====================================================================================================
public Dictionary.KeyCollection密钥{get{return(this.Dictionary.Keys);}
public Dictionary.ValueCollection值{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,TValue){this.ThrowListReadOnlyException();this.Dictionary.Add(key,value);}
public bool ContainsKey(TKey){return(this.Dictionary.ContainsKey(key));}
public bool Remove(TKey key){this.ThrowListReadOnlyException();return(this.Dictionary.Remove(key));}
public bool TryGetValue(TKey,out TValue值){return(this.Dictionary.TryGetValue(key,out value));}
#endregion接口实现:IDictionary。
}
字典
显式地实现了
ICollection
接口

正如您在上所看到的,这些方法列在“显式接口实现”下

显式实现接口意味着这些方法将无法通过具体类型使用。您必须将字典强制转换为
i集合
,才能调用它们

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
Dictionary dict=new Dictionary();
bool sync=dict.IsSynchronized;//不准
ICollection dict=新字典();
bool sync=dict.IsSynchronized;//允许

有关显式接口实现的更多信息:

谢谢。我知道显式接口,但没有想到这一点。那么问题是,为什么字典要实现ICollection呢?IEnumerable已经负责迭代,不允许任意添加KayValuePairs,对吗?@RaheelKhan嗯,我也不认为实现
ICollection
有什么好处。。也许只是为了完整性和c