using foreach statement on a serializable dictionary

I have a serializable dictionary that I created for a WCF REST web service

[Serializable]
public class jsonDictionary<TKey, TValue> : ISerializable
{
    private Dictionary<TKey, TValue> _Dictionary;
    public jsonDictionary()
    {
        _Dictionary = new Dictionary<TKey, TValue>();
    }
    public jsonDictionary(SerializationInfo info, StreamingContext context)
    {
        _Dictionary = new Dictionary<TKey, TValue>();
    }
    public TValue this[TKey key]
    {
        get { return _Dictionary[key]; }
        set { _Dictionary[key] = value; }
    }
    public void Add(TKey key, TValue value)
    {
        _Dictionary.Add(key, value);
    }
    public void GetObjectData(SerializationInfo info, StreamingContext context)
    {
        foreach (TKey key in _Dictionary.Keys)
            info.AddValue(key.ToString(), _Dictionary[key]);
    }
}

I need to search through this dictionary to determine if 1 of several possible keys are present. I figured I would do this using a foreach statement kinda like so

foreach(var pair in dictionary)
{
    switch(pair.key)
    {
        case "something":

        Break;
        case "somethingelse":
        Break;
     }
}

However I keep getting the error:

    foreach statement cannot operate on variables of type 'ZTERest.jsonDictionary<string,string>' because 'ZTERest.jsonDictionary<string,string>' does not contain a public definition for 'GetEnumerator'

I know I have to do something with the IEnumerable or IEnumerator interface but I'm not sure how.

Answers


A dictionary provides a key/value lookup for a reason, it's not something that you usually iterate through. The generic Dictionary<> object that your class is wrapping already has a method called ContainsKey() that will do exactly what you're looking for, without the overhead of going through every single key/value pair to see if it's there. There's no need to expose an iterator, just add this to your class.

public bool ContainsKey(TKey key)
{
    return _Dictionary.ContainsKey(key);
}

And call it like this.

if (dictionary.ContainsKey("Something"))
{
    //do something
}

Your class needs to implement IEnumerable to perform foreach on the Key of the dictionary, hence your class would look as below

public class jsonDictionary<TKey, TValue> : ISerializable, IEnumerable<TKey>

Then you need to implement the following methods from the IEnumerable interface as shown:

public IEnumerator<TKey> GetEnumerator()
{
    // Since we want to iterate on the Key we specifiying the enumerator on keys
    return _Dictionary.Keys.GetEnumerator();
}

IEnumerator IEnumerable.GetEnumerator()
{
    return GetEnumerator();
}

Then when you perform a foreach the keys from dictionary are fetched hence you can have your foreach as shown:

foreach(var key in dictionary)
{
    switch(key)
    {
         case "Something":
             // do something
             break;
         case "SomethingElse":
             //do something
             break;
    }
}

In case you actually need to iterate through the dictionary (instead of just checking if a certain key exists) the Dictionary.Keys property returns an enumerable collection which you could use iterate through the dictionary:

foreach(var key in dictionary.Keys)
{
    if(dictionary[key] == "something")
    {
        // Do something
    }
}

Need Your Help

Chrome bug - border radius not clipping contents when combined with css transition

google-chrome css-transitions css3 css-transforms

My issue is that during CSS transition border-radius temporarily stops clipping elements inside if transition of overlapping element involves transform. In my case I have two divs absolutely positi...

Network discovery in .NET

c# networking

I need to get a list of machine connected to the current computer, just like explorer do. I want to use their name to retrieve their shared path using NetShareEnum.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.