Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

C# Program to Demonstrate the IList Interface

  • Last Updated : 20 Dec, 2021

In C# IList interface is an interface that belongs to the collection module where we can access each element by index. Or we can say that it is a collection of objects that are used to access each element individually with the help of an index. It is of both generic and non-generic types. Its implementation is generally categorized into three parts that are:

  • read-only: In which the IList cannot be modified.
  • fixed-size: In which IList is not allowed to add or remove elements.
  • variable size: In which IList is allowed to modify, add, or remove elements.

Syntax:

// For Non_generic IList
public interface IList : System.Collections.ICollection

// For Generic IList
public interface IList<T> : System.Collections.Generic.ICollection<T>,
                            System.Collections.Generic.IEnumerable<T>

Properties:

Some of the commonly used properties of the IList interface are:

Property NameDescription
CountThis property will return the total number of items present in the ICollection.
IsFixedSizeThis property will return a value that determines whether the IList has a fixed size or not.
IsReadOnlyThis property will return a value that determines whether the IList is read-only or not.
IsSynchronizedThis property will return a value that determines whether access to the ICollection is synchronized or not.
Item[Int32]This property will gets or sets the item from the specified index.
SyncRootThis property will get an object that can be used to synchronize access to the ICollection.

Methods: 

Some of the commonly used methods of the IList interface are:

Method NameDescription
Add(Object)This method is used to add an item to the IList.
Clear()This method is used to remove or delete all items from the IList.
Contains(Object)This method is used to check whether the IList contains a specific value or not.
CopyTo(Array, Int32)This method is used to copy the elements of the ICollection to an array, starting from the given index.
GetEnumerator()This method is used to get an enumerator that iterates through a collection.
IndexOf(Object)This method is used to find the index of a specific item in the IList.
Insert(Int32, Object)This method is used to insert an item to the IList at the given index.
Remove(Object)This method is used to remove or eliminate the first occurrence of a given object from the IList.
RemoveAt(Int32)This method is used to remove the IList item from the given index.

Now we understand the working of IList interface with the help of an example. In the below example, we create a list with subjects and students and display the list. So to do this we have to follow the following approach:

Approach:

1. Create an array of subjects

{"os","cn","php","c/cpp","java/jsp","python/r"}

2. Create a list of student names and add names into it using Add() method.

List<string> data = new List<string>();
data.Add("sai");
data.Add("sravan");
data.Add("jyothika");

3. Define an IList Interface to display the students and subjects names.

 static void Show(IList<string> list)
{
    // Iterate through list
    foreach (string str in list)
    {
        // Print
        Console.WriteLine("\t" + str);
    }
 }

4. Call the Show() method to display the list of students and subjects.

Show(subjects);
Show(data);

Example:

C#




// C# program to illustrate the IList interface
using System;
using System.Collections.Generic;
  
class GFG{
  
// Show method to display the list of
// subject and students name
static void Show(IList<string> list)
{
      
    // Iterate through the list
    foreach(string str in list)
    {
        Console.WriteLine("\t" + str);
    }
}
  
// Driver code
static void Main()
{
      
    // Declare a list of subjects
    string[] subjects = { "OS", "CN", "PHP", "C/CPP"
                          "Java/Jsp", "Python/R" };
  
    // Define  a list
    List<string> data = new List<string>();
  
    // Add students to the list
    data.Add("sai");
    data.Add("sravan");
    data.Add("jyothika");
      
    // Display subjects
    Console.WriteLine("Subjects Name: ");
    Show(subjects);
      
    // Display students
    Console.WriteLine("Students Name: ");
    Show(data);
}
}

Output:

Subjects Name: 
    OS
    CN
    PHP
    C/CPP
    Java/Jsp
    Python/R
Students Name: 
    sai
    sravan
    jyothika

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!