0

While I was trying to implement the IDictionary<TKey, TValue> interface on one of my classes, I chose to delegate all the interface methods of IDictionary<TKey, TValue> to an instance of Dictionary<TKey, TValue> that I had instantiated in my class. Then, I was quite confused that the IDE complained that I was trying to access private methods of Dictionary<TKey, TValue>. However, these methods are ought to be part of the IDictionary<TKey, TValue> interface! How can they be private? I checked the source code of Dictionary<TKey, TValue> and indeed it has a private implementation of CopyTo. How am I supposed to delegate CopyTo and how is it even possible for an interface method to be private in the first place?

The IDE says that CopyTo is private in the following code.

public abstract class BindingCollectionTemplate<TName, TValue>: ScopeTemplate, IDictionary<TName, TValue>
{
    private readonly Dictionary<TName, TValue> dictionary = new();
    // ...
    public void CopyTo(KeyValuePair<TName, TValue>[] array, int arrayIndex)
    {
        dictionary.CopyTo(array, arrayIndex); // Accessing a private interface method??
    }
    // ...
}
David von Tamar
  • 797
  • 3
  • 12
  • 29
  • 2
    Please show your code. A [mcve] please. – Sweeper Jul 06 '23 at 14:52
  • 3
    The implementaton of [`CopyTo(Array, Int32)`](https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.dictionary-2.system-collections-icollection-copyto?view=net-7.0#system-collections-generic-dictionary-2-system-collections-icollection-copyto(system-array-system-int32)) is not private, it's **explicit**. See [Explicit Interface Implementation (C# Programming Guide)](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/interfaces/explicit-interface-implementation). – dbc Jul 06 '23 at 14:54
  • See [C# Interfaces. Implicit implementation versus Explicit implementation](https://stackoverflow.com/q/143405). – dbc Jul 06 '23 at 14:55
  • Does this answer your question? [Why can't I call methods within a class that explicitly implements an interface?](https://stackoverflow.com/questions/2520727/why-cant-i-call-methods-within-a-class-that-explicitly-implements-an-interface) – Charlieface Jul 06 '23 at 16:02

1 Answers1

4

This method comes from ICollection<T> interface and dictionary implements it:

public interface IDictionary<TKey,TValue>
 : ICollection<KeyValuePair<TKey,TValue>>, ... 

In dictionary this method is implemented via an explicit interface implementation which uses the private CopyTo:

void ICollection<KeyValuePair<TKey, TValue>>.CopyTo(
    KeyValuePair<TKey, TValue>[] array, 
    int index) => CopyTo(array, index);

So you can access it via interface:

class MyDict<TKey, TValue> : IDictionary<TKey, TValue>
{
    private Dictionary<TKey, TValue> _internal = new Dictionary<TKey, TValue>();
    public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex) =>
        ((IDictionary<TKey, TValue>)_internal).CopyTo(array, arrayIndex);
}
Guru Stron
  • 102,774
  • 10
  • 95
  • 132