c# foreach list code example

Example 1: c# foreach enum

enum Foos {
  Foo,
  Bar,
}

foreach(Foos val in Enum.GetValues(typeof(Foos))) {}

Example 2: c# loop through list

using System;
using System.Collections.Generic;
 
namespace forgetCode {
    class program {
        public static void Main() {
 
            List list = new List();
            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 3: how do i foreach c#

//No counter just the foreach.
 //the HelloWorld is the Class objects name nor a public string can be used aswell.
foreach (string element in fibNumbers)
{
    Console.WriteLine(element.HelloWorld);
}

Example 4: c# foreach

var numbers = new NumberList { 0, 1, 2, 3, 4, 5 };
foreach (int num in numbers)
{
    DoSomething();
}

Example 5: c# list foreach

someList.ForEach(x => { if(x.RemoveMe) someList.Remove(x); });

Example 6: c# list.foreach

List someList = 
someList.ForEach(delegate(string s) {
    
});

Tags:

Misc Example