Complete Article: Collection Interface in C#
Collection Interfaces in C# are used to manage and manipulate groups of objects efficiently. They provide a standard way to work with collections like Lists, Dictionaries, Queues, Stacks, and Sets.
Namespace Used
using System.Collections; using System.Collections.Generic;
Why Collection Interfaces are Used
- Store multiple objects
- Access data efficiently
- Reuse code
- Provide flexibility
- Support polymorphism
- Hide internal implementation
1. IEnumerable Interface
Definition
IEnumerable is used to iterate through a collection using foreach loop.
Use
- Read data one by one
- Used in loops
Example
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
IEnumerable<int> numbers = new List<int>()
{
10,20,30,40
};
foreach (int num in numbers)
{
Console.WriteLine(num);
}
}
}
2. ICollection Interface
Definition
ICollection represents a collection of objects.
Use
- Add items
- Remove items
- Count items
Example
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
ICollection<string> students = new List<string>();
students.Add("Rahul");
students.Add("Aman");
Console.WriteLine("Total Students: " + students.Count);
students.Remove("Rahul");
foreach (string name in students)
{
Console.WriteLine(name);
}
}
}
3. IList Interface
Definition
IList represents an ordered collection with index support.
Use
- Access elements using index
- Insert items at specific position
Example
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
IList<int> numbers = new List<int>();
numbers.Add(100);
numbers.Add(200);
numbers.Add(300);
Console.WriteLine(numbers[1]);
numbers.Insert(1, 150);
foreach (int num in numbers)
{
Console.WriteLine(num);
}
}
}
4. IDictionary Interface
Definition
IDictionary stores data in key-value pairs.
Use
- Fast searching using keys
- Store mapped data
Example
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
IDictionary<int, string> students =
new Dictionary<int, string>();
students.Add(1, "Shiva");
students.Add(2, "Rohan");
Console.WriteLine(students[1]);
foreach (KeyValuePair<int, string> item in students)
{
Console.WriteLine(item.Key + " " + item.Value);
}
}
}
5. ISet Interface
Definition
ISet stores only unique values.
Use
- Remove duplicate data
- Unique record management
Example
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
ISet<int> numbers = new HashSet<int>();
numbers.Add(10);
numbers.Add(20);
numbers.Add(10);
foreach (int num in numbers)
{
Console.WriteLine(num);
}
}
}
Generic Collection Interfaces
| Interface | Description |
|---|---|
| IEnumerable<T> | Iteration |
| ICollection<T> | Collection handling |
| IList<T> | Ordered collection |
| IDictionary<TKey,TValue> | Key-value collection |
| ISet<T> | Unique items |
Collection Interface Hierarchy
IEnumerable
↓
ICollection
↓
IList
Generic Hierarchy
IEnumerable<T>
↓
ICollection<T>
↓
IList<T>
Difference Between Collection Interfaces
| Interface | Index Support | Key-Value | Duplicate Allowed |
|---|---|---|---|
| IEnumerable | No | No | Yes |
| ICollection | No | No | Yes |
| IList | Yes | No | Yes |
| IDictionary | No | Yes | No Keys |
| ISet | No | No | No |
Advantages of Collection Interfaces
- Code reusability
- Flexibility
- Easy maintenance
- Better abstraction
- Type safety
- Polymorphism support
Real-Life Example
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
List<string> employees = new List<string>();
employees.Add("Amit");
employees.Add("Riya");
employees.Add("Karan");
foreach (string emp in employees)
{
Console.WriteLine(emp);
}
}
}
Common Collection Classes in C#
| Collection Class | Interface |
|---|---|
| List<T> | IList<T> |
| Dictionary<TKey,TValue> | IDictionary<TKey,TValue> |
| HashSet<T> | ISet<T> |
| Queue<T> | IEnumerable<T> |
| Stack<T> | IEnumerable<T> |
Interview Questions on Collection Interfaces
Q1. What is IEnumerable in C#?
Used for iteration using foreach.
Q2. Difference between IList and ICollection?
| IList | ICollection |
|---|---|
| Supports indexing | No indexing |
| Ordered collection | Basic collection |
Q3. Why use Generic Collections?
- Type safety
- Better performance
- No boxing/unboxing
Conclusion
Collection Interfaces in C# provide a standard and efficient way to manage groups of objects. They improve flexibility, maintainability, and code reusability in software development.
Most commonly used interfaces are:
- IEnumerable<T>
- ICollection<T>
- IList<T>
- IDictionary<TKey,TValue>
- ISet<T>
These interfaces are widely used in real-world C# applications and are very important for interviews and practical programming.
Why Use Collection Interfaces in C# if Collection Classes Already Exist?
Even though collection classes like List, Dictionary, and HashSet already exist, we use Collection Interfaces in C# because they provide flexibility, abstraction, reusable programming, and better software design.
1. Flexibility
Using interfaces allows developers to change the collection type later without changing much code.
Without Interface
List<string> names = new List<string>();
Now you can only use List.
With Interface
IList<string> names = new List<string>();
Later you can change to:
names = new Collection<string>();
without changing other code.
2. Loose Coupling
Interfaces reduce dependency on a specific class. Good software design always prefers:
Interface → not concrete class
Example
public void PrintNames(IEnumerable<string> names)
{
foreach(var name in names)
{
Console.WriteLine(name);
}
}
This method works with:
- List
- Array
- HashSet
- Queue
3. Code Reusability
One method can work with many collection types.
public void ShowData(ICollection<int> data)
{
Console.WriteLine(data.Count);
}
Works for:
- List<int>
- HashSet<int>
- Collection<int>
4. Abstraction
Interfaces hide internal implementation details.
You only focus on:
- Add
- Remove
- Search
You don't care about:
- Memory management
- Resizing logic
- Internal algorithms
5. Better Maintainability
Suppose today:
ICollection<int> numbers = new List<int>();
Tomorrow:
ICollection<int> numbers = new HashSet<int>();
Minimal code changes are required.
6. Supports Polymorphism
One interface can refer to multiple collection objects.
IEnumerable<int> data;
Can hold:
- List<int>
- int[]
- Queue<int>
- Stack<int>
Real-Life Analogy
Think of a USB Port.
You can connect:
- Keyboard
- Mouse
- Pen Drive
Same interface, different devices.
Similarly:
- ICollection
- IList
- IDictionary
provide common behavior for different collection classes.
Advantages of Collection Interfaces
- Flexibility
- Code Reusability
- Loose Coupling
- Abstraction
- Maintainability
- Polymorphism
Short Interview Answer
We use collection interfaces in C# because they provide flexibility, abstraction, loose coupling, code reusability, and polymorphism. Interfaces make applications easier to maintain and allow switching between different collection classes without changing much code.

0 Comments
POST Answer of Questions and ASK to Doubt