Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a custom collection in .NET 2.0

Hi I want to create my custom collection, I am deriving my custom collection class from CollectionBase class as below:

public class MyCollection : System.Collections.CollectionBase
{
    MyCollection(){}
    public void Add(MyClass item)
    {
        this.List.Add(item);
    }
}

class MyClass
{
    public string name;
}

Let me ask a few questions:

  1. Whether this approach is correct and new, as I working on .NET 3.5 framework.
  2. I want to expose this collection from my web service ( WCF) .How can I do that?
  3. Do I have to implement GetEnumerator?
  4. Whether this will Bind to DataGridView.
like image 237
Ashish Ashu Avatar asked Dec 07 '22 06:12

Ashish Ashu


2 Answers

Deriving from List<T> is somewhat pointless, especially now that it has the IEnumerable<T> constructor and the availability of extension methods. It has no virtual methods that you can override except Equals, GetHashCode, and ToString. (I suppose you could derive from List<T> if your goal was to implement Java's toString() functionality for lists.)

If you want to create your own strongly-typed collection class and possibly customize the collection behavior when items are add/removed, you want to derive from the new (to .NET 2.0) type System.Collections.ObjectModel.Collection<T>, which has protected virtual methods including InsertItem and RemoveItem that you can override to perform actions at those times. Be sure to read the documentation - this is a very easy class to derive from but you have to realize the difference between the public/non-virtual and protected/virtual methods. :)

public class MyCollection : Collection<int>
{
    public MyCollection()
    {
    }

    public MyCollection(IList<int> list)
        : base(list)
    {
    }

    protected override void ClearItems()
    {
        // TODO: validate here if necessary
        bool canClearItems = ...;
        if (!canClearItems)
            throw new InvalidOperationException("The collection cannot be cleared while _____.");

        base.ClearItems();
    }

    protected override void RemoveItem(int index)
    {
        // TODO: validate here if necessary
        bool canRemoveItem = ...;
        if (!canRemoveItem)
            throw new InvalidOperationException("The item cannot be removed while _____.");

        base.RemoveItem(index);
    }
}
like image 123
Sam Harwell Avatar answered Dec 29 '22 10:12

Sam Harwell


I think you would be better off using one of the container classes defined in System.Collections.Generic

  1. Whether this approach is correct and new, as I working on .NET 3.5 framework.
    • No. Use List or something instead.
  2. I want to expose this collection from my web service ( WCF) .How can I do that?
    • pass. haven't used WCF.
  3. Do I have to implement GetEnumerator?
    • not if you use one of the standard System.Collections.Generic container classes. It's already done for you
  4. Whether this will Bind to DataGridView.
    • Any of the standard collections that support IEnumerable will bind to controls pretty well. You could look at using IBindingListView if you want sorting and filtering.
like image 45
Hamish Smith Avatar answered Dec 29 '22 10:12

Hamish Smith