C# Program to Demonstrate the iList Interface
Posted by Superadmin on August 12 2022 05:04:38

C# Program to Demonstrate the iList Interface

 

This is a C# Program to demonstrate iList interface.

Problem Description

This C# Program Demonstrates iList Interface.

Problem Solution

Here Lists and arrays implement IList and this interface is an abstraction that allows list types to be used with through a single reference type. With it, we can create a single method to receive an int[] or a List.

Program/Source Code

Here is source code of the C# Program to Demonstrate iList Interface. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

/*
 *  C# Program to Demonstrate iList Interface
 */
using System;
using System.Collections.Generic;
 
class Program
{
    static void Main()
    {
        int[] a = new int[3];
        a[0] = 1;
        a[1] = 2;
        a[2] = 3;
        Display(a);
 
        List<int> list = new List<int>();
        list.Add(5);
        list.Add(7);
        list.Add(9);
        Display(list);
        Console.ReadLine();
    }
 
    static void Display(IList<int> list)
    {
        Console.WriteLine("Count: {0}", list.Count);
        foreach (int num in list)
        {
            Console.WriteLine(num);
        }
    }
}
Program Explanation

This C# Program is used to demonstrate iList Interface. Lists and arrays implement IList. This interface is an abstraction that allows list types to be used with through a single reference type with it. Create a single method to receive an int[] or a List<int> the Add(Object) adds an item to the IList.

 

Here the lists and arrays implements IList. This interface is an abstraction that allows list types to be used with through a single reference type. With it, we can create a single method to receive an int[] or a List.

Runtime Test Cases
 
Count: 3
1
2
3
Count: 3
5
7
9