Javatpoint Logo
Javatpoint Logo

C# Program to Demonstrate the IList Interface

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:

  1. Add: Adds a new item in the IList.
  2. Clear: Removes all items from the IList.
  3. Contains: Determines if a particular value is present in the IList.
  4. IndexOf: Finds the object's first occurrence and returns its index.
  5. Insert: Inserts a new element to the IList at the provided index.
  6. Remove: Removes the first occurrence of a specific object from the IList.
  7. RemoveAt: Removes the item at the given index.

Properties:

  1. IsFixedSize: Gets a value that indicates if the size of the IList is fixed.
  2. IsReadOnly: Gets a value that specifies whether an IList can only be read.
  3. Item: At the given index, gets or sets the element.

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>: 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:

The C# program demonstrating the IList interface illustrates key concepts related to working with a non-generic collection of objects.

Example:

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:

1. Namespace and Declaration of Class:

It contains the namespaces required by the program in the using statements. The Program class is declared.

2. Main Method:

The program starts with the Main method.

3. 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.

4. Adding Elements:

The Add function is used to add three string elements ("Apple," "Banana," and "Orange") to the ArrayList.

5. Displaying Elements:

The program iterates over the list (my_List) using a for loop, displaying each entry and index.

6. Message Display:

A message indicates that the program is about to demonstrate other IList methods.

7. 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.

8. Finding the Index:

The program uses the IndexOf function to locate and display the element "Orange" index.

9. 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.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA