Geeks With Blogs

News Hi, my name is Vincent Grondin and I'm a senior consultant at Fujitsu, a consulting firm in Montreal, Québec. I'm starting that blog to share some of my thoughts and knowledge on .NET architectures and code. Being a consultant in the .NET world I come across many different things. Some good, some bad and others that are worth a blog post here whether they be good, or hmmm... shall I say, less good :) I hope you enjoy yourself while learning new stuff. Feel free to leave a comment or contact me anytime.
Vincent Grondin
When writing multi tier systems, a common thing that architects or any other person in charge of communications between tiers will do is define the public interfaces for the system. Another thing we often do when designing such systems is define delegates available for use within the code. This is something I always thought was important to do but, honestly, how much time do I end up losing writing a new delegate declaration each time I need one? Way too much in my opinion! Since framework 2.0 and the arrival of Generics, we had the opportunity to start using the Action<T> delegate. This generic delegate works like an all purpose delegate and I call it the “swiss knife” of delegates. 
Basically any method that takes one parameter and returns “void” can be used as the target of this signature! What, you expected Microsoft would create a delegate for each type available in the framework??? Nah think “Generics”!!! Simply specify the type of the parameter when declaring the delegate, like in the following example when I use a method that takes a string as the only parameter:
public Action<string> myDelegate;
Also, if the method you want to use as the target of the delegate as no parameter and returns void, you can use the Action class available from within the “System” namespace.
With framework 3.5, things have evolved and our “swiss knife” has added some extra tools for us to use! The Action<T> delegate has now 3 new flavors namely Action<T1, T2>, Action<T1, T2, T3> and Action<T1, T2, T3, T4>. Now you can use this fantastic time saver on methods that have up to four parameters! Look at the following:
public Action<string, int, double, Employee> myDelegate;
In framework 4.0, they literally transformed our “swiss knife” into an über “swiss knife”!!! This one goes out to the brave men and women from the .NET Framework who have decided to add 12 more flavors of Action<T> delegates bringing the total Action flavors to 16… That’s awesome… more goodies for us to use but, why 16?  I think that’s a lot but still, a great asset.
Alright, now what should you use if the method you want to use with a delegate returns a value? Hmmm… Action<T> clearly states that the target has to return void. The answer is Func<TResult>!!! Using the same pattern, the designers of the 4.0 .Net Framework added 16 flavors of a method that returns a value. The only difference between the two delegates is that Func wasn’t available in the 2.0 .Net Framework…
Predicates are going to be for the next time…. Until then, happy learning!
Posted on Monday, December 7, 2009 10:06 PM | Back to top

Comments on this post: Behold, the "swiss knife" of delegates... Action T !!!

No comments posted yet.
Your comment:
 (will show your gravatar)

Copyright © Vincent Grondin | Powered by: