IList<T> Interface

System.Collections.Generic

Represents a strongly typed list of objects that can be accessed by index. Provides methods for adding, removing, and manipulating elements.

public interface IList<T> : ICollection<T>, IEnumerable<T>, IEnumerable

The IList<T> interface is a generic collection interface that represents a strongly typed list of objects that can be accessed by index. It inherits from ICollection<T> and IEnumerable<T>, providing additional indexed access and modification capabilities.

Remarks

The IList<T> interface enables random access to elements by index, as well as the ability to insert and remove elements at arbitrary positions. It is a fundamental interface for many collection types in the .NET Framework, such as List<T>.

Implementations of IList<T> should provide thread safety guarantees if required, although the interface itself does not mandate it.

Indexers

Name Type Description
this[int index] T Gets or sets the element at the specified index.

Parameters:
  • index: The zero-based index of the element to get or set.
Exceptions:
  • ArgumentOutOfRangeException: index is less than 0 or index is equal to or greater than Count.
  • NotSupportedException: The property is set and the IList is read-only.

Methods

Name Description
int Add(T item) Adds an object to the end of the IList<T>.
void Clear() Removes all objects from the IList<T>.
bool Contains(T item) Determines whether an element is in the IList<T>.
int IndexOf(T item) Determines the index of a specific item in the IList<T>.
void Insert(int index, T item) Inserts an item into the IList<T> at the specified index.
void Remove(T item) Removes the first occurrence of a specific object from the IList<T>.
void RemoveAt(int index) Removes the element at the specified index of the IList<T>.

Example

Using IList<T> with List<T>

The following example demonstrates how to use the IList<T> interface with a List<T> object to add, access, and remove elements.


using System;
using System.Collections.Generic;

public class Example
{
    public static void Main()
    {
        // Declare an IList of strings, initialized with a List
        IList<string> names = new List<string>();

        // Add elements using the Add method
        names.Add("Alice");
        names.Add("Bob");
        names.Add("Charlie");

        // Access elements by index using the indexer
        Console.WriteLine($"Element at index 1: {names[1]}"); // Output: Element at index 1: Bob

        // Insert an element at a specific index
        names.Insert(1, "David");
        Console.WriteLine($"After insertion, element at index 1: {names[1]}"); // Output: After insertion, element at index 1: David

        // Check if an item exists
        if (names.Contains("Alice"))
        {
            Console.WriteLine("Alice is in the list.");
        }

        // Remove an element by value
        names.Remove("Bob");

        // Remove an element by index
        names.RemoveAt(0);

        // Iterate through the list
        Console.WriteLine("\nRemaining elements:");
        foreach (string name in names)
        {
            Console.WriteLine(name);
        }
        // Output:
        // David
        // Charlie
    }
}