Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Having a collection in class [duplicate]

Tags:

c#

collections

There are several options when one class must have a container (collection) of some sort of objects and I was wondering what implementation I shall prefer.

Here follow the options I found:

public class AClass : IEnumerable<string>{
   private List<string> values = new List<string>()

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

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

Pros: AClass is not dependent on a concrete implementation of a collection (in this case List).

Cons: AClass doesn't have interface for Adding and removing elements

public class AClass : ICollection<string>{
   private List<string> values = new List<string>()

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

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

   //other ICollectionMembers
}

Pros: Same as IEnumerable plus it have interface for adding and removing elements

Cons: The ICollection interface define other methods that one rarely uses and it get's boring to implement those just for the sake of the interface. Also IEnumerable LINQ extensions takes care of some of those.

public class AClass : List<string>{

}

Pros: No need of implementing any method. Caller may call any method implemented by List

Cons: AClass is dependent on collection List and if it changes some of the caller code may need to be changed. Also AClass can't inherit any other class.

The question is: Which one shall I prefer to state that my class contains a collection supporting both Add and Remove operations? Or other suggestions...

like image 878
Bruno Costa Avatar asked Jan 20 '14 23:01

Bruno Costa


People also ask

Can a collection have duplicates?

A Set is a Collection that cannot contain duplicate elements. It models the mathematical set abstraction. The Set interface contains only methods inherited from Collection and adds the restriction that duplicate elements are prohibited.

Which collection doesn allow duplicates?

ArrayList allows duplicate values while HashSet doesn't allow duplicates values.


1 Answers

My suggestion is just define a generic List inside of your class and write additional Add and Remove methods like this and implement IEnumerable:

public class MyClass : IEnumerable
{
    private List<string> myList;

    public MyClass()
    {
        myList = new List<string>();
    }

    public void Add(string item)
    {
        if (item != null) myList.Add(item);
    }

    public void Remove(string item)
    {
        if (myList.IndexOf(item) > 0) myList.Remove(item);
    }

    public IEnumerable<string> MyList { get { return myList; } }

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

This is the best way if you don't want to implement your own collection.You don't need to implement an interface to Add and Remove methods.The additional methods like this fits your needs I guess.

like image 94
Selman Genç Avatar answered Oct 17 '22 18:10

Selman Genç