I have a class that's IEnumerable<T>
where I want to have different properties that provides a filtered IEnumerable<T>
access.
So for instance:
class Shape
ShapeType = Box/Sphere/Pyramid
class ShapeCollection : IEnumerable<Shape>
{
public IEnumerable<Shape> OnlyBox
{
foreach(var s in this)
{
if (s.ShapeType == Box)
yield return s;
}
}
}
Is this how it should be? Just not sure, about it completely.
Thanks.
Sure, but you might want to rewrite it as
public IEnumerable<Shape> OnlyBox
{
get { return this.Where(x => x.ShapeType == ShapeType.Box); }
}
which does the exact same thing.
class ShapeCollection : IEnumerable<Shape>
{
public IEnumerable<Shape> OnlyBoxes
{
get { return this.Where(s => s.ShapeType == Box); }
}
}
You were missing the get
/parenthesis to make it a method. Also what is Box
, did you mean ShapeType.Box
? Also maybe rename it to OnlyBoxes
, seems more descriptive.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With