Latest on twitter:
I'm a programmer from the netherlands. This is the stuff I come across. I keep it more for me than for you, but I do hope you like it.
Ask me something
One of the most powerfull parts of c# that totally went past me until a month or so ago, are Expressions. Simply put, if you create a lambda expression:
Func<int,int> square = x => x*x
You can call it as a function:
square(2); //returns 4
But if you forecefully declare it as an expression:
Expression<Func<int,int>> square = x => x*x
You have created an Expression of type lambdaExpression. It’s something that represents the function, but isn’t compiled yet:
square(2); //throws exception
Func<int,int> compiledSquare = square.compile();
compiledSquare(2); //returns 4
I first noticed the need for this when creating functions that needed to call the database using linq2sql. If along the way you create a function that accepts a delegate (Func<T> instead of Expression<Func<T>>) your queries suddenly won’t be translated to database queries anymore :(
So what can we do using expressions? well in my next posts I will describe how we can use it …