C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Next: In this program, we introduce the Display method, which receives an IList<int> parameter.
Display: This method accesses the Count property and then uses the enumerator in a foreach-loop.
Int, uintPropertyForeachInfo: 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>.
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
Tip: With this layer of abstraction, you can formulate more concise ways of acting upon your data.