Using Nested Functions in C#

Posted by Tim on September 7, 2008 in .NET Framework, C# |

Scenario

We currently have a templating API system where this allows developers to use a combination of C# and HTML markup to customized the display of the client’s websites. In this case, I needed a way to quickly perform some calculations and formatting without having to declare the function at design-time. My problem was I was to pass in a Boolean, a integer and a string that performed specific logic to produce a outcome (a string).

Solution

I was able to use a Func delegate specifying the parameter types and the return type. With a help of Lambda expressions I was able to capture the input of the three variables and return the result.

Func<bool, int, String, String> nestedFunction = (b, i, s) => { //Do something return s; };
String foo = nestedFunction(true,1,”format string”);

References

http://www.codeguru.com/csharp/csharp/cs_misc/designtechniques/article.php/c12749/

Tags: ,

Leave a Reply

Copyright © 2008-2012 Codelab Blog All rights reserved.
Desk Mess Mirrored version 1.9.1 theme from BuyNowShop.com.