Example. With IList, 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.
Detail The Display method accesses the Count property and then uses the enumerator in a foreach-loop.
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
Display(new int[] {1, 2});
Display(new List<int>() {1, 2});
}
static void Display(IEnumerable<int> list)
{
foreach (int value in list)
{
Console.WriteLine(value);
}
}
}1
2
1
2
Discussion. You can also implement IList T for a custom class. The methods required are an indexer, the IndexOf method, the Insert method, and the RemoveAt method.
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.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.