c sharp

How to Use Expressions in C# for Dynamic Logic

Expressions are a fundamental feature in paradigms such as LINQ, lambda expressions, expressions trees, and other dynamic programming constructs.

In simple terms, expressions refer to a technique of representing the code in the form of data. You will find them in lambda expressions, expressions tress, and more. This ensures that we can pass the logic as parameters, construct queries, build predicates, and more.

Types of Expressions in C#

There are two main types of expressions in C#. In this tutorial, we will learn the basics of working with these two expression types:

  1. Lambda Expressions
  2. Expression Trees

Lambda Expressions

Lambda expressions are very popular in C# and other programming languages. Lambda expressions are basically anonymous methods that allow us to create a simple way of representing a set of instructions.

You will find the lambda expressions in functional programming and LINQ.

An example of a lambda expression is as follows:

Func<int, int, int> add = (a, b) => a + b;
Console.WriteLine(add(10, 12));

In this case, the entry (a, b) => a + b is a lambda expression that we assign to a Func delegate. The expression takes two integer values and returns the sum of the two.

Expression Trees

The other type of expressions in C# are expression trees. Expression trees are data structures that allow us to represent the logic of an expression and can be manipulated and executed at runtime.

An example of an expression tree is as follows:

Expression<Func<int, int, int>> expression = (a, b) => a + b;
Func<int, int, int> add = expression.Compile();
Console.WriteLine(add(10, 12));

In this example, we create an expression tree to represent the logic of adding two integer values.

We then use the Expression<Func<int, int, int>> type to declare an expression tree that matches the lambda expression.

Finally, we compile the expression into a delegate which we can invoke like a normal method.

Expressions for Dynamic Logic

Since the expressions can be built and compiled at runtime, they provide an exceptional tool for building a dynamic logic. Dynamic logic is a logic that can change through the lifetime of the application.

Consider the following example that uses an expression to build a dynamic filter:

public static List<T> Filter<T>(List<T> data, Expression<Func<T, bool>> predicate)
{
    var func = predicate.Compile();
    return data.Where(func).ToList();
}
var numbers = new List<int> {101, 2202, 302, 412, 532, 623, 712, 834, 934, 103};
var evenNumbers = Filter(numbers, n => n % 2 == 0);

In the given example, we create an expression to dynamically filter a list of integers. The “Filter” method accepts a list and a predicate expression.

Once we compile the expression, we can use it to filter the list using the “where” method from C# LINQ.

Conclusion

In this tutorial, we discussed how we can use the C# expressions to create a dynamic logic that can change and can be compiled at the runtime of an application.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list