Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# inherit from Dictionary, iterate over KeyValuePairs

I have a class that inherits from Dictionary<string, string>. Within an instance method, I want to iterate over all KeyValuePair<string, string>'s. I've tried doing the following:

foreach (KeyValuePair<string, string> pair in base)

But this fails with the following error:

Use of keyword 'base' is not valid in this context

How can I iterate over the KeyValuePair<string, string>'s in an instance method in a class that derives from Dictionary<string, string>?

Edit: I found I can do the following:

var enumerator = base.GetEnumerator();
while (enumerator.MoveNext())
{
    KeyValuePair<string, string> pair = enumerator.Current;
}

However, I would still like to know if there's a way to do this via a foreach loop.

Edit: thanks for the advice about not inheriting from Dictionary<string, string>. I'm instead implementing System.Collections.IEnumerable, ICollection<KeyValuePair<string, string>>, IEnumerable<KeyValuePair<string, string>>, IDictionary<string, string>.

like image 239
Sarah Vessels Avatar asked Dec 04 '22 13:12

Sarah Vessels


2 Answers

First, deriving from the .NET collection classes is generally ill-advised because they don't offer virtual methods for calls not inherited from object. This can result in bugs when passing your derived collection in via a base-class reference somewhere. You are better off implementing the IDictionary<T,TKey> interface and aggregating a Dictionary<,> inside your implementation - to which you then forward the appropriate calls.

That aside, in your specific case, what you want to do is:

foreach( KeyValuePair<string,string> pair in this )  { /* code here */ }

The base keyword is primarily used to access specific members of your base class. That's not what you're doing here - you are attempting to iterate over the items of a particular instance ... which is simply the this reference.

like image 149
LBushkin Avatar answered Dec 06 '22 02:12

LBushkin


I agree with JaredPar's comment that this isn't a great idea. You probably don't want to publicly expose all of the methods of Dictionary to the outside world, so just make the Dictionary a private member variable and then provide your own interface to it.

With that said, the way to do what you're trying to do is:

foreach (KeyValuePair<string, string> pair in this)
like image 35
Nate C-K Avatar answered Dec 06 '22 03:12

Nate C-K