Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implement IEnumerable<T> For a List Wrapper

Tags:

c#

ienumerable

I have a class, which is just a wrapper over a list, i.e.,

public class Wrapper
{
   public List<int> TList
   {get;set;}
   public Wrapper()
   {
      TList=new List<int>();
   }
}

I want to make Wrapper inherits from IEnumerable so that I can use the following syntax:

Wrapper wrapper = new Wrapper()
                       {
                         2,4,3,6 
                       };

Any idea how to which interface to implement IEnumerable<T>, or IEnumerable, and how to define the method body?

like image 525
Graviton Avatar asked Dec 12 '09 08:12

Graviton


People also ask

Does list t 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 .

How do you use IEnumerable T?

IEnumerable<T> contains a single method that you must implement when implementing this interface; GetEnumerator, which returns an IEnumerator<T> object. The returned IEnumerator<T> provides the ability to iterate through the collection by exposing a Current property.

Is IEnumerable faster than list C#?

IEnumerable is conceptually faster than List because of the deferred execution. Deferred execution makes IEnumerable faster because it only gets the data when needed. Contrary to Lists having the data in-memory all the time.


2 Answers

If you implement ICollection<int> you get the desired functionality.

Correction: you actually only need to implement IEnumerable or IEnumerable<T> and have a public Add method in your class:

public class Wrapper : IEnumerable<int>
{
    public List<int> TList
    { get; private set; }
    public Wrapper()
    {
        TList = new List<int>();
    }

    public void Add(int item)
    {
        TList.Add(item);
    }
    public IEnumerator<int> GetEnumerator()
    {
        return TList.GetEnumerator();
    }

    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }
}

(I also took the liberty of making the TList setter private; it is usually recommended that collection type properties are read-only so that the collection as such can not be substituted by any code outside the type.)

like image 102
Fredrik Mörk Avatar answered Sep 21 '22 17:09

Fredrik Mörk


In order to get collection initializers you need to do 2 things:

  1. Implement IEnumerable
  2. Have a method called Add with the correct signature

The preferable way to get these is to implement ICollection, but the minimum you need to do is:

public class Wrapper : IEnumerable<int>
{
    public List<int> TList
    {get;set;}

    public IEnumerator<int> GetEnumerator()
    {
        return TList.GetEnumerator();
    }

    IEnumerator IEnumerable.GetEnumerator() // Explicitly implement the non-generic version.
    {
        return TList.GetEnumerator();
    }

    public void Add(int i)
    {
         TList.Add(i);
    }

    public Wrapper()
    {
        TList=new List<int>();
    }
}
like image 21
ICR Avatar answered Sep 22 '22 17:09

ICR