Debugging Expression Trees in Visual Studio 2010

First of all, let’s take a look at the example from one of my previous posts. It creates an expression tree for calculating the factorial of a number.ParameterExpression value = Expression.Parameter(typeof(int), “value”); ParameterExpression result = Expression.Parameter(typeof(int), “result”); LabelTarget label = Expression.Label(typeof(int)); BlockExpression block = Expression.Block( new[] { result }, Expression.Assign(result, Expression.Constant(1)), Expression.Loop( Expression.IfThenElse( Expression.GreaterThan(value, Expression.Constant(1)),… Read more

Dynamic in C# 4.0: Creating Wrappers with DynamicObject

In the previous post I showed how you can use the new dynamic feature and the ExpandoObject class to add and remove properties at run time, and how this can make your code more readable and flexible than code written with LINQ to XML syntax. But there were some obvious flaws in that example: While… Read more

Dynamic in C# 4.0: Introducing the ExpandoObject

You have probably already heard about the new dynamic feature in C# 4.0 and how it is used to support COM interop. If you haven’t, I strongly recommend reading the following MSDN articles: Using Type dynamic and How to: Access Office Interop Objects by Using Visual C# 2010 Features. Well, where else can you use… Read more

Generating Dynamic Methods with Expression Trees in Visual Studio 2010

Expression trees first appeared in Visual Studio 2008, where they were mainly used by LINQ providers. You can use expression trees to represent code in a tree-like format, where each node is an expression. You can also convert expression trees into compiled code and run it. This transformation enables dynamic modification of executable code as… Read more

How to use LINQ methods to compare objects of custom types

LINQ provides a convenient syntax and many useful methods for operating with collections of objects. However, to be correctly processed by LINQ comparison methods such as Distinct or Intersect, a type must satisfy certain requirements. Let’s take a look at the Distinct method, which returns all distinct objects from a collection. List<int> numbers = new… Read more

Does the “LINQ to Objects” provider have built-in performance optimization?

Let’s start with the basics and maybe repeat some information that many of you already know. One of the most important concepts in LINQ performance and optimization is, of course, deferred execution. It simply means that when you declare a variable and assign it a query expression, that expression is not executed immediately.   //… Read more