c#:Enumeration

Converting Enumeration<Integer> for loop from Java to C#? What exactly is an Enumeration<Integer> in C#?

本问题已经有最佳答案,请猛点这里访问。

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

1
2
3
4
5
6
7
8
9
// 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());
}


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

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

任何一个

1
2
3
4
5
   foreach (var pair in htPlaylist) {
     // int key = pair.Key;
     // SongInfo info = pair.Value;
     ...
   }

或者如果你只想要钥匙:

1
2
3
   foreach (int key in htPlaylist.Keys) {
     ...
   }