How to use ForEach Method with Collection in C#

No.of Views1713
Bookmarked0 times
Downloads 
Votes0
By  pranay rana   On  29 Jul 2011 10:07:14
Tag : CSharp , CSharp4.0
In this article, I will explain, How to use ForEach Method with Collection in C#.It is help to reduces number of lines code compare with normal for each method and simple.
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

In this article, I will explain, How to use ForEach Method with Collection in C#.It is help to reduces number of lines code compare with normal for each method and simple.

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.

Image Loading

ForEach method make code more simple and easy to understand.Hopes help and thank you reading.

 
Sign Up to vote for this article
 
About Author
 
pranay rana
Occupation-CEO
Company-GMind Solusion
Member Type-Expert
Location-India
Joined date-08 Jan 2011
Home Page-http://pranayamr.blogspot.com
Blog Page-http://pranayamr.blogspot.com
Hey, I am Pranay Rana, working as a Senior Software engineer in mid-size company located in ahmedabad. Web development in Asp.Net with C# and MS sql server are the experience tools that I have had for the past 4.3 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 me on my blog - http://pranayamr.blogspot.com/ StackOverFlow - http://stackoverflow.com/users/314488/pranay My CV :- http://careers.stackoverflow.com/pranayamr
 
 
Other popularSectionarticles
    In this article, i will explain how to use the How to use ExpandoObject Class in .NET 4.0.The ExpandoObject class is introduced on .NET Framework 4.0 and inherited using many interfaces.
    Published Date : 16/Apr/2011
    C# 4.0 supports Dynamic Programming by introducing new Dynamic Typed Objects
    Published Date : 26/May/2010
    Dynamic Datatype in C# 4.0 is quit bit familiar to Var Datatype. Deference between Var and Dynamic is VAR initialized on CompileTime and Dynamic initialized On Runtime
    Published Date : 17/May/2010
    In this article I will show how to use the IObserver and IObservable Interfaces in C#. These two interfaces is work with connection to Push based approach on Reactive Framework
    Published Date : 15/Dec/2010
    Through this article, i will introduce in FCL named Tuple which can store n - number of values in it. Yes, you specify the type of each of those variables as generic parameters, and the object will create those values for you
    Published Date : 15/Nov/2010
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top