Click here to Skip to main content
15,906,816 members
Articles / Programming Languages / C#
Article

The Power of the 'Action' Delegate

Rate me:
Please Sign up or sign in to vote.
2.87/5 (15 votes)
3 Mar 2008CPOL2 min read 42.4K   15   10
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.

C#
void Foo(Action method);

That looks sound but...

C#
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.

C#
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)


Written By
Other
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionIt won’t compile Pin
npcomplete112-Feb-09 3:01
npcomplete112-Feb-09 3:01 
AnswerRe: It won’t compile Pin
Ming Xin2-Jan-12 4:26
professionalMing Xin2-Jan-12 4:26 
GeneralAction delegate not recognize Pin
Raven3924-Oct-08 4:48
Raven3924-Oct-08 4:48 
GeneralNot a cast Pin
William E. Kempf4-Mar-08 3:34
William E. Kempf4-Mar-08 3:34 
GeneralRe: Not a cast Pin
chaiguy13374-Mar-08 5:26
chaiguy13374-Mar-08 5:26 
GeneralRe: Not a cast Pin
William E. Kempf4-Mar-08 5:41
William E. Kempf4-Mar-08 5:41 
QuestionRe: Not a cast Pin
redjoy4-Mar-08 11:56
redjoy4-Mar-08 11:56 
GeneralRe: Not a cast Pin
Ryan Leckey4-Mar-08 16:41
Ryan Leckey4-Mar-08 16:41 
JokeShameless coke-a-cola advert! Pin
sameeraperera3-Mar-08 22:59
sameeraperera3-Mar-08 22:59 
...real programmers drink beer!

Just kidding. Great topic, didn't know about this one.
Thanks!
Got 5 from me
Big Grin | :-D

Sameera Perera
http://www.codoxide.com

GeneralRe: Shameless coke-a-cola advert! Pin
Ryan Leckey4-Mar-08 16:44
Ryan Leckey4-Mar-08 16:44 

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.