我很难理解如何将这个Java代码转换为C#代码。非常感谢任何帮助。
public enum Direction {
NORTH(0), EAST(1), SOUTH(2), WEST(3);
private static Map<Integer, Direction> map = new HashMap<Integer, Direction>();
static {
for (Direction directionEnum : Direction.values()) {
map.put(directionEnum.directionIndex, directionEnum);
}
}
答案 0 :(得分:1)
这是您移植的C#代码。如果您不了解Enum.GetValues(typeof(Direction)
API,那么唯一可能阻碍您顺利移植的内容就是它。希望这有帮助!
public enum Direction
{
NORTH = 0,
EAST = 1,
SOUTH = 2,
WEST = 3
}
private static Dictionary<int, Direction> map = new Dictionary<int, Direction>();
private static void PortJavaCodeToCSharp()
{
foreach (var directionName in Enum.GetValues(typeof(Direction)))
{
map.Add((int)directionName, (Direction)directionName);
}
}