我如何实现IEnumerable? [英] How do I implement IEnumerable?

查看:179
本文介绍了我如何实现IEnumerable?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含对象的静态数的类。这个类需要经常'比较'的其他类,这将是简单的List对象。

I have a class that contains a static number of objects. This class needs to be frequently 'compared' to other classes that will be simple List objects.

public partial class Sheet
{
 public Item X{ get; set; }
 public Item Y{ get; set; }
 public Item Z{ get; set; }
}



中的项目显然不会是XY Z,那些例如只是通用名称。问题是,由于需要做什么性质,一个列表将不工作;尽管一切都在这里将是项目类型。它像的非常具体的事情一个清单具有针对代码和运行时进行测试。

the items are obviously not going to be "X" "Y" "Z", those are just generic names for example. The problem is that due to the nature of what needs to be done, a List won't work; even though everything in here is going to be of type Item. It is like a checklist of very specific things that has to be tested against in both code and runtime.

这所有的作品很好,很好;这不是我的问题。我的问题是它的迭代。比如我要做到以下几点...

This works all fine and well; it isn't my issue. My issue is iterating it. For instance I want to do the following...

List<Item> UncheckedItems = // Repository Logic Here.



UncheckedItems包含了所有可用的物品;而CheckedItems是板材类实例。 CheckedItems将包含从未经检查的移动托运物品;然而,由于存储系统的性质,项目已移至经过离不开未选中删除。我只是想通过选中进行迭代,并从列表中未选中已在选中删除任何东西。

UncheckedItems contains all available items; and the CheckedItems is the Sheet class instance. CheckedItems will contain items that were moved from Unchecked to Checked; however due to the nature of the storage system, items moved to Checked CANNOT be REMOVED from Unchecked. I simply want to iterate through "Checked" and remove anything from the list in Unchecked that is already in "Checked".

所以很自然的,这会是这样一个正常的列表。

So naturally, that would go like this with a normal list.

foreach(Item item in Unchecked)
{
 if( Checked.Contains(item) )
 Unchecked.Remove( item );
}



不过,由于手册是不是'清单',我不能这样做。所以我想实现IEnumerable这样我就可以。有什么建议么?我从来没有实现IEnumerable的直接之前,我到哪里开始很困惑。

But since "Sheet" is not a 'List', I cannot do that. So I wanted to implement IEnumerable so that I could. Any suggestions? I've never implemented IEnumerable directly before and I'm pretty confused as to where to begin.

推荐答案

您需要创建一个迭代器返回驻留在纸上的产品。

You need to create an iterator that returns the Items that reside in the Sheet.

使用迭代器

public partial class Sheet
{
    public Item X{ get; set; }
    public Item Y{ get; set; }
    public Item Z{ get; set; }

    public IEnumerable<Item> EnumerateItems()
    {
        yield return X;
        yield return Y;
        yield return Z;
        // ...
    }
}

如果您。不希望有打电话给你可以做到这一点的方法

If you don't want to have to call the method you can do this.

public partial class Sheet : IEnumerable<Item>
{
    public Item X{ get; set; }
    public Item Y{ get; set; }
    public Item Z{ get; set; }

    public IEnumerator<Item> GetEnumerator()
    {
        yield return X;
        yield return Y;
        yield return Z;
        // ...
    }

    IEnumerator IEnumerator.GetEnumerator()
    {
        return GetEnumerator();
    }
}

这篇关于我如何实现IEnumerable?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆