Using "ForEach" method (of "List") with Actions, delegates and Lambdas

by Jagadish Pulakhandam on 3/26/2012 2:53:51 PM
Rated 0 from 0 votes
Brief: Demonstrates 5+ different ways to work with "ForEach" method (available in "List" class) including delegates, anonymous methods, inline delegates, Action delegate, Lambda syntax etc.
Posted to: Events, Delegates and Lambda Expressions in .NET Programming
Add to DiggAdd to del.icio.usAdd to FURLAdd to RedditAdd to YahooAdd to BlinklistAdd to GoogleAdd to ma.gnoliaAdd to ShadowsAdd to Technorati

Following source code explains the following (with respect to List.ForEach() method):
  • How to use "Action" delegate (with a custom method)
  • How to use "Action" delegate with anonymous method
  • How to use "Action" delegate directly with List.ForEach() method
  • How to use "Action" delegate with anonymous method using lambda syntax (both long & short forms)
  • How to pass anonymous method as parameter to List.ForEach() method (both delegate and Lambda syntax)
  • How to use a custom method with List.ForEach()
  • How to pass an Action delegate as parameter/argument to a method
  • How to pass a lambda expression (Action based delegate) as a parameter/argument to a method

Screen shot:



Source Code:

Method 1:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
 
namespace WindowsFormsApplication1
{
    public class Employee
    {
        public int Empno { get; set; }
        public string Ename { get; set; }
        public int Sal { get; set; }
    }
 
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }
 
        List<Employee> lstEmployees;
        private void Form2_Load(object sender, EventArgs e)
        {
            lstEmployees = new List<Employee>() {
                new Employee() {Empno = 1001, Ename="Jag", Sal=4500},
                new Employee() {Empno = 1002, Ename="Chat", Sal=5600},
                new Employee() {Empno = 1003, Ename="Dhana", Sal=7800},
                new Employee() {Empno = 1004, Ename="Winner", Sal=3400},
                new Employee() {Empno = 1005, Ename="Scott", Sal=4400},
                new Employee() {Empno = 1006, Ename="Smith", Sal=6400}
            };
 
            //Action delegate with a custom method
            //-------------------------------------
            var oAction = new Action<Employee>(IncreaseSal);
            lstEmployees.ForEach(oAction);
            dataGridView1.DataSource = lstEmployees;
 
        }
 
        private void IncreaseSal(Employee e)
        {
            e.Sal += 50;
        }
    }
}


Method 2:

//Action delegate with anonymous method
//-------------------------------------
var oAction = new Action<Employee>(delegate(Employee o) { o.Sal += 50; });
lstEmployees.ForEach(oAction);
dataGridView1.DataSource = lstEmployees;

Method 3:

//Action delegate instance (with anonymous method) as argument to "ForEach"
//-------------------------------------------------------------------------
lstEmployees.ForEach(new Action<Employee>(delegate(Employee p) { p.Sal += 50; }));
dataGridView1.DataSource = lstEmployees;

Method 4:

//Action delegate with anonymous method using lambda syntax
//----------------------------------------------------------
var oAction = new Action<Employee>((Employee o) => { o.Sal += 50; });
lstEmployees.ForEach(oAction);
dataGridView1.DataSource = lstEmployees;

Method 5:

//Action delegate with anonymous method using lambda syntax (short form)
//----------------------------------------------------------------------
var oAction = new Action<Employee>(o => o.Sal += 50);
lstEmployees.ForEach(oAction);
dataGridView1.DataSource = lstEmployees;

Method 6:

//Passing an anonymous method (delegate) to "ForEach"
//---------------------------------------------------
lstEmployees.ForEach(delegate(Employee p) { p.Sal += 50; });
dataGridView1.DataSource = lstEmployees;

Method 7:

//Passing an anonymous method (using lambda syntax) to "ForEach"
//--------------------------------------------------------------
lstEmployees.ForEach((Employee p) => { p.Sal += 50; });
dataGridView1.DataSource = lstEmployees;

Method 8:

//Passing an anonymous method (using lambda syntax) to "ForEach" (short form)
//---------------------------------------------------------------------------
lstEmployees.ForEach(p => p.Sal += 50);
dataGridView1.DataSource = lstEmployees;

Method 9:

    //Passing a custom method to "ForEach"
    //-----------------------------------
    lstEmployees.ForEach(IncreaseSal);
    dataGridView1.DataSource = lstEmployees;
 
 
private void IncreaseSal(Employee e)
{
    e.Sal += 50;
}

Method 10:

    //Passing an action delegate to a method
    //--------------------------------------
    var oAction = new Action<Employee>((Employee o) => { o.Sal += 50; });
    IncreaseSalForAllEmployees(oAction);
    dataGridView1.DataSource = lstEmployees;
 
private void IncreaseSalForAllEmployees(Action<Employee> oAction)
{
    lstEmployees.ForEach(oAction);
}

Method 11:

    //Passing an anonymous action method (delegate) to a method using lambda syntax
    //-----------------------------------------------------------------------------
    IncreaseSalForAllEmployees(o => o.Sal += 50);
    dataGridView1.DataSource = lstEmployees;
 
private void IncreaseSalForAllEmployees(Action<Employee> oAction)
{
    lstEmployees.ForEach(oAction);
}


Join the .NET Code Central Community and join the discussion!
Signing-up is FREE and quick. Do it now, we want to hear your opinion
0

Rated 0 from 0 votes ( login  to rate)
DotnetKicks DotnetKicksDe DotNetShoutout

Attachments / Source Code
You need to Login or Join for FREE to download the following