Change Add to Dictionary: Override IDictionary versus Dictionary Inheritance

So, let's say that I need to override Add in my Dictionary<string, string> .

Solution A:

 public class ImplementedDict : IDictionary<string, string> { private readonly Dictionary<string, string> _data = new Dictionary<string, string>(); public void Add(string key, string value) { _data.Add(key, value); } } 

Solution B:

 public class InheritedDict : Dictionary<string, string> { public override void Add(string key, string value) { base.Add(key, value); } } 

Another Qaru user told me (in another comment thread) that solution A is the right one. Which is contrary to what I expected.

Apparently, this is because "Add is not a virtual method." He pointed me to the documentation: https://msdn.microsoft.com/en-us/library/k7z0zy8k(v=vs.110).aspx

I admit, I still do not understand.

Edit : The question is, why is solution B considered bad code? (sorry @Backs answer made sense when I mistakenly asked "why would solution A be considered bad code?")

+4
source share
4 answers

When you obscure a method, if the client code calls the method with the instance entered as the base class, then your method will not be called. It will call the base class method.

This is the main reason you are encouraged to use method1.

For instance:

 Dictionary<string, string> dic = new InheritedDict(); dic.Add("key","value");//Will not call your method. 

If the Add method was declared virtual (this is not the case), you could override the Add method. In this case, it will call the method of the derived class, as you expect.

+4
source

Short - no reason :) I see no problems. You have your own implementation of IDictionary. This is normal.

+2
source

Another possibility is the extension method:

  public static DictionaryExtensions { public static void MyAdd(this Dictionary<string, string> dictionary, String key, String value) { ... } } 

Thus, you can use the existing Dictionary<String, String> (Microsoft) implementation with enhanced functionality:

  Dictionary<string, string> dict = new Dictionary<string, string>(); ... dict.MyAdd("myKey", "SomeValue"); 
+2
source

You can think about it, since each implemented interface has its own virtual method table.

However, Dictionary.Add not virtual; you cannot invoke an inheritance implementation:

 Dictionary<string, string> d = new InheritedDict<string, string>(); // Ok, you can do it d.Add("foo", "bar"); // Wrong! You can, but Dictionary.Add will be called 

But:

 public class InheritedDict : Dictionary<string, string> { void IDictionary<string, string>.Add(string key, string value) { base.Add(key, value); } } Dictionary<string, string> d = new InheritedDict<string, string>(); var d2 = d as IDictionary<string, string>; d2.Add("foo", "bar"); // Ok 

The second way is complicated, right?

Therefore, it is better to implement the entire IDictionary interface instead of overriding a single Dictionary method. Therefore, although solution A is a bit detailed, it is absolutely correct.

+2
source

All Articles