Click here to Skip to main content
15,116,253 members
Articles / All Topics
Technical Blog
Posted 25 Sep 2010

Tagged as


10 bookmarked

Exploring Action Delegate

Rate me:
Please Sign up or sign in to vote.
5.00/5 (10 votes)
25 Sep 2010CPOL1 min read
In the last few days, I found a very elegant way to use the delegate...

We used delegate a lot in some or the other way. Like we know, the event base model that we use in Windows as web programming, is based on delegate only. I am in the habit of using delegates often. In the last few days, I found a very elegant way to use the delegate. I was in the habit of using only generic delegates that allows us type safety and a lot more flexibility.

In our traditional programming, we use the delegate as in the following steps:

  1. Declaring the delegate
  2. Creating object of the delegate and assigning it to the appropriate function
  3. Calling the delegate

Let's see the code:

.NET 2.0 introduced one generic delegate Action which takes a single parameter and returns nothing. Its declaration is:

Public delegate void Action<T1>(T1 t1) // Takes 1 parameter and returns nothing

This is a very elegant way to use the delegate.

And C# 3.0 introduced 4 delegates which are as follows:

  1. C#
    Public delegate void Action() 	// Takes no parameter and returns nothing 
  2. C#
    Public delegate void Action<T1,T2>(T1 t1,T2 t2) 	// Takes 2 parameters 
    						// and returns nothing 
  3. C#
    Public delegate void Action<T1,T2,T3>(T1 t1,T2 t2,T3 t3) // Takes 3 parameters 
    						// and returns nothing 
  4. C#
    Public delegate void Action<T1,T2,T3,T4>(T1 t1,T2 t2,T3 t3),T4 t4) // Takes 4 
    					// parameters and returns nothing 

Let's see them running.

As from the code, we can also see that Action delegate also provides a method invoke to call the method.

These Action delegates can also be used with Anonymous methods as:

They can also be used with lambda function. Let's see:

So code diligently and efficiently.

Note: The ForEach and ForEach<T> methods each take an Action<T> delegate as a parameter. The method encapsulated by the delegate allows us to perform an action on each element in the array or list.

Happy .NETing….




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


About the Author

Software Developer (Senior)
India India
Brij is a 3-times Microsoft MVP in ASP.NET/IIS Category and a passionate .NET developer. More than 6 years of experience in IT field, currently serving a MNC as a Tech Lead/Architect.

He is a very passionate .NET developer and have expertise over Web technologies like ASP.NET 2.0/3.5/4.0, jQuery, JSON, Javascript, IIS and related technologies. He is also a Exchange Server (EWS) Specialist. He has great experience in design patterns and N-Tier Architecture.

He is also certified as Microsoft Certified Technologies Specialist-ASP.NET and Microsoft Certified Technologies Specialist-WCF in .NET 4.0. He has also received several awards at various forums and his various articles got listed as "Article of the day" at ASP.NET Microsoft Official Website

He has done MCA from NIT Durgapur and completed his graduation from Lucknow University.

Learning new technologies and sharing knowledge excites him most. Blogging, solving problems at various forums, helping people, keeps him busy entire day.

Visit his Blog: Code Wala

Area of Expertise :
C#, ASP.NET 2.0,3.5,4.0, AJAX, JQuery, JSON, XML, XSLT, ADO.Net, WCF, Active Directory, Exchange Server 2007 (EWS), Java script, Web Services ,Win services, DotnetNuke, WSS 3.0,Sharepoint Designer, SQL Server 2000/2005/2008

Comments and Discussions

GeneralVery lucid explanation! Pin
Divyans Mahansaria7-Apr-14 7:13
professionalDivyans Mahansaria7-Apr-14 7:13 
GeneralMy vote of 5 Pin
Dilip Baboo10-Mar-11 12:37
MemberDilip Baboo10-Mar-11 12:37 
GeneralRe: My vote of 5 Pin
Brij11-Mar-11 18:57
mentorBrij11-Mar-11 18:57 
GeneralNice Pin
Abhijit Jana25-Sep-10 20:39
professionalAbhijit Jana25-Sep-10 20:39 
GeneralRe: Nice Pin
Brij25-Sep-10 20:41
mentorBrij25-Sep-10 20:41 
AnswerRe: Nice Pin
thatraja27-Sep-10 23:21
professionalthatraja27-Sep-10 23:21 
GeneralRe: Nice Pin
Brij28-Sep-10 3:45
mentorBrij28-Sep-10 3:45 

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

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