Click here to Skip to main content
Click here to Skip to main content

Tagged as

Go to top

ForEach Method for the collections

, 25 Jul 2011
Rate this:
Please Sign up or sign in to vote.
In this small post I am going to discuss about the new method introduce in the .net framework ForEach which allow to perform the action on the each set of element.Syntax public void ForEach(Action action)Action is delegate or function to perform on each element of the List.To get in more detail how
In this small post I am going to discuss about the new method introduce in the .net framework ForEach which allow to perform the action on the each set of element.

Syntax
public void ForEach(Action<t> action)
Action<t> is delegate or function to perform on each element of the List.

To get in more detail how it works check check the following code.
Below is Employee class which has property related to employee of the company.
public class Employee
{
        public string Name { get; set; }
        public int Salary { get; set; }
        public string Address { get; set; }
        public int Id { get; set; }
}
and now in following line of code I am initializing list of employees
class Program
    {
        static void Main(string[] args)
        {
          List<employee> emplst = new List<employee> { 
          new Employee() { Name="pranay", Address = "Ahmedabad", Id=1, Salary=1234},
          new Employee() { Name="Hemang", Address = "Ahmedabad", Id=2, Salary=2348},
          new Employee() { Name="kurnal", Address = "Himmatnagar", Id=3, Salary=8999},
          new Employee() { Name="Hanika", Address = "Ahmedabad", Id=4, Salary=7888}
        };
Now I want to increase salary of each employee by 10%, so with the the help of new ForEach construct I can easily achieve it by using following line of code.
emplst.ForEach(new Program().incSalary);
     }

  private void incSalary(Employee emp)
  {
     emp.Salary += (emp.Salary * 10)/100; 
  }
}
As you can see in above code I have written new Program().incSalary as action, in the incsalary method as you see I increase the salary of each employee 10%.
This thing can easily also done by making use of the foreach loop available but if you see the the ForEach in reflector it does the same thing.
ForEach method make code more simple and easy to understand.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Pranay Rana
Software Developer (Senior) GMind Solusion
India India

Microsoft C# MVP (12-13)

 
Hey, I am Pranay Rana, working as a ITA in MNC. Web development in Asp.Net with C# and MS sql server are the experience tools that I have had for the past 5.5 years now.
 
For me def. of programming is : Programming is something that you do once and that get used by multiple for many years
 

You can visit my blog

StackOverFlow - http://stackoverflow.com/users/314488/pranay
My CV :- http://careers.stackoverflow.com/pranayamr
 
Awards:


Follow on   Twitter   LinkedIn

Comments and Discussions

 
QuestionWhy use the additional method? PinmemberMichael Lee Yohe27-Jul-11 11:42 
AnswerRe: Why use the additional method? PinmemberPranay Rana27-Jul-11 20:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140926.1 | Last Updated 25 Jul 2011
Article Copyright 2011 by Pranay Rana
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid