简体   繁体   English

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

[英]Why does IDictionary<TKey, TValue> extend ICollection<KeyValuePair<TKey, TValue>>?

I am trying to create a custom ReadOnlyDictionary<TKey, TValue> for .NET 4.0. 我正在尝试为.NET 4.0创建自定义ReadOnlyDictionary<TKey, TValue> The approach is to keep a private Dictionary<TKey, TValue> object as well as flags to determine whether adding/removing and item assignment is allowed. 方法是保留私有Dictionary<TKey, TValue>对象以及标志,以确定是否允许添加/删除和项目分配。

This works fine but I wanted to implement the IDictionary<TKey, TValue> interface for completeness. 这很好但我想实现IDictionary<TKey, TValue>接口的完整性。 However, I notice that it extends ICollection<KeyValuePair<TKey, TValue>> whereas none of its properties or methods appear in the Dictionary<TKey, TValue> class. 但是,我注意到它扩展了ICollection<KeyValuePair<TKey, TValue>>而它的属性或方法都没有出现在Dictionary<TKey, TValue>类中。 How is this possible? 这怎么可能? If the interface is implemented, why are ICollection members not exposed? 如果实现了接口,为什么ICollection成员不会被暴露?

Furthermore, why does the Dictionary class need to implement ICollection in the first place? 此外,为什么Dictionary类需要首先实现ICollection

Here is a rough implementation: 这是一个粗略的实现:

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> implements the ICollection<KeyValuePair<TKey, TValue>> interface explicitly . Dictionary<TKey, TValue> 显式实现ICollection<KeyValuePair<TKey, TValue>>接口。

As you can see on the MSDN page for Dictionary , these methods are listed under "Explicit interface implementations". 正如你可以看到上为MSDN页Dictionary ,这些方法是在“显式接口实现”上市。

Explicitly implementing an interface means that those methods will not be available through the concrete type. 显式实现接口意味着这些方法不能通过具体类型获得。 You'll have to cast the dictionary to an ICollection<T> to be able to call them. 您必须将字典ICollection<T>转换为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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 转换iDictionary <TKey,TValue> 进入IDictionary <TSomeProperty, ICollection<TValue> &gt; - Convert iDictionary<TKey,TValue> into IDictionary<TSomeProperty, ICollection<TValue>> 为什么IDictionary <TKey,TValue>实现了ICollection和IEnumerable - Why IDictionary<TKey, TValue> implements both ICollection and IEnumerable 为什么要向上转换IDictionary <TKey, TValue> 到IEnumerable <object> 失败? - Why does upcasting IDictionary<TKey, TValue> to IEnumerable<object> fail? 为什么反射器不显示 IDictionary<TKey, TValue> 实现 IEnumerable<T> ? - Why does the reflector not show that IDictionary<TKey, TValue> implements IEnumerable<T>? 如何转换IDictionary <TKey, List<TValue> &gt;到IDictionary <TKey, IEnumerable<TValue> &gt;? - How to convert IDictionary<TKey, List<TValue> > to IDictionary<TKey, IEnumerable<TValue> >? ObservableDictionary类 <TKey, TValue> :IDictionary <TKey, TValue> 作为DataContract - class ObservableDictionary<TKey, TValue> : IDictionary<TKey, TValue> as DataContract 推荐的转换IGrouping的方法 <TKey, TValue> 到IDictionary <TKey, IEnumerable<TValue> &gt; - Recommended way to convert IGrouping<TKey, TValue> to IDictionary<TKey, IEnumerable<TValue>> 为什么不能/不能IDictionary <TKey,TValue>实现ILookup <TKey,TValue>? - Why doesn't/couldn't IDictionary<TKey,TValue> implement ILookup<TKey,TValue>? 创建可枚举的扩展方法TryGetValue(TKey,out TValue) <KeyValuePair<TKey, TValue> &gt;就像字典一样 <TKey, TValue> - Create Extension Method TryGetValue(TKey, out TValue) of Enumerable<KeyValuePair<TKey, TValue>> just like of Dictionary<TKey, TValue> 为什么我不能将KeyValuePair <TKey,TValue>与默认值进行比较 - Why can't I compare a KeyValuePair<TKey, TValue> with default
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM