tags:

views:

57

answers:

2

If I have a Customer class and want to add a Orders property which would return all orders for this customer, what would be the appropriate type for the Orders property?

Some of the possible choices I can think of:

  • List
  • IList
  • IEnumerable
+1  A: 

ReadOnlyCollection<Order> or IEnumerable<Order> are both good choices. If you return IEnumerable<T>, make sure the user cannot cast the returned object into another type and modify it (or, if they do, they will not modify the state of the Customer class).

Igor ostrovsky
How would I make sure the use cannot cast into another type?
(meant to type: "user" not "use"!!!)
+3  A: 

Generally the least possible type is preferable. If you can get away with IEnumerable - do it. Then IList<> etc. The more basic the list type, the easier it will be to work with the class later in the app's life.

Rob Conery
The Orders property will be populated by calling repository.Find<Order>(x => x.CustomerId == this.CustomerId) which returns an IList. Therefore, doesn't it makes sense to use IList as the type for the Orders property?