Junior Collections

What is the difference between IEnumerable<T> and ICollection<T> in C#?

Feature IEnumerable

<T>

ICollection<T>

Read-only Yes No

Modification Not supported Supports Add, Remove,

Clear

Count property No Yes (Count)

Example:

IEnumerable<string> names = new List<string> { "A", "B" };

// names.Add("C"); // Error

ICollection<string> nameCollection = new List<string> { "A", "B" };

nameCollection.Add("C"); // Allowed

Real-world scenario:

Use IEnumerable<T> for read-only, query-focused tasks like LINQ. Use

ICollection<T> when managing and modifying the collection.

More from C# Programming Tutorial

All questions for this course