Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I implement IEnumerable?

Tags:

c#

ienumerable

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; }
}

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 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 );
}

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.

like image 354
Ciel Avatar asked May 07 '10 14:05

Ciel


People also ask

What is IEnumerable in C# with example?

IEnumerable is an interface defining a single method GetEnumerator() that returns an IEnumerator interface. It is the base interface for all non-generic collections that can be enumerated. This works for read-only access to a collection that implements that IEnumerable can be used with a foreach statement.

How do you make a class IEnumerable in C#?

So to implement IEnumerable , you must also implement IEnumerator . If you don't implement IEnumerator , you can't cast the return value from the GetEnumerator method of IEnumerable to the IEnumerator interface. In summary, the use of IEnumerable requires that the class implement IEnumerator .

Does List implement IEnumerable?

List implements IEnumerable, but represents the entire collection in memory. LINQ expressions return an enumeration, and by default the expression executes when you iterate through it using a foreach, but you can force it to iterate sooner using .

Does string implement IEnumerable?

In C#, all collections (eg lists, dictionaries, stacks, queues, etc) are enumerable because they implement the IEnumerable interface. So are strings. You can iterate over a string using a foreach block to get every character in the string.


1 Answers

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

Using Iterators

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();
    }
}
like image 128
ChaosPandion Avatar answered Oct 11 '22 13:10

ChaosPandion