6

What are differences between Collection and List in C#. I mean which one should I use for navigation properties:

public Collection<OrderDetail> OrderDetails { get; set; }

OR

public List<OrderDetail> OrderDetails { get; set; }
Mustafa Özçetin
  • 1,893
  • 1
  • 14
  • 16
Mike L
  • 65
  • 6

3 Answers3

6

There appear to be no docs for this, but Entity Framework supports any IEnumerable<T> for collection navigation properties.

So you can just declare them as:

  • IEnumerable<T>
  • ICollection<T>
  • Collection<T>
  • HashSet<T>
  • IList<T>
  • List<T>

And probably more. As far as Entity Framework is concerned, there's no difference, it'll assign the appropriate one at runtime and can do all it does with each one, apart from calling AddRange() on most of them, but when do you want to use that?

If you want databinding, you could use an ObservableCollection<T>, for example, otherwise I'd go with the leanest interface that still allows adding items: ICollection<T>.

CodeCaster
  • 147,647
  • 23
  • 218
  • 272
  • and how does this answer the "where are the differences"? If or if not EF is used makes no difference on the topic if there are differences between the collection-types, IMHO. – MakePeaceGreatAgain Nov 25 '21 at 13:01
  • Thanks, was still updating. Better? – CodeCaster Nov 25 '21 at 13:03
  • I'd still argue that EF is completely irrelevant here and thus the dupe applies. – MakePeaceGreatAgain Nov 25 '21 at 13:04
  • 1
    Why do you think so? The OP is asking about this specifically in an Entity Framework context, see tags. When you want to model a one-to-many relationship on your entities, you can choose from many framework collection types, and I'm saying it doesn't matter which you pick as EF supports many of them, but that you may have benefits choosing one over the other (such as ObservableCollection), and that otherwise it doesn't matter. That's an entirely different question than API design. – CodeCaster Nov 25 '21 at 13:06
  • Actually the minimum requirement for **EF Core** is `IEnumerable`. And supported is everything which inherits/implements it. It can be seen from `HasMany`, `WithMany`, `ThenInclude`, `EntityEntry.Collection` and similar method signatures. – Ivan Stoev Nov 25 '21 at 16:37
  • @Ivan thanks, updated! But `IEnumerable` is not really a useful declared property type for this kind of usage, as you'll usually want to treat it as an in-memory collection (especially: adding items). – CodeCaster Nov 25 '21 at 17:02
  • Don't forget `BindingList` for data binding. – JAlex May 18 '23 at 12:24
3

Entity Framework Core 7

  • If the navigation is exposed as a HashSet<T>, then an instance of HashSet<T> using ReferenceEqualityComparer is created.
  • Otherwise, if the navigation is exposed as a concrete type with a parameterless constructor, then an instance of that concrete type is created. This applies to List<T>, but also to other collection types, including custom collection types.
  • Otherwise, if the navigation is exposed as an IEnumerable<T>, an ICollection<T>, or an ISet<T>, then an instance of HashSet<T> using ReferenceEqualityComparer is created.
  • Otherwise, if the navigation is exposed as an IList<T>, then an instance of List<T> is created.
  • Otherwise, an exception is thrown.

Source: MSDN

ˈvɔlə
  • 9,204
  • 10
  • 63
  • 89
-1

The entity frameworks supports the Collections more effective than the List . So better use Collection for your usage.

List represents a collection where the order of items is important. It also supports methods such as Sort and search.

Whereas Collection is a modifiable set. You can add and remove objects from the set, you can also get the count of items in the set. But there still is no order, and because there is no order: no way to access an item by index, nor is there any way to sort.

If you want to expose a custom data structure, you should probably extend the collection.

Arun_Raja1
  • 207
  • 1
  • 1
  • What do you mean by "supports the Collections more effective than the List"? – CodeCaster Nov 25 '21 at 13:16
  • Also, `Collection` does have an indexer property, and collection navigation properties are not populated in any defined order (https://stackoverflow.com/questions/7522784/order-navigation-properties-when-using-include-and-or-select-methods-with-ef-4-1). – CodeCaster Nov 25 '21 at 13:19