In C#, the IList interface is part of the .NET Framework and is defined in the System.Collections namespace. It represents a non-generic collection of objects that are individually accessed by index. The methods to add, remove, and access elements in a list-like structure are provided by the IList interface, which extends the ICollection interface.
Members in the IList Interface:
There are several members of the IList Interface in C#. Some main members of the IList Interface are as follows:
- Add: Adds a new item in the IList.
- Clear: Removes all items from the IList.
- Contains: Determines if a particular value is present in the IList.
- IndexOf: Finds the object's first occurrence and returns its index.
- Insert: Inserts a new element to the IList at the provided index.
- Remove: Removes the first occurrence of a specific object from the IList.
- RemoveAt: Removes the item at the given index.
- IsFixedSize: Gets a value that indicates if the size of the IList is fixed.
- IsReadOnly: Gets a value that specifies whether an IList can only be read.
- Item: At the given index, gets or sets the element.
Properties:
Implementations:
The IList interface is implemented by classes such as ArrayList, CollectionBase , and List<T>, which provide particular implementations of the methods and properties defined by the interface.
Syntax:
It has the following syntax:
public interface IList<T>: System.Collections.Generic.ICollection<T>,
System.Collections.Generic.IEnumerable<T>
- public interface IList<T>: It declares a generic interface named IList<T>. The <T> syntax indicates that this interface can work with a specific type, denoted by T. For example, if you have an IList<int>, the list works with integers.
- Collections.Generic.ICollection<T>: It indicates that the IList<T> interface inherits from the generic ICollection<T> interface, which is part of the System.Collections.Generic namespace. ICollection<T> extends IEnumerable<T> and provides methods for adding, removing, and checking the presence of elements.
- Collections.Generic.IEnumerable<T>: It indicates that the IList<T> interface also inherits from the generic IEnumerable<T> interface. IEnumerable<T> provides methods for iterating over a collection.
Create a list using List<T> class syntax:
List list_name = new List();
The C# program demonstrating the IList interface illustrates key concepts related to working with a non-generic collection of objects.
Example:
using System;
using System.Collections;
class Program
{
static void Main()
{
// Creating an ArrayList instance, which is an implementation of IList
IList my_List = new ArrayList();
// Adding elements to the list
my_List.Add("Apple");
my_List.Add("Banana");
my_List.Add("Orange");
// Using the IList interface methods to display the elements
Console.WriteLine("Elements in the list are:\n");
for (int i = 0; i < my_List.Count; i++)
{
Console.WriteLine($"\nElement at index {i} is: {my_List[i]}");
}
//Displaying additional IList techniques
Console.WriteLine("\nDemonstrating other IList methods:");
// Checking if an element exists in the list
string searchElement = "Banana";
if (my_List.Contains(searchElement))
{
Console.WriteLine($"{searchElement} is present in the list.");
}
else
{
Console.WriteLine($"{searchElement} is not present in the list.");
}
// Determining an element's index
int indexOfOrange = my_List.IndexOf("Orange");
Console.WriteLine($"Index of 'Orange': {indexOfOrange}");
// Removing an element
my_List.Remove("Banana");
Console.WriteLine("\nAfter removing 'Banana':\n");
for (int i = 0; i < my_List.Count; i++)
{
Console.WriteLine($"Element at index {i}: {my_List[i]}");
}
}
}
Output:
Elements in the list are:
Element at index 0 is: Apple
Element at index 1 is: Banana
Element at index 2 is: Orange
Demonstrating other IList methods:
Banana is present in the list.
Index of 'Orange': 2
After removing 'Banana':
Element at index 0: Apple
Element at index 1: Orange
Explanation:
- Namespace and Declaration of Class:
It contains the namespaces required by the program in the using statements. The Program class is declared.
- Main Method:
The program starts with the Main method.
- The initialization of an ArrayList and an IList:
An instance of ArrayList is constructed to be assigned to the IList interface (my_List).
Implementing the IList interface, ArrayList is a dynamic array.
- Adding Elements:
The Add function is used to add three string elements ("Apple," "Banana," and "Orange") to the ArrayList.
- Displaying Elements:
The program iterates over the list (my_List) using a for loop, displaying each entry and index.
- Message Display:
A message indicates that the program is about to demonstrate other IList methods.
- Searching for an Element:
The program uses the Contains method to check if the element "Banana" is present in the list and displays an appropriate message.
- Finding the Index:
The program uses the IndexOf function to locate and display the element "Orange" index.
- Removing an Element and Displaying the Modified List:
The program uses the Remove technique to eliminate the element "Banana" from the list.
After removal, a for loop is used to display the modified list.
Conclusion:
This program is a practical example of using the IList interface and the ArrayList class in C#. It covers adding elements, accessing elements by index, searching for elements, finding the index of an element, and removing elements from the list. It demonstrates the flexibility and functionality provided by the IList interface.