繁体   English   中英

C#类型转换帮助

[英]C# type conversion Help

我有一个结构如下

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();
}

我希望返回类型为位置列表,我该怎么做? 提前致谢

...

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 { x, y } ,不如select new Location(x, y)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM