C# 类型转换帮助
我有一个如下的结构,
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();
}
我希望返回类型是位置列表,我该怎么做? 提前致谢
I have a Struct as follows,
struct Location
{
public int Row;
public int Column;
public Location(int row, int column)
{
this.Row = row;
this.Column = column;
}
}
and i have a function as follows,
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();
}
I want the return type be the List of Locations how do i do it?
Thanks in Advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
...
...
您不应执行返回匿名类型的
select new { x, y }
操作,而应执行select new Location(x, y)
操作。Instead of doing
select new { x, y }
which returns an anonymous type you should doselect new Location(x, y)
.