IList<T> Interface
Interface
.NET Core 1.0
.NET Framework 1.1
Namespace: System.Collections.Generic
Represents a strongly typed collection of objects that can be accessed by index. The sealed classes that implement IList<T>
inherit from the Collection<T>
class or use the IList<T>
interface directly.
Methods
Add
void Add(T item);
Adds an item to the end of the IList<T>
.
Contains
bool Contains(T item);
Determines whether the IList<T>
contains a specific value.
IndexOf
int IndexOf(T item);
Determines the index of a specific item in the IList<T>
.
Insert
void Insert(int index, T item);
Inserts an item at the specified index of the IList<T>
.
Remove
void Remove(T item);
Removes the first occurrence of a specific item from the IList<T>
.
RemoveAt
void RemoveAt(int index);
Removes the item at the specified index from the IList<T>
.
Properties
this[int index]
T this[int index] { get; set; }
Gets or sets the element at the specified index.
Examples
C# Example
// Create a list of strings.
IList<string> words = new List<string>();
words.Add("Hello");
words.Add("World");
words.Add("!");
// Access an element by index.
string firstWord = words[0]; // firstWord is "Hello"
// Insert an element at a specific position.
words.Insert(1, "Beautiful"); // words is now ["Hello", "Beautiful", "World", "!"]
// Check if an item exists.
bool containsWorld = words.Contains("World"); // containsWorld is true
// Remove an element.
words.Remove("!"); // words is now ["Hello", "Beautiful", "World"]