| Servergeek |
| Mickey Williams' weblog |
|
My C# BookMicrosoft Press PageOn Amazon Racing LinksSlalomSkateboarder.comNCDSA slalom page 3dm Ick Sticks Pocket Pistols .NETScott GuthrieRob Howard .NET Notes BradA LonghornScobleChris Sells ServicesDon BoxChristian Weyer All Things Distributed Ingo Rammer Tim Ewald BlawgsBag and BaggageSCOTUS Blog EconArg MaxCapital Spectator OtherWilliam Gibson |
Thursday, September 15, 2005
Lambda Functions with the C# 3.0 PreviewPosted 2:41 PMSo how do Lambda functions work using the PDC05 preview? I've come up with a simple example that leverages the PDC HOL lab exercise. Consider the code fragment below, which uses C# 2.0 syntax for the predicate delegate: static List As you can see, with anonymous delegates we avoid the need to declare a predicate method that's only used for filtering. However, we still have a lot of procedural code plumbing. Frankly, 50% of the code is concerned with convincing the compiler that we want an online delegate. The equivalent lambda function syntax is more concise and declarative: static List It turns out that this lambda function is a common case that allows us to use a simpler style. Because the type of the parameter is known, the type can be removed: static List So -- given an anchor parameter i (the type is already knowable by the compiler), simply return true or false, depending on the parameter's value. I'll have more info on lambda functions later.
|