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 pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.