Welcome to the “Important Interface in C#” article series. In this series we will be talking about various useful interfaces of the .NET class library. In our previous article we have talked about the IEnumerable interface, you can read it here.
Important Interface in .NET: Work with Ienumerable Interface
In this presentation we will understand the ICollection interface of the .NET class library. We can say that the ICollection interface is the base interface of all collection types in the .NET library. And the uses of the ICollection interface is every now and then in application development. You might think, no I have never used the ICollection interface in my day-to-day coding life. Let me ask a small question. Have you used List or any other type of collection in .NET? Haha, your answer is yes. Then you have used the ICollection interface without your knowledge.
There are the following two versions of the ICollection interface:
- ICollection: without parameter
- ICollection<T>: with parameter of type T
The ICollection interface is located in the following place of the .NET class library:
- Namespace: System.Collections
- Assembly: mscorlib.dll
And the ICollection<T> is located in the following location:
- Namespace: System.collection.Generics
- Assembly: mscorlib.dll
Here is a different version of the ICollection interface.

Fine, so far we have understood the various versions of the ICollection interface and their location in the .NET class library.
Definition of ICollection and the ICollection<T> interface
There are three properties (all are read only in nature) and one method in the ICollection interface.
public interface ICollection : IEnumerable
{
int Count { get; }
bool IsSynchronized { get; }
Object SyncRoot { get; }
void CopyTo(Array array, int index);
}
ICollection inherits from IEnumerable, so that we can iterate an object collection of the ICollection interface (for example List and List<T>) using a foreach loop.
Here is the body of the generic ICollection<T> interface.
public interface ICollection<T> : IEnumerable<T>, IEnumerable
{
int Count { get; }
bool IsReadOnly { get; }
void Add(T item);
void Clear();
bool Contains(T item);
void CopyTo(T[] array, int arrayIndex);
bool Remove(T item);
}
It contains two read only properties and five methods. We are seeing that ICollection<T> implements IEnumerable<T> and IEnumerable interfaces. Those methods are simple to understand, here I am trying to provide a small description.
- Add: Adds an item to the ICollection<T>.
- Clear: Removes all items from the ICollection<T>.
- Contains: Determines whether the ICollection<T> contains a specific value.
- CopyTo: Copies the elements of the ICollection<T> to an Array, starting at a specific Array index.
- GetEnumerator: Returns an enumerator that iterates through the collection. (Inherited from IEnumerable<T>.)
- Remove: Removes the first occurrence of a specific object from the ICollection<T>.
Example of ICollection<T>
We will now implement an ICollection<T> interface in the following example. As we know T represents type, so we can pass any data type in place of T, it does not matter whether T is user defined or pre-defined. Have a look at the following example.
static void Main(string[] args)
{
//Premitive data type in place of T in Icollection
ICollection<string> names = new List<string>();
names.Add("Sourav Kayal");
names.Add("Soumen Das");
names.Add("Rohit");
var v = from n in names.Where(m=>m.Contains("S")) select n;
foreach (string s in v)
{
Console.WriteLine(s);
}
Console.ReadLine();
}



Kant NikhilPosted Jan 12, 2021, 9:18 AM
Nice content
Lalit RaghavPosted Sep 11, 2015, 3:48 AM
amazing