I have this function:
class Path : List<LineSegment> { }
private IEnumerable<LineSegment> GetLineSegments(CollisionType collisionType, Path path)
{
if (collisionType == CollisionType.End)
{
yield return path.First();
yield return path.Last();
}
else if (collisionType == CollisionType.Segment)
{
foreach (LineSegment lineSegment in path)
{
yield return lineSegment;
}
}
}
Basically, I am checking for collision (between ball and line) in two ways. In one case I only want to check the end points of the drawn path, but in the other case I want to return each line segment in the entire path.
It seems to be a bit weird to loop through the given list just to return the entire list. Can I just return the list? I'd like to be able to do something like this but I'm getting errors:
private IEnumerable<LineSegment> GetLineSegments(CollisionType collisionType, Path path)
{
if (collisionType == CollisionType.End)
{
yield return path.First();
yield return path.Last();
}
else if (collisionType == CollisionType.Segment)
{
return path.AsEnumerable<LineSegment>();
}
}
You have no choice but to use a foreach
loop. It's a somewhat frequently requested feature, but one that is not in the language.
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句