1

I have an object that contains a list of child objects. This is a very simple Order -> OrderLines type of relationship.

Now I'm trying to map out my class and have set up the object as an ICollection:

public virtual ICollection<OrderLines> OrderLinesCollection { get; set; }

However when I assign values to the collection, I am unsure on whether to use a HashSet or a List to hold my collection of OrderLines.

this.OrderLinesCollection = new HashSet<OrderLines>();
this.OrderLinesCollection = new List<OrderLines>();

What is the best way of doing this?

ShaunP
  • 463
  • 1
  • 5
  • 20
  • It depends on what kind of operations you want to do on the collection. – David S. Aug 04 '14 at 15:32
  • the best way depens on your needs. what do you need ? a HashSet or a List ? – Selman Genç Aug 04 '14 at 15:32
  • They are two very different data structures and the right one to choose depends on how you want to use them. Read up on what is a `List` and what is a `HashSet` and you'll be able to choose by yourself. – dee-see Aug 04 '14 at 15:32
  • Does `OrderLines` override `Equals`+`GetHashCode`? If the insertion order is relevant or you want to access via index you should use a `List` (or `T[]`). If the order doesn't matter and you want to find it quickly (or have faster `Contains`-check) use a `HashSet`. – Tim Schmelter Aug 04 '14 at 15:32
  • As, above however, you should consider whether you want to expose the property. I would have have a private List/ Hashset (Dictionary) and then a public getter of ICollection – Tony Hopkinson Aug 04 '14 at 15:35

3 Answers3

2

The choice between a List<T> and a HashSet<T> is straightforward:

  • If you need to maintain a specific order of iteration, use a List<T>
  • If you do not need a specific order of iteration, and you need a fast lookup, use HashSet<T>

Note that in order to be usable in a HashSet<OrderLine> your OrderLine objects must implement GetHashCode and Equals. Strictly speaking, they should do it anyway, but for hash-based containers it's a must.

Also note that exposing a setter of OrderLinesCollection as a public member puts you in a position where the choice of the container is not entirely up to you: anyone using your class could potentially set OrderLinesCollection to a HashSet or to a List, depending on their preferences. Unless you make the setter private, your class needs to be ready to deal with it.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
1

It depends on you requirements. If the order is important, use a List, if you often need to check if an item is contained in the collection, use a HashSet. A HashSet doesn't respect the order of the items.

For more information see for example this post: What is the difference between HashSet<T> and List<T>?

Community
  • 1
  • 1
Flat Eric
  • 7,971
  • 9
  • 36
  • 45
0

Well you have to think about your use case

From msdn: The HashSet class provides high-performance set operations. A set is a collection that contains no duplicate elements, and whose elements are in no particular order.

In my view a list is used when you have to add/delete elements.

So ask yourself, are you going to be adding/deleting elemnts? Is your data set going to have duplicate elements? If neither is the case and you just need something to hold your data, i would use an Array

user2202911
  • 2,898
  • 5
  • 20
  • 28