将 Enumeration<Integer> for 循环从 Java 转换为 C#?C# 中的 Enumeration<Integer> 到底是什么?

Mic*_*ton 6 c# java

我正在将一个项目从 Java 转换为 C#。我试图搜索这个,但我遇到的只是关于枚举的问题。有一个 Hashtable htPlaylist,循环使用 Enumeration 遍历键。我如何将此代码转换为 C#,但使用字典而不是哈希表?

// 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());
}
Run Code Online (Sandbox Code Playgroud)

Dmi*_*nko 6

呃,只是一个foreach循环?对于给定的

   Dictionary<int, SongInfo> htPlaylist = MySongs.getSongs();
Run Code Online (Sandbox Code Playgroud)

任何一个

   foreach (var pair in htPlaylist) {
     // int key = pair.Key;
     // SongInfo info = pair.Value;
     ...
   }
Run Code Online (Sandbox Code Playgroud)

或者如果你只想要钥匙:

   foreach (int key in htPlaylist.Keys) {
     ...
   }
Run Code Online (Sandbox Code Playgroud)