如何通过return语句返回每个foreach -loop元素 [英] how to return each foreach -loop element through return statement
本文介绍了如何通过return语句返回每个foreach -loop元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
public Circle3D GetSphereCircle(BaseLibrary.VIEWCODE viewCode)
{
List<vec3> VectorList = new List<vec3>();
Vec3 vec = new Vec3(0, 0, 0);
-----
foreach (Vec3 vector in VectorList)
{
refPlane = new Plane3D(new Plane3(centPt, vector), false);
circle = new Circle3D(new Circle3(centPt, _local_data._radius), refPlane, false);
}
return circle;
}
上面我给出了短代码片段,我想返回foreach循环中生成的每个'circle',我该怎么做?
Above i am given short code snippet,i want to return each 'circle' generated in foreach loop,how i can do that?
推荐答案
合理的方法是返回一个容器(例如 List
)的圆圈,当然这意味着要更改调用者代码但是,任何其他方法(据我所知)都会参与其中。
A reasonable way is returning a container (e.g. a List
) of circles, of course this implies changing the callers code, but any other approach (as far as I can see) would be rather involved.
要添加CPallini所说的 - 而且他是对的 - 唯一的另一种方法是收益率回报
: https://msdn.microsoft .com / zh-CN / library / 9k7k7cf0.aspx [ ^ ]使其成为迭代器方法。
To add to what CPallini has said - and he's right - the only other way to do it would involve a yield return
: https://msdn.microsoft.com/en-us/library/9k7k7cf0.aspx[^] making it into an iterator method.
这篇关于如何通过return语句返回每个foreach -loop元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文