.NET Zone is brought to you in partnership with:

Dave Bush has been programming for 27 years. Dave is an advocate of programming best practices as well as a respected .NET programmer. He has presented at several user group meetings and organizations. He currently blogs about .NET and best practices at http://blog.dmbcllc.com. Dave is a DZone MVB and is not an employee of DZone and has posted 47 posts at DZone. You can read more from them at their website. View Full User Profile

Self Executing Anonymous Function

03.07.2013
| 2560 views |
  • submit to reddit

Not because it is all that new, but because it took me a while to find it, here’s how to create a self executing anonymous function using CSharp, just like you can do in JavaScript.

((Func<int>)(delegate() 
{ 
   return 0; 
}))();

Func<int> says the the delegate will return an integer.  If you need to pass a parameter you can use the alternate form of Func<parameterType,returnType>

delegate() {return 0;} is just basic delegate syntax that I’ve discussed in the past.… Read the rest

This post was originally found at .NET Answers the original article can be found at Self Executing Anonymous Function

Published at DZone with permission of Dave Bush, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)