C# - IList Interface Example

In this example, we will learn about the implementation of IList Interface using C# program.
Submitted by Nidhi, on November 05, 2020 [Last updated : March 22, 2023]

Here, we will demonstrate the IList interface with the help of the List collection class.

C# program to demonstrate the example of IList interface

The source code to demonstrate the IList interface is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.

//C# - IList Interface Example
 
using System;
using System.Collections.Generic;

class Demo
{
    static void Print(IList<string> list)
    {
        foreach (string str in list)
        {
            Console.WriteLine("\t"+str);
        }
    }
    static void Main()
    {
        string[] countries = {"India","China","Russia","USA"};
        List<string> studntents = new List<string>();

        studntents.Add("Rohit");
        studntents.Add("Shikhar");
        studntents.Add("Virat");

        Console.WriteLine("Countries: ");
        Print(countries);

        Console.WriteLine("Students: ");
        Print(studntents);
    }
}

Output

Countries:
        India
        China
        Russia
        USA
Students:
        Rohit
        Shikhar
        Virat
Press any key to continue . . .

Explanation

In the above program, we created a Demo class that contains two static methods Print() and Main(). In the Print() method, we receive an object that contains strings in the IList interface and then print the items on the console screen.

Now look to the Main() method, Here we created the array of strings that contains the strings. After that, we created a list using the List collection class that contains name students, and then we printed the name of countries and students using the Print() method on the console screen.

C# Basic Programs »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.