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

6

我正在将一个Java项目转换成C#。我已经尝试搜索这个问题,但是所有相关的都是关于枚举类型的问题。有一个Hashtable htPlaylist,并且循环使用Enumeration去遍历键。如果我想使用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());
}

2
天啊,这段Java代码有多老? - Andy Turner
1
什么是在C#中迭代字典的最佳方法? - Equalsk
那我应该只需要使用foreach(KeyValuePair<string, string>吗?不过在Java中,Enumeration<Integer>具体是什么呢?在C#中相当于它的等价物是什么? - Michael Weston
忽略,我在stackoverflow的另一个线程中得到了关于哈希表的答案。这是一个重复的问题,请允许我删除。 - Michael Weston
1个回答

6
嗯,只是一个 foreach 循环?根据给定条件。
   Dictionary<int, SongInfo> htPlaylist = MySongs.getSongs();

either

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

或者如果您只想要键:

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接