what is the different between IDictionary.GetEnumerator and IEnumerable.GetEnumerator

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Lyu KingT
    New Member
    • Aug 2010
    • 5

    #1

    what is the different between IDictionary.GetEnumerator and IEnumerable.GetEnumerator

    i am glad to read this post:
    Why two GetEnumerator methods implementing IDictionary?

    and what i learned from the post is the different between Implicit and Explicit Interface Implementations .
    however, i do still not understand the difference between
    IDictionary.Get Enumerator() : System.Collecti ons.IDictionary Enumerator
    and
    IEnumerable.Get Enumerator() : System.Collecti ons.IEnumerator

    and i wish to know when implementing IDictionary, which one of GetEnumerator() i should explicit implementing, IDictionary.Get Enumerator() or IEnumerable.Get Enumerator().

    regards.
  • Christian Binder
    Recognized Expert New Member
    • Jan 2008
    • 218

    #2
    Try the following

    Code:
          IDictionary dict = new Dictionary<string, string>();
    
          dict.Add("Test1", "Value1");
          dict.Add("Test2", "Value2");
    
          System.Diagnostics.Debug.WriteLine("ForEach with IDictionary");
    
          foreach (object v1 in (IDictionary)dict) {
            System.Diagnostics.Debug.WriteLine(v1.GetType().Name);
          }
    
          System.Diagnostics.Debug.WriteLine("ForEach with IEnumerable");
    
          foreach (object v2 in (IEnumerable)dict) {
            System.Diagnostics.Debug.WriteLine(v2.GetType().Name);
          }


    You'll see, that the first one, returns a DictionaryEntry
    and the second one returns a KeyValuePair<st ring, string>

    Comment

    Working...