c# list foreach code example

Example 1: c# foreach arra

int[,] numbers2D = new int[3, 2] { { 9, 99 }, { 3, 33 }, { 5, 55 } };
// Or use the short form:
// int[,] numbers2D = { { 9, 99 }, { 3, 33 }, { 5, 55 } };

foreach (int i in numbers2D)
{
    System.Console.Write("{0} ", i);
}
// Output: 9 99 3 33 5 55

Example 2: c# list append

static void Main()
{
    // Add first 4 numbers to the List.
    List<int> primes = new List<int>();
    primes.Add(2);
    primes.Add(3);
    primes.Add(5);
    primes.Add(7);
}

Example 3: c# ienumerable to list

List<object> list = enumerable.ToList();

Example 4: c# list foreach

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

Example 5: c# list.foreach

List<string> someList = <some way to init>
someList.ForEach(delegate(string s) {
    <process the string>
});