Created
June 15, 2021 16:28
-
-
Save JeremyLikness/0d13e15b4a1313756beaec39c643274a to your computer and use it in GitHub Desktop.
Using expressions for comparisons
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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