How to use IEnumerable to loop over array and list in C#

1 Answer

0 votes
using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        int[] arr = {1, 2, 3, 4, 5};
        List<int> lst = new List<int>() {8, 9, 0};

        Print(arr);
        Console.WriteLine();
        Print(lst);
    }

    static void Print(IEnumerable<int> values) {
        foreach (int n in values) {
            Console.WriteLine(n);
        }
    }
}



/*
run:

1
2
3
4
5

8
9
0

*/

 



answered Sep 11, 2020 by avibootz
...