How to use index in foreach with C#

1 Answer

0 votes
using System;

class Program
{
    static void Main() {
        string[] arr = { "c#", "c", "c++", "java", "rust" };
        int i = 0;
         
        foreach (string s in arr) {
            Console.WriteLine(i + " " + s);
            i++;
        }
    }
}



/*
run:

0 c#
1 c
2 c++
3 java
4 rust

*/

 



answered Aug 25, 2023 by avibootz

Related questions

1 answer 269 views
269 views asked Mar 6, 2023 by avibootz
2 answers 332 views
332 views asked Nov 25, 2020 by avibootz
1 answer 315 views
1 answer 230 views
2 answers 334 views
1 answer 271 views
...