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
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:
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).
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.