C#错误消息:无法隐式转换类型

数据

下面的代码引发以下异常:

错误1无法将类型'Clarity.DevTest.Exercises.ChessPosition'隐式转换为'System.Collections.Generic.IEnumerable <Clarity.DevTest.Exercises.ChessPosition>'。存在显式转换(您是否缺少演员表?)

有人可以向我解释出了什么问题以及为什么它不能返回结果吗?

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; }
  }
}
大卫·L

您将返回单个ChessPosition而不是可枚举的结果集合。

要么返回一个可枚举的集合:

ChessPosition result = new ChessPosition(4, 5);
IEnumerable<ChessPosition> results = new List<ChessPosition>();
results.Add(result);

return results;

或更改您的退货类型:

public ChessPosition GetLegalMoves(
    ChessPosition rookToMove, ChessPosition friendlyRook, ChessPosition 
    enemyRook1, ChessPosition enemyRook2)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

无法隐式转换C#类型

无法在C#中将类型void隐式转换为对象错误

如何修复C#中的“无法隐式转换类型”错误?

错误 2 无法将类型“int”隐式转换为“string”c#

C#“无法隐式转换类型”相同的模型属性

无法隐式转换类型错误

获取错误消息:CS0029:无法隐式转换(ASP.NET Core MVC & C#)

错误消息“无法将类型'String'隐式转换为'Int'”

C# 返回错误“无法将类型‘int’隐式转换为‘string’”,即使没有给出 int

C#:错误-无法将类型'int'隐式转换为'System.Collections.Generic.IEnumerable <double>'

c# - 如何修复停止“无法将int类型隐式转换为bool”错误代码

将CType转换为C#时,无法将类型'string'隐式转换为'string []'

无法隐式转换类型

无法在通用方法中隐式转换类型错误

无法将类型'double'隐式转换为'int'。-错误

使用别名时无法隐式转换类型错误

c#无法将类型“ double”隐式转换为System.Window.Forms.Textbox

无法隐式转换类型-Autofac C#和ASP.NET MVC

CS0029 C#'无法将类型string []隐式转换为字符串'

C#,无法将类型'long'隐式转换为'uint'

无法在C#中的PrivateObject中将类型对象隐式转换为字符串

无法隐式转换类型'system.datetime'的顶级字符串c#

C# - 无法将类型“int”隐式转换为“int[][]” - CodeFights

无法将类型'int'隐式转换为'string'c#

无法将类型void隐式转换为字符串C#

无法在C#中将类型字符串隐式转换为int

C ++隐式类型转换

在C#中减去整数时出现错误“无法将int隐式转换为short”

C#wpf DbContext无法隐式转换类型