Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
What does the interface ICollection do in C#
The ICollection interface in C# defines the size, enumerators, and synchronization methods for all nongeneric collections. It is the base interface for classes in the System.Collections namespace and serves as the foundation for collection types like ArrayList, Hashtable, and Queue.
This interface provides essential functionality that all collection classes must implement, including methods to copy elements and enumerate through the collection, along with properties to get the count and synchronization object.
Syntax
Following is the syntax for implementing the ICollection interface −
public interface ICollection : IEnumerable
{
int Count { get; }
object SyncRoot { get; }
bool IsSynchronized { get; }
void CopyTo(Array array, int index);
}
Properties
The ICollection interface defines the following properties −
| Property | Description |
|---|---|
| Count | Gets the number of elements contained in the ICollection. |
| SyncRoot | Gets an object that can be used to synchronize access to the ICollection. |
| IsSynchronized | Gets a value indicating whether access to the ICollection is synchronized (thread-safe). |
Methods
The ICollection interface defines the following methods −
| Method | Description |
|---|---|
| CopyTo(Array, Int32) | Copies the elements of the ICollection to an Array, starting at a particular Array index. |
| GetEnumerator() | Returns an enumerator that iterates through a collection (inherited from IEnumerable). |
Using ICollection with ArrayList
Here's an example demonstrating how ArrayList implements ICollection −
using System;
using System.Collections;
class Program {
static void Main() {
ArrayList arrayList = new ArrayList();
arrayList.Add("Apple");
arrayList.Add("Banana");
arrayList.Add("Cherry");
// Using ICollection properties
ICollection collection = arrayList;
Console.WriteLine("Count: " + collection.Count);
Console.WriteLine("IsSynchronized: " + collection.IsSynchronized);
// Using CopyTo method
string[] array = new string[collection.Count];
collection.CopyTo(array, 0);
Console.WriteLine("Elements copied to array:");
foreach (string item in array) {
Console.WriteLine("- " + item);
}
}
}
The output of the above code is −
Count: 3 IsSynchronized: False Elements copied to array: - Apple - Banana - Cherry
Using ICollection with Hashtable
The following example shows how Hashtable implements ICollection −
using System;
using System.Collections;
class Program {
static void Main() {
Hashtable hashtable = new Hashtable();
hashtable.Add("key1", "Value1");
hashtable.Add("key2", "Value2");
hashtable.Add("key3", "Value3");
ICollection collection = hashtable;
Console.WriteLine("Hashtable Count: " + collection.Count);
Console.WriteLine("Is Synchronized: " + collection.IsSynchronized);
// Copy keys to array using CopyTo
DictionaryEntry[] entries = new DictionaryEntry[collection.Count];
collection.CopyTo(entries, 0);
Console.WriteLine("Hashtable entries:");
foreach (DictionaryEntry entry in entries) {
Console.WriteLine(entry.Key + " = " + entry.Value);
}
}
}
The output of the above code is −
Hashtable Count: 3 Is Synchronized: False Hashtable entries: key3 = Value3 key2 = Value2 key1 = Value1
Custom Collection Implementation
You can create custom collections by implementing the ICollection interface −
using System;
using System.Collections;
class CustomCollection : ICollection {
private object[] items;
private int count;
public CustomCollection(int capacity) {
items = new object[capacity];
count = 0;
}
public int Count => count;
public object SyncRoot => this;
public bool IsSynchronized => false;
public void Add(object item) {
if (count < items.Length) {
items[count++] = item;
}
}
public void CopyTo(Array array, int index) {
for (int i = 0; i < count; i++) {
array.SetValue(items[i], index + i);
}
}
public IEnumerator GetEnumerator() {
for (int i = 0; i < count; i++) {
yield return items[i];
}
}
}
class Program {
static void Main() {
CustomCollection custom = new CustomCollection(5);
custom.Add("First");
custom.Add("Second");
custom.Add("Third");
Console.WriteLine("Custom collection count: " + custom.Count);
Console.WriteLine("Elements in custom collection:");
foreach (object item in custom) {
Console.WriteLine("- " + item);
}
}
}
The output of the above code is −
Custom collection count: 3 Elements in custom collection: - First - Second - Third
Conclusion
The ICollection interface provides the fundamental structure for all nongeneric collections in C#, defining essential properties like Count and methods like CopyTo(). It serves as the base interface that ensures consistent behavior across different collection types in the System.Collections namespace.
