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

标签 java c#

<分区>

我正在将一个项目从 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());
}

最佳答案

嗯,只是一个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) {
     ...
   }

关于java - 将 Enumeration<Integer> for 循环从 Java 转换为 C#? C# 中的 Enumeration<Integer> 到底是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42022276/

相关文章:

c# - 测试超出执行超时时间

c# - 从 Asp.Net WebApi 返回字符串后的 JsonConvert DeserializeObject 异常

c# - String.Split() 删除前导零

C# 类构造函数分配给 'this'

java - 拦截 JSON 消息 - Eclipse RAP

java - 具有相同事件监听器的堆叠组件

java - JPanel 的空指针异常

c# - 单元测试 Windows 8 应用商店应用程序用户界面(Xaml 控件)

java - 如何使用 spring-amqp 关闭与 Rabbit MQ 代理的连接

Java - 将当前日期保存到sql数据库(时间字段)