Skip to content

Instantly share code, notes, and snippets.

@JeremyLikness
Created June 15, 2021 16:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JeremyLikness/0d13e15b4a1313756beaec39c643274a to your computer and use it in GitHub Desktop.
Save JeremyLikness/0d13e15b4a1313756beaec39c643274a to your computer and use it in GitHub Desktop.
Using expressions for comparisons
using System;
using System.Linq.Expressions;
namespace expressioncompare
{
class Program
{
static void Main(string[] args)
{
var left = Expression.Parameter(typeof(int), "left");
var right = Expression.Parameter(typeof(int), "right");
var lt = Expression.LessThan(left, right);
var lambda = Expression.Lambda<Func<int, int, bool>>(lt, left, right);
var fn = lambda.Compile();
for (var x = 0; x < 5; x++)
{
for (var y = 0; y < 5; y++)
{
Console.WriteLine($"{x} < {y} = {fn(x,y)}");
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment