Using lambdas to create generic factories

One item I find to be limiting in C# is the new generic constraint.  The syntax construct specifies that the type backing a given generic parameter contains a parameter less constructor.   It allows methods to create instances of generic parameters in a type safe manner. public static void Example<T>() where T : new() { var…

4

Why is LINQ absent from debugger windows?

As the owner of the VB.Net portion of the overall debugging experience, I frequently hear the request from customers to add LINQ support into the Watch / Immediate and Locals window.  Virtually every other type of expression is available in the debugger windows so why leave one of the most popular ones out? Quick Diversion:…

9

PowerShell LINQ: Take-Count and Take-While

The Take pair of functions are very similar to the Skip functions.  The Take expression does essentially the opposite of the Skip functions.  Skip is useful for getting elements further down the pipeline.  Take is used for getting elements from the start of the pipeline.  #============================================================================ # Take count elements fro the pipeline #============================================================================ function…

2

PowerShell LINQ: Skip-While

Next up in the PowerShell LINQ series is SkipWhile.  This LINQ function takes an enumerable instance and a predicate.  The function will skip the elements in the enumerable while the predicate is true.  The argument to the predicate is the current value of the enumerable.  The LINQ version takes a predicate in the form of…

2

Script Blocks and Closures (or lack there of)

Script blocks are a concise way of representing an expression or statement group in Powershell.  It’s the C#/F#/VB lambda equivalent for PowerShell.  One difference between C#/F#/VB lambda expressions and a scriptblock is the lack of lexical closures (otherwise known as variable capturing).  This feature allows for a variable defined in an outer scope to be…

0

Script Blocks and Arguments: Figuring it out for myself … again

Script blocks are a powershell construct for storing an expression or group of statements inside an expression.  It’s the equivalent of a C#/F#/VB Lamba expression.  Recently I needed to use a script block but found I had forgotten how to read passed parameters inside the script block.  Forgetting how to use a feature is typically…

4

Switching on Types

One action I find frustrating in C# is where a particular action needs to be taken based off of the type of a particular object.  Ideally I would like to solve this with a switch statement but switch statements only support constant expressions in C# so no luck there.  Previously I’ve had to resort to…

9

SynchronizationContext and Higher Order Functions

It’s often useful to ensure that actions occur on specific threads, in particular event handlers.  Take Windows Forms for instance where all operations on a Control must occur on the thread it was created on.  Typically this is not a problem since WinForms respond to events such as Click, Move, etc…  These events are sourced…

1

C# Lambda Type Inference

One of the limitations of C# type inference is that you cannot use it to infer the type of a lambda expression.  For example, the following code will not compile var f = () => 4; Normally this is not too much of an issue because you can just explicitly declare the type of the…

1

Lambda Unexpected Behavior

One item you strive to avoid when you design and implement a feature is unexpected behavior.  Unfortunately there is one case we couldn’t avoid with Lambda’s in VB9.  I just ran into the this problem when coding up a handler.  I wanted to disable a button when the text of particular TextBox was empty.  I…

1