IList<T> Interface
Namespace: System.Collections.Generic
Assembly: System.Runtime.dll
Represents a strongly typed collection of objects that can be individually accessed by index. The most base interface for collections that can be accessed by index.
Remarks
The IList<T> interface inherits from the ICollection<T> interface, which in turn inherits from the IEnumerable<T> interface.
An IList<T> can be accessed by an integer index. The elements are zero-based.
This interface supports the following operations:
- Adding elements.
- Removing elements.
- Inserting elements.
- Accessing elements by index.
- Searching for elements.
The implementations of IList<T> can be read-only, in which case they throw an NotSupportedException if an attempt is made to add or remove elements.
Inheritance Hierarchy
System.ObjectSystem.Collections.Generic.IEnumerable<T>System.Collections.ICollectionSystem.Collections.Generic.ICollection<T>System.Collections.Generic.IList<T>
Members
Properties
| Name | Description |
|---|---|
Count |
Gets the number of elements contained in the ICollection<T>. (Inherited from ICollection<T>) |
IsFixedSize |
Gets a value indicating whether the IList has a fixed size. |
IsReadOnly |
Gets a value indicating whether the ICollection<T> is read-only. (Inherited from ICollection<T>) |
Item[Int32] |
Gets or sets the element at the specified index. |
Methods
| Name | Description |
|---|---|
Add(T element) |
Adds an item to the end of the IList<T>. |
Clear() |
Removes all items from the ICollection<T>. (Inherited from ICollection<T>) |
Contains(T element) |
Determines whether the ICollection<T> contains a specific value. (Inherited from ICollection<T>) |
CopyTo(T[] array, Int32 arrayIndex) |
Copies the entire ICollection<T> to a compatible one-dimensional Array, starting at the specified array index. (Inherited from ICollection<T>) |
IndexOf(T element) |
Determines the zero-based index of the first occurrence of a value in the IList<T>. |
Insert(Int32 index, T element) |
Inserts an item at the specified position in the IList<T>. |
Remove(T element) |
Removes the first occurrence of a specific object from the ICollection<T>. (Inherited from ICollection<T>) |
RemoveAt(Int32 index) |
Removes the IList<T> item at the specified index. |
Explicit Interface Implementations
| Interface | Name | Description |
|---|---|---|
System.Collections.IList |
System.Collections.IList.Add(Object value) |
Adds an Object to the IList. |
System.Collections.IList |
System.Collections.IList.Contains(Object value) |
Determines whether an element is in the IList. |
System.Collections.IList |
System.Collections.IList.IndexOf(Object value) |
Determines the zero-based index of the first occurrence of a value that matches the object. |
System.Collections.IList |
System.Collections.IList.Insert(Int32 index, Object value) |
Inserts an element into the IList at the specified index. |
System.Collections.IList |
System.Collections.IList.Remove(Object value) |
Removes the first occurrence of the specified Object from the IList. |
System.Collections.IList |
System.Collections.IList.RemoveAt(Int32 index) |
Removes the element at the specified index from the IList. |
Examples
C# Example
using System;
using System.Collections.Generic;
public class Example
{
public static void Main(string[] args)
{
// Create a list of strings using the IList interface
IList<string> stringList = new List<string>();
// Add elements
stringList.Add("Apple");
stringList.Add("Banana");
stringList.Add("Cherry");
Console.WriteLine("Initial list:");
foreach (string fruit in stringList)
{
Console.WriteLine($"- {fruit}");
}
// Access element by index
Console.WriteLine($"\nElement at index 1: {stringList[1]}");
// Insert an element
stringList.Insert(1, "Blueberry");
Console.WriteLine("\nList after inserting 'Blueberry' at index 1:");
foreach (string fruit in stringList)
{
Console.WriteLine($"- {fruit}");
}
// Check if an element exists
bool containsBanana = stringList.Contains("Banana");
Console.WriteLine($"\nDoes the list contain 'Banana'? {containsBanana}");
// Find the index of an element
int cherryIndex = stringList.IndexOf("Cherry");
Console.WriteLine($"Index of 'Cherry': {cherryIndex}");
// Remove an element
stringList.Remove("Banana");
Console.WriteLine("\nList after removing 'Banana':");
foreach (string fruit in stringList)
{
Console.WriteLine($"- {fruit}");
}
// Remove element at index
stringList.RemoveAt(0); // Removes "Apple"
Console.WriteLine("\nList after removing element at index 0:");
foreach (string fruit in stringList)
{
Console.WriteLine($"- {fruit}");
}
// Clear the list
stringList.Clear();
Console.WriteLine($"\nList count after clearing: {stringList.Count}");
}
}
Note: The System.Collections.Generic.List<T> class is a common implementation of the IList<T> interface.