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 IList do in C#?
The IList interface in C# represents a non-generic collection of objects that can be individually accessed by index. It provides indexed access to elements, allowing you to retrieve, modify, add, and remove items at specific positions within the collection.
The IList interface combines the functionality of ICollection and IEnumerable, making it suitable for collections that need both indexed access and dynamic resizing capabilities.
Syntax
Following is the syntax for declaring an IList variable −
IList list = new ArrayList(); IList list = new List<object>();
Following is the syntax for accessing elements by index −
object item = list[index]; list[index] = newValue;
Properties of IList Interface
| Property | Description |
|---|---|
| Count | Gets the number of elements contained in the IList. |
| IsFixedSize | Gets a value indicating whether the IList has a fixed size. |
| IsReadOnly | Gets a value indicating whether the IList is read-only. |
| IsSynchronized | Gets a value indicating whether access to the IList is synchronized. |
| Item[Int32] | Gets or sets the element at the specified index. |
Methods of IList Interface
| Method | Description |
|---|---|
| Add(Object) | Adds an item to the IList and returns the index of the added item. |
| Clear() | Removes all items from the IList. |
| Contains(Object) | Determines whether the IList contains a specific value. |
| IndexOf(Object) | Determines the index of a specific item in the IList. |
| Insert(Int32, Object) | Inserts an item to the IList at the specified index. |
| Remove(Object) | Removes the first occurrence of a specific object from the IList. |
| RemoveAt(Int32) | Removes the IList item at the specified index. |
Using IList with ArrayList
The ArrayList class implements the IList interface and provides dynamic array functionality −
Example
using System;
using System.Collections;
class Program {
public static void Main() {
IList list = new ArrayList();
// Adding elements
list.Add("Apple");
list.Add("Banana");
list.Add("Cherry");
list.Insert(1, "Orange");
Console.WriteLine("List contents:");
for (int i = 0; i < list.Count; i++) {
Console.WriteLine($"Index {i}: {list[i]}");
}
Console.WriteLine($"\nCount: {list.Count}");
Console.WriteLine($"Contains 'Banana': {list.Contains("Banana")}");
Console.WriteLine($"Index of 'Cherry': {list.IndexOf("Cherry")}");
// Remove element
list.Remove("Orange");
Console.WriteLine($"\nAfter removing 'Orange', Count: {list.Count}");
}
}
The output of the above code is −
List contents: Index 0: Apple Index 1: Orange Index 2: Banana Index 3: Cherry Count: 4 Contains 'Banana': True Index of 'Cherry': 3 After removing 'Orange', Count: 3
Using IList with Generic List
You can also use IList with generic List<T> for better type safety −
Example
using System;
using System.Collections;
using System.Collections.Generic;
class Program {
public static void Main() {
IList numbers = new List<int> { 10, 20, 30 };
numbers.Add(40);
numbers.Insert(0, 5);
Console.WriteLine("Numbers in the list:");
foreach (object num in numbers) {
Console.WriteLine(num);
}
Console.WriteLine($"\nFirst element: {numbers[0]}");
Console.WriteLine($"Last element: {numbers[numbers.Count - 1]}");
// Modify element at index 2
numbers[2] = 25;
Console.WriteLine($"Modified element at index 2: {numbers[2]}");
}
}
The output of the above code is −
Numbers in the list: 5 10 20 30 40 First element: 5 Last element: 40 Modified element at index 2: 25
Common Use Cases
-
Dynamic Collections: When you need a resizable collection with indexed access.
-
Polymorphism: When working with different list implementations through a common interface.
-
Legacy Code: Interfacing with older .NET code that uses non-generic collections.
-
Mixed Data Types: When storing objects of different types in a single collection.
Conclusion
The IList interface provides indexed access to collections, combining enumeration and collection capabilities. It serves as a foundation for dynamic, resizable collections like ArrayList and can be used polymorphically with different list implementations for flexible data management.
