【发布时间】:2020-01-29 13:19:00
【问题描述】:
我在 Java 中使用HashMap,如下所示:
Map<Player, List<Attribute>> map = new HashMap<>();
map.put(
player("Lebron James"),
attribute("mid height", "mid weidght", "high vertical")
);
效果很好,但现在我想将它转换成C#,使用Dictionary:
Dictionary<Player, List<Attribute>> dictionary = new Dictionary<Player, List<Attribute>>();
dictionary.Add(
player("Kobe Bryant"), // <- Doesn't compile
attribute("mid height", "mid weidght", "high vertical")
);
谁能解释一下,为什么C#代码不能编译?消息是
无法调用的播放器组件不能作为方法使用
【问题讨论】:
-
您能否明确告诉我们什么是不起作用以及结果如何?是否有错误(消息)?
-
dictionary.Addof aDictionary<Player, List<Attribute>>期望List<Attribute>作为第二个参数,但您只传递一个Attribute而不是一个列表。这正是编译器错误消息(您忘记包含在您的问题中)告诉您的内容。 -
"无法调用的播放器组件不能作为方法使用"
-
new Player("Kobe Bryant")而不是player("Kobe Bryant")? -
如果没有
new!..,您的 Java 代码也不应该工作。如果该 Java 代码确实工作,这意味着您有称为player和attribute的方法返回一个 @分别为 987654334@ 实例和List<Attribute>,这也可以在 C# .NET 代码中完成..
标签: java c# dictionary hashmap