TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# IList Generic Interface

This C# example program shows the IList generic interface. IList is implemented by the List and array types.

IList. 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, we can create a single method to receive an int[] or a List<int>.

Example. First, with the IList generic interface, you must specify a type parameter. If you want your method to act upon ints, you can use IList<int>. Any type (string, object) can be specified.

Next: In this program, we introduce the Display method, which receives an IList<int> parameter.

Note: It accesses the Count property and then uses the enumerator in a foreach-loop.

Int

C# program that uses IList generic interface

using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
	int[] array = new int[3];
	array[0] = 1;
	array[1] = 2;
	array[2] = 3;
	Display(array);

	List<int> list = new List<int>();
	list.Add(5);
	list.Add(7);
	list.Add(9);
	Display(list);
    }

    static void Display(IList<int> list)
    {
	Console.WriteLine("Count: {0}", list.Count);
	foreach (int value in list)
	{
	    Console.WriteLine(value);
	}
    }
}

Output

Count: 3
1
2
3
Count: 3
5
7
9

Passing collections to Display method. You can see in the Main method that you can pass an int[] or a List<int> to the Display method. These are implicitly cast to their base interface IList<int>.

Note: Arrays always implement IList<T>. The List type also implements IList<T>.

Discussion. You can also implement IList<T> for a custom class in your C# program. The methods required by this contract are an indexer, the IndexOf method, the Insert method, and the RemoveAt method.

IndexerList InsertRemoveAt

Summary. We looked at the IList generic interface, which can be used as an abstraction for arrays and Lists. The IList generic interface is separate from the regular IList interface. It uses different syntax.

Tip: With this layer of abstraction, you can formulate more concise ways of acting upon your data.


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf