1

In my current implementation, I was trying to create custom class inherited by IDictionary<string, Object> interface.

While implementation, I have to implement the below property

IsReadOnly { get; } part of  ICollection<KeyValuePair<TKey, TValue>> interface.

So while doing this I came across below scenario,

I am not seeing that IsReadOnly Property in Dictionary<TKey, TValue> (.NET built in class) class, then how property implementation of ReadOnly in not exists in Dictionary class as this class is also inherited through ICollection<KeyValuePair<TKey, TValue>> interface.

I have same query regarding CopyTo method also.

This is just my conceptual question.

Ashish Sapkale
  • 540
  • 2
  • 13
  • The interface **is** implemented on `Dictionary`, however you see it only when you cast your map to an `ICollection`, becaue the interface is implemented **explicitely**. – MakePeaceGreatAgain Nov 13 '18 at 09:33
  • What problem do you want to solve with your own implementation of `IDictionary`? --- if you *really* want to do that, you might want to look at https://referencesource.microsoft.com/#mscorlib/system/collections/generic/dictionary.cs – Corak Nov 13 '18 at 09:33

1 Answers1

1

The property is implemented on Dictionary<TKey, TValue>. The reason you don´t see it on your instance is the way how the member is implemented:

bool ICollection<KeyValuePair<TKey,TValue>>.IsReadOnly {
    get { return false; }
}

The qualifier in front of the member-name indicates an explicit implementation. This means you can access this member only when explicitely referring to that type by casting to it:

var readOnly = ((ICollection<MyType>)myMap.IsReadOnly;
MakePeaceGreatAgain
  • 35,491
  • 6
  • 60
  • 111