c# iterate through List of strings code example

Example 1: c# loop through list

using System;
using System.Collections.Generic;
 
namespace forgetCode {
    class program {
        public static void Main() {
 
            List<int> list = new List<int>();
            list.Add(1);
            list.Add(2);
            list.Add(3);
 
            foreach (int item in list) { // Loop through List with foreach
                Console.WriteLine(item);
            }
 
            for (int i = 0; i < list.Count; i++) { // Loop through List with for
                Console.WriteLine(list[i]);
            }
        }
    }
}

/*
Outputs:
1
2
3
1
2
3
*/

Example 2: c# loop string array

//Using Linq to itterate over an array
var strArr = new string[4] {"one", "Two", "Three", "Four"};
Console.WriteLine(strArr.Select((s, i) => $"Item no: {i + 1}, Value: {s}"));