How to use foreach loop in C#

2 Answers

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

class Program
{
    static void Main() {
        var lst = new List<int> { 5, 8, 3, 1, 0, 7 };

        foreach (int element in lst) {
            Console.WriteLine(element);
        }
    }
}



/*
run:

5
8
3
1
0
7

*/

 



answered Nov 25, 2020 by avibootz
0 votes
using System;

class Program
{
    static void Main() {
        var arr = new int[] { 5, 7, 2, 1, 9 };
        
        foreach (int n in arr) {
            Console.WriteLine(n);
        }
    }
}



/*
run:

5
7
2
1
9

*/

 



answered Nov 25, 2020 by avibootz

Related questions

1 answer 157 views
157 views asked Mar 6, 2023 by avibootz
1 answer 199 views
1 answer 183 views
1 answer 110 views
110 views asked Aug 8, 2024 by avibootz
3 answers 225 views
1 answer 201 views
2 answers 293 views
...