Monday, January 27, 2014

A handy linq ForEach on List collection

To access each element in the list; for example, to print each element to the console we can just use one single line of code to do it by using Linq ForEach on List collection:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TestListContainsInEmptyList
{
    class Program
    {
        static void Main(string[] args)
        {
            List<int> list = new List<int>();

            for (int i = 0; i < 10; i++)
                list.Add(i);

            list.ForEach(x => Console.WriteLine(x));
            Console.ReadLine();
         
        }
    }
}

No comments:

Post a Comment