IList<T> Interface
The System.Collections.Generic.IList<T> interface is a generic interface that represents an ordered collection of objects that can be accessed by index. It inherits from ICollection<T>, which in turn inherits from IEnumerable<T>, providing additional functionality beyond simple iteration and counting.
Syntax
public interface IList<out T> : ICollection<T>
Remarks
An IList<T> provides capabilities for:
- Accessing elements by their zero-based index.
- Inserting elements at a specific index.
- Removing elements from a specific index.
- Searching for elements within the list.
- Getting or setting an element at a specific index.
Implementations of IList<T> can vary in performance characteristics. For example, List<T> offers O(1) time complexity for most operations (add, remove at index, access by index), while other implementations might have different performance profiles.
Methods
| Method | Description |
|---|---|
int Add(T element) |
Adds an item to the end of the IList<T>. |
void Clear() |
Removes all items from the IList<T>. |
bool Contains(T element) |
Determines whether an element is in the IList<T>. |
int IndexOf(T element) |
Searches for the specified object and returns the zero-based index of the first occurrence within the entire IList<T>. |
void Insert(int index, T element) |
Inserts an item at the specified index. |
void Remove(T element) |
Removes the first occurrence of a specific object from the IList<T>. |
void RemoveAt(int index) |
Removes the element at the specified index. |
Properties
| Property | Description |
|---|---|
T this[int index] { get; set; } |
Gets or sets the element at the specified index. |
int Count { get; } |
Gets the number of elements contained in the IList<T>. (Inherited from ICollection<T>) |
bool IsReadOnly { get; } |
Gets a value indicating whether the IList<T> has a fixed size. (Inherited from ICollection<T>) |
Example
C# Example
using System;
using System.Collections.Generic;
public class Example
{
public static void Main(string[] args)
{
// Create a List, which implements IList
IList<string> fruits = new List<string>();
// Add elements
fruits.Add("Apple");
fruits.Add("Banana");
fruits.Add("Cherry");
Console.WriteLine("Initial list:");
foreach (var fruit in fruits)
{
Console.WriteLine($"- {fruit}");
}
// Access and modify an element
Console.WriteLine($"\nElement at index 1: {fruits[1]}");
fruits[1] = "Blueberry";
Console.WriteLine($"Updated element at index 1: {fruits[1]}");
// Insert an element
fruits.Insert(1, "Apricot");
Console.WriteLine("\nList after inserting 'Apricot' at index 1:");
foreach (var fruit in fruits)
{
Console.WriteLine($"- {fruit}");
}
// Check if an element exists
Console.WriteLine($"\nContains 'Banana'? {fruits.Contains("Banana")}");
// Find the index of an element
Console.WriteLine($"Index of 'Cherry': {fruits.IndexOf("Cherry")}");
// Remove an element
fruits.Remove("Apple");
Console.WriteLine("\nList after removing 'Apple':");
foreach (var fruit in fruits)
{
Console.WriteLine($"- {fruit}");
}
// Remove an element by index
fruits.RemoveAt(0); // Removes 'Apricot'
Console.WriteLine("\nList after removing element at index 0:");
foreach (var fruit in fruits)
{
Console.WriteLine($"- {fruit}");
}
Console.WriteLine($"\nTotal elements: {fruits.Count}");
}
}