C# Program To Demonstrate The Ilist Interface

In C#, the IList interface is a component of the .NET Framework and is specified in the System.Collections namespace. It signifies a non-generic assembly of items that can be individually retrieved by their index. The IList interface offers functions for adding, removing, and retrieving elements within a list-type arrangement, building upon the features of 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.
  • Properties:

  • 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.
  • Implementations:

The IList interface is utilized by various classes like ArrayList, CollectionBase, and List<T>, which offer specific implementations of the methods and properties outlined in the interface.

Syntax:

It has the following syntax:

Example

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__PRESERVE_18__ class syntax:

    Example
    
    List list_name = new List();
    

The C# code example showcasing the IList interface highlights important principles concerning managing a collection of non-generic objects.

Example:

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:

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. When working with object-oriented programming in PHP, it is essential to understand the concept of namespaces and how to declare a class within a particular namespace.

It includes the namespaces necessary for the program within the using directives. The Program class is defined.

  1. Entry Point:

The software begins execution from the Main function.

  1. Creating an ArrayList and an IList:

An ArrayList object is created to be assigned to the IList interface (my_List).

Implemented as part of the IList interface, ArrayList functions as a resizable array.

  1. Inserting Elements:

The Add method is employed to insert three string items ("Apple," "Banana," and "Orange") into the ArrayList.

  1. Viewing Elements:

The code loops through the list (my_List) with a for loop, showcasing both the entry and its corresponding index.

  1. Output Display:

A notification signifies that the software is going to showcase various IList techniques.

  1. Locating an Item:

The software utilizes the Contains function to verify the presence of the item "Banana" within the collection and exhibits a suitable notification.

  1. Locating the Index:

The software utilizes the IndexOf method to find and exhibit the position of the item "Orange."

  1. Deleting an Element and Showing the Updated List:

The software utilizes the Delete method to remove the item "Banana" from the array.

Following deletion, a for loop is employed to showcase the adjusted list.

Conclusion:

This code snippet illustrates the application of the IList interface and the ArrayList class in C#. It encompasses the addition of elements, retrieval of elements by index, searching for elements, locating the index of an element, and deleting elements from the list. It showcases the versatility and capabilities offered by the IList interface.

Input Required

This code uses input(). Please provide values below: