c#类型转换帮助

本文关键字:帮助 类型转换 | 更新日期: 2023-09-27 17:50:18

我有一个Struct如下,

struct Location
{
    public int Row;
    public int Column;
    public Location(int row, int column)
    {
        this.Row = row;
        this.Column = column;
    }
}

和我有一个函数如下,

public List<Location> getNeighboringLocations(int row, int column)
{
    int[,] array = new int[rows, columns];
    int refx = row;
    int refy = column;
    //var neighbours = from x in Enumerable.Range(refx - 1, 3)
    //                 from y in Enumerable.Range(refy - 1, 3)
    //                 where x >= 0 && y >= 0 && x < array.GetLength(0) && y < array.GetLength(1)
    //                 select new { x, y };
    var neighbours = from x in Enumerable.Range(0, array.GetLength(0)).Where(x => Math.Abs(x - refx) <= 1)
                 from y in Enumerable.Range(0, array.GetLength(1)).Where(y => Math.Abs(y - refy) <= 1)
                 select new { x, y };
    return neighbours.ToList();
}

我想返回类型是位置列表我怎么做?

c#类型转换帮助

select new Location(x, y);
var neighbours = from x in Enumerable.Range(0, array.GetLength(0)).Where(x => Math.Abs(x - refx) <= 1)
                             from y in Enumerable.Range(0, array.GetLength(1)).Where(y => Math.Abs(y - refy) <= 1)
                             select new Location( x, y );
return neighbours.ToList();

不要使用返回匿名类型的select new { x, y },而应该使用select new Location(x, y)