Coder Perfect

On many-many/one-many relationships, why use ICollection instead of IEnumerable or ListT>?

Problem

This is something I see a lot in tutorials, especially with navigation properties like ICollectionT>.

Is this a required feature of Entity Framework? Can I use IEnumerable?

What is the advantage of using ICollection over IEnumerable or even ListT>?

Asked by Jan Carlo Viray

Solution #1

What you choose will usually be determined by whatever approaches you require. ICollection> (MSDN: http://msdn.microsoft.com/en-us/library/92t2ye13.aspx) for a list of objects that needs to be iterated through and modified, and IEnumerable> (MSDN: http://msdn.microsoft.com/en-us/library/system.collections.ienumerable.aspx) for a list of objects that needs to be iterated through and List> for a list of objects that must be iterated through, updated, sorted, and so on (see http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx for a complete list).

Lazy loading comes into play when selecting the kind, to be more exact. Entity Framework navigation properties come with change tracking and are proxies by default. The virtual type must implement ICollection in order for the dynamic proxy to be used as a navigation property.

More details on Defining and Managing Relationships may be found here. MSDN

Answered by Travis J

Solution #2

Because the IEnumerableT> interface does not allow for adding, removing, or otherwise altering the collection, ICollectionT> is utilized.

Answered by Justin Niessner

Solution #3

In response to your inquiry concerning ListT>, I’ll say this:

ListT> is a class; defining an interface provides for additional implementation flexibility. “Why not IListT>?” is a better question.

Consider what IListT> adds to ICollectionT>: integer indexing, which means the elements have an arbitrary order and can be retrieved using that order as a reference. In most circumstances, this is unlikely to be useful because objects are likely to be ranked differently in different contexts.

Answered by phoog

Solution #4

ICollection and IEnumerable have some fundamental differences.

Simple Program:

using System;
using System.Collections;
using System.Collections.Generic;

namespace StackDemo
{
    class Program 
    {
        static void Main(string[] args)
        {
            List<Person> persons = new List<Person>();
            persons.Add(new Person("John",30));
            persons.Add(new Person("Jack", 27));

            ICollection<Person> personCollection = persons;
            IEnumerable<Person> personEnumeration = persons;

            // IEnumeration
            // IEnumration Contains only GetEnumerator method to get Enumerator and make a looping
            foreach (Person p in personEnumeration)
            {                                   
               Console.WriteLine("Name:{0}, Age:{1}", p.Name, p.Age);
            }

            // ICollection
            // ICollection Add/Remove/Contains/Count/CopyTo
            // ICollection is inherited from IEnumerable
            personCollection.Add(new Person("Tim", 10));

            foreach (Person p in personCollection)
            {
                Console.WriteLine("Name:{0}, Age:{1}", p.Name, p.Age);        
            }
            Console.ReadLine();

        }
    }

    class Person
    {
        public string Name { get; set; }
        public int Age { get; set; }
        public Person(string name, int age)
        {
            this.Name = name;
            this.Age = age;
        }
    }
}

Answered by Ramakrishnan

Solution #5

This is how I recall it:

Answered by user3918295

Post is based on https://stackoverflow.com/questions/10113244/why-use-icollection-and-not-ienumerable-or-listt-on-many-many-one-many-relatio