Reputation: 8851
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>();
}
}
Upvotes: 1
Views: 202
Reputation: 203837
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.
Upvotes: 4