IList Interface
Represents a non-generic collection of objects that can be individually accessed by index.
Syntax
public interface IList : ICollection, IEnumerable
Remarks
The IList
interface is the non-generic equivalent of the System.Collections.Generic.IList<T>
interface.
It represents a strongly typed list of objects that can be accessed by index.
Methods and properties of the IList
interface are used to add, remove, insert, and retrieve elements from the list.
The order of elements in a generic list is an important aspect. For example, the Insert
method adds an element at a specified index.
The Count
property returns the number of elements in the list, and the Item
property (get/set indexer) retrieves or sets the element at a specified index.
This interface supports a fixed size, but it does not impose any restrictions on the type of elements that can be stored in the list.
For a generic implementation, see System.Collections.Generic.IList<T>
.
Members
Properties
Name | Description |
---|---|
IsFixedSize |
Gets a value indicating whether the IList has a fixed size. |
IsReadOnly |
Gets a value indicating whether the IList is read-only. |
Count |
Gets the number of elements contained in the ICollection . |
IsSynchronized |
Gets a value indicating whether access to the ICollection is synchronized (thread-safe). |
SyncRoot |
Gets an object that can be used to synchronize access to the ICollection . |
Methods
Name | Description |
---|---|
Add(object value) |
Adds an element to the end of the IList . |
Clear() |
Removes all elements from the IList . |
Contains(object value) |
Determines whether an element is in the IList . |
IndexOf(object value) |
Determines the index of a specific item in the IList . |
Insert(int index, object value) |
Inserts an element into the IList at the specified index. |
Remove(object value) |
Removes the first occurrence of a specific object from the IList . |
RemoveAt(int index) |
Removes the element at the specified index from the IList . |
CopyTo(Array array, int index) |
Copies the entire IList to a compatible one-dimensional Array , starting at the specified destination index. |
GetEnumerator() |
Returns an enumerator that iterates through the IList . |
Indexer
Name | Description |
---|---|
this[int index] |
Gets or sets the element at the specified index. |
Example
C# Example
using System;
using System.Collections;
public class IListExample
{
public static void Main(string[] args)
{
// Using ArrayList which implements IList
IList myList = new ArrayList();
myList.Add("Apple");
myList.Add("Banana");
myList.Add("Cherry");
Console.WriteLine($"Count: {myList.Count}");
Console.WriteLine($"Contains 'Banana': {myList.Contains("Banana")}");
Console.WriteLine("Elements:");
foreach (var item in myList)
{
Console.WriteLine($"- {item}");
}
Console.WriteLine($"Index of 'Banana': {myList.IndexOf("Banana")}");
myList.Remove("Banana");
Console.WriteLine("After removing 'Banana':");
foreach (var item in myList)
{
Console.WriteLine($"- {item}");
}
myList.Insert(1, "Blueberry");
Console.WriteLine("After inserting 'Blueberry' at index 1:");
for (int i = 0; i < myList.Count; i++)
{
Console.WriteLine($"[{i}]: {myList[i]}");
}
Console.WriteLine($"Element at index 0: {myList[0]}");
}
}
Requirements
Assembly | DLL |
---|---|
mscorlib | mscorlib.dll |