Implementing IList interface
Asked Answered
J

6

21

I am new to generics. I want to implement my own collection by deriving it from IList<T> interface.

Can you please provide me some link to a class that implements IList<T> interface or provide me a code that at least implements Add and Remove methods?

Jollenta answered 24/7, 2009 at 8:33 Comment(0)
D
16

Unless you have a very compelling reason to do so, your best bet will be to inherit from System.Collections.ObjectModel.Collection<T> since it has everything you need.

Please note that although implementors of IList<T> are not required to implement this[int] (indexer) to be O(1) (basically, constant-time access), it's strongly recommended you do so.

Demonize answered 24/7, 2009 at 8:43 Comment(0)
R
36

In addition to deriving from List<T>, you can facade List<T> and add more features to your facade class.

class MyCollection<T> : IList<T>
{
    private readonly IList<T> _list = new List<T>();

    #region Implementation of IEnumerable

    public IEnumerator<T> GetEnumerator()
    {
        return _list.GetEnumerator();
    }

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

    #endregion

    #region Implementation of ICollection<T>

    public void Add(T item)
    {
        _list.Add(item);
    }

    public void Clear()
    {
        _list.Clear();
    }

    public bool Contains(T item)
    {
        return _list.Contains(item);
    }

    public void CopyTo(T[] array, int arrayIndex)
    {
        _list.CopyTo(array, arrayIndex);
    }

    public bool Remove(T item)
    {
        return _list.Remove(item);
    }

    public int Count
    {
        get { return _list.Count; }
    }

    public bool IsReadOnly
    {
        get { return _list.IsReadOnly; }
    }

    #endregion

    #region Implementation of IList<T>

    public int IndexOf(T item)
    {
        return _list.IndexOf(item);
    }

    public void Insert(int index, T item)
    {
        _list.Insert(index, item);
    }

    public void RemoveAt(int index)
    {
        _list.RemoveAt(index);
    }

    public T this[int index]
    {
        get { return _list[index]; }
        set { _list[index] = value; }
    }

    #endregion

    #region Your Added Stuff

    // Add new features to your collection.

    #endregion
}
Reamer answered 24/7, 2009 at 8:59 Comment(6)
But in this case I can't bind my collection to DataGridView rather I have to expose the _list member in MyCollection.Jollenta
What are the drawbacks of doing this?Correction
Sorry for the late response, but this one lost me @ IEnumerator<T> GetEnumerator returning GetEnumerator. This looks like a circular reference. How does this not cause a Stack Overflow Exception?Phemia
@jp2code it's not an infinite loop. It is calling IEnumerator<T> GetEnumerator(). I have added a comment to the sample stating that.Autocorrelation
Does IList<T> also implement IList? I thought it would, but I'm getting a compiler error. I guess List<T> does, but extending MyCollection so it can be cast as IList means also implementing that interface?Syncrisis
marsh-wiggle's answer uses VS to generate most of the above, automatically.Ghazi
D
16

Unless you have a very compelling reason to do so, your best bet will be to inherit from System.Collections.ObjectModel.Collection<T> since it has everything you need.

Please note that although implementors of IList<T> are not required to implement this[int] (indexer) to be O(1) (basically, constant-time access), it's strongly recommended you do so.

Demonize answered 24/7, 2009 at 8:43 Comment(0)
S
15

Visual Studio offers an automatic full working implementation of interfaces like IList<>.

You need only to write something like this code:

public class MyCollection<T> : IList<T>
{
    // This line is important. Without it the auto implementation creates only
    // methods with "NotImplemented" exceptions
    readonly IList<T> _list = new List<T>();
}

(while the line

readonly IList<T> _list = new List<T>(); 

is the important one!)

enter image description here

Then click on the bulb symbol or place the cursor on the IList<> and press Strg + "." You will become several implementations offered, like:

enter image description here

Saponify answered 14/8, 2019 at 11:3 Comment(1)
exactly what i needed. should be accepted answer as this is the correct way to do it.Daisie
D
1

You can look at Mono project. There is available complete source codes, sou you can look how are some classes implemented. For example "System.Collections.Generics.List<T>".

Diagnostician answered 24/7, 2009 at 8:36 Comment(3)
anonsvn.mono-project.com/viewvc/trunk/mcs/class/corlib/…Ligament
after opening the home page of Mono project .. what should I download ??Jollenta
Updated link to Mono class - github.com/mono/mono/blob/master/mcs/class/corlib/…Pasteboard
C
1

In most cases you can simply use List<T> or derive from List<T>. If you derive from List<T> you will automatically get the implementation for Add and Remove.

Crispen answered 24/7, 2009 at 8:39 Comment(0)
J
0

Inheriting from List is often the quickest approach but can be limiting later on down the line if you need to inherit from another class (e.g. ContextBoundObject etc.). It's pretty quick to implement IList and as pointed out above, it gives a lot more flexibility.

Jeanette answered 4/2, 2011 at 10:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.