我正在将一个项目从Java转换为C#。我试过搜索这个,但我遇到的都是关于枚举的问题。有一个哈希表htPlaylist,循环使用枚举来遍历键。如何将此代码转换为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());
}发布于 2017-02-03 10:44:47
呃,只是一个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) {
...
}https://stackoverflow.com/questions/42022276
复制相似问题