将枚举<integer>转换为从Java到C#的循环? C#中的Enumeration <integer>究竟是什么?

时间:2017-02-03 10:38:52

标签: java c#

我正在将项目从Java转换为C#。我试图搜索这个,但我遇到的只是关于枚举的问题。有一个Hashtable htPlaylist,循环使用Enumeration来完成密钥。我如何将此代码转换为C#,但使用Dictionary而不是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());
}

1 个答案:

答案 0 :(得分:4)

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

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

或者

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

或者如果你只想要钥匙:

   foreach (int key in htPlaylist.Keys) {
     ...
   }