6

I'm converting a project from Java to C#. I've tried to search this, but all I come across is questions about enums. There is a Hashtable htPlaylist, and the loop uses Enumeration to go through the keys. How would I convert this code to C#, but using a Dictionary instead of a Hashtable?

// My C# Dictionary, formerly a Java Hashtable.
Dictionary<int, SongInfo> htPlaylist = MySongs.getSongs();

// Original Java code trying to convert to C# using a Dictionary.
for(Enumeration<Integer> e = htPlaylist.keys(); e.hasMoreElements();
{
    // What would nextElement() be in a Dictonary? 
    SongInfo popularSongs = htPlaylist.get(e.nextElement());
}
Michael Weston
  • 377
  • 4
  • 15

1 Answers1

6

Eh, just a foreach loop? For the given

   Dictionary<int, SongInfo> htPlaylist = MySongs.getSongs();

either

   foreach (var pair in htPlaylist) {
     // int key = pair.Key;
     // SongInfo info = pair.Value;
     ...
   }

or if you want just keys:

   foreach (int key in htPlaylist.Keys) {
     ...
   }
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215