Click here to Skip to main content
Licence CPOL
First Posted 3 Mar 2008
Views 23,422
Downloads 82
Bookmarked 15 times

The Power of the 'Action' Delegate

By | 3 Mar 2008 | Article
Overview and test library of how powerful the System.Action delegate can be

Introduction

For a few weeks now, I've been trying to think of a reasonable way to code a single function that would accept a delegate to any function, without having to write 23 or more overloads. And to make it look... clean and consistent across any function being passed. Zero boxing overhead is a must.

So obviously I must have found a way, otherwise I would still be pouring over the code (as well as my coke-a-cola). Simple, clean, and I even was able to write an asynchronous version of the function without any added complexity.

How It Works

First off - as you've probably realized from the title- this code cites the power of the Action delegate. So what about it? It is a delegate to a function without a return value or any parameters. And why does that make it special? Because you can basically cast any function to a standard System.Action delegate.

void Foo(Action method);

That looks sound but...

void Main()
{
    Foo(Blah);
}

void Blah(int index, int la) { }

... will generate an error. As you say, "Hey this guy said I could pass any method I wanted..." blah blah blah. Yes but you need to do what I call 'lambda casting' to it first.

void Main()
{
    Foo(() => Blah(0, 34));
}

void Blah(int index, int la) { }

As you can see, you take an empty lambda declaring statement '()' followed by the operator '=>' and then just call the function as you normally would.

When Foo is executed, it will call the function just like you specified. This method works wonders for a profiler (like the sample library) however I am not sure about any other uses.

A Final Thought

With 'lambda casting' comes a very small amount of overhead. Invisible to most applications. I wouldn't suggest this for a game though. Too many of these and things might get ugly.

About The Sample Library

The included sample library is merely a heavily-documented example of using 'lambda casting'. If for any reason you do not understand my inane documentation styles, please feel free to leave a comment asking questions or send me an e-mail at Blobmiester@gmail.com.

History

  • 4th March, 2008: Original article submitted

License

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

About the Author

Ryan Leckey

Other

United States United States

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionIt won’t compile Pinmembernpcomplete13:01 12 Feb '09  
AnswerRe: It won’t compile Pinmembersoujiro seta4:26 2 Jan '12  
GeneralAction delegate not recognize Pinmemberjstrider394:48 24 Oct '08  
GeneralNot a cast PinmemberWilliam E. Kempf3:34 4 Mar '08  
GeneralRe: Not a cast Pinmemberlogan13375:26 4 Mar '08  
GeneralRe: Not a cast PinmemberWilliam E. Kempf5:41 4 Mar '08  
QuestionRe: Not a cast Pinmemberredjoy11:56 4 Mar '08  
GeneralRe: Not a cast PinmemberRyan Leckey16:41 4 Mar '08  
JokeShameless coke-a-cola advert! Pinmembersameeraperera22:59 3 Mar '08  
GeneralRe: Shameless coke-a-cola advert! PinmemberRyan Leckey16:44 4 Mar '08  

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.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 4 Mar 2008
Article Copyright 2008 by Ryan Leckey
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid