Joa*_*nge 8 .net c# collections ienumerable
我有一个类,IEnumerable<T>我希望有不同的属性,提供过滤IEnumerable<T>访问.
例如:
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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
这应该是怎么回事?只是不确定,关于它完全.
谢谢.
mqp*_*mqp 11
当然,但你可能想把它重写为
public IEnumerable<Shape> OnlyBox
{
get { return this.Where(x => x.ShapeType == ShapeType.Box); }
}
Run Code Online (Sandbox Code Playgroud)
它做了完全相同的事情.