Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Dictionary<T, V> explicitly implements ICollection<KeyValuePair<T, V>> when it is already inherited by IDictionary<T,V>

Recently I've been asked by my friend why Dictionary<T, V> explicitly implements ICollection<KeyValuePair<T, V>> when it is already inherited by IDictionary<T,V>?

I realized that I don't know the answer as well. Here are the links to official documentation:

Dictionary<T, V> - http://msdn.microsoft.com/en-us/library/xfhwa508(v=vs.110).aspx IDictionary<T,V> - http://msdn.microsoft.com/en-us/library/s4ys34ea(v=vs.110).aspx

Is there any specific reason why it was done like that? Is there a specific architecture pattern or anything?

like image 296
Sergei Avatar asked Jan 23 '14 22:01

Sergei


1 Answers

This appears to simply be a case of showing all of the implemented interfaces implicitly.

I made this following example to test this. My source code is:

public class Class1 : ITest
{
    public void Testing()
    {
        Console.WriteLine("Test");
    }

    public void Testing2()
    {
        Console.WriteLine("Test2");
    }
}

public interface ITest : ITest2
{
    void Testing();
}

public interface ITest2
{
    void Testing2();
}

Now going in to Reflector.Net and decompiling I get this:

public class Class1 : ITest, ITest2
{
    public void Testing()
    {
        Console.WriteLine("Test");
    }

    public void Testing2()
    {
        Console.WriteLine("Test2");
    }
}
like image 70
Enigmativity Avatar answered Nov 01 '22 16:11

Enigmativity