【发布时间】:2015-08-04 23:55:34
【问题描述】:
下面的代码抛出以下异常:
错误 1 无法隐式转换类型 'Clarity.DevTest.Exercises.ChessPosition' 到 'System.Collections.Generic.IEnumerable'。显式转换 存在(您是否缺少演员表?)
有人可以向我解释发生了什么问题以及为什么它不能返回结果吗?
using System;
using System.Collections.Generic;
namespace Clarity.DevTest.Exercises
{
public class Exercise1
{
public IEnumerable<ChessPosition> GetLegalMoves(ChessPosition rookToMove, ChessPosition friendlyRook, ChessPosition enemyRook1, ChessPosition enemyRook2)
{
//create array to hold positions
ChessPosition[,] coords = new ChessPosition [8,8];
//set the Rooks into the correct places on the array
//eg rookToMove.X = 4, rookToMove.Y = 5, "R" would be places in 4,5
coords[rookToMove.X, rookToMove.Y] = "R";
coords[friendlyRook.X, friendlyRook.Y] = "F";
coords[enemyRook1.X, enemyRook1.Y] = "1";
coords[enemyRook2.X, enemyRook2.Y] = "2";
//throw new NotImplementedException();
ChessPosition result = new ChessPosition(4, 5);
return result;
}
}
public partial class ChessPosition
{
public ChessPosition(int x, int y)
{
//check to see if out of bounds
if (x < 1 || x > 8 || y < 1 || y > 8)
{
throw new ArgumentException("x and y must be in the range 1-8");
}
X = x;
Y = y;
}
public int X { get; set; }
public int Y { get; set; }
}
}
【问题讨论】:
标签: c# class multidimensional-array