|
1 | 1 | # Expression Equality Comparer
|
2 | 2 |
|
3 |
| -## Using |
| 3 | +The default equality comparison logic between two <xref:System.Linq.Expressions.Expression>s verifies only if they refer to the same instance. |
4 | 4 |
|
5 |
| -## Extends |
| 5 | +```csharp |
| 6 | +using System; |
| 7 | +using System.Collections.Generic; |
| 8 | +using System.Linq.Expressions; |
| 9 | + |
| 10 | +public class Program |
| 11 | +{ |
| 12 | + static HashSet<Expression> expressions; |
| 13 | + public static void Main() |
| 14 | + { |
| 15 | + IEqualityComparer<Expression> comparer = EqualityComparer<Expression>.Default; |
| 16 | + expressions = new HashSet<Expression>(comparer); |
| 17 | + |
| 18 | + Console.WriteLine("Expression equality:"); |
| 19 | + AddExpression((x, y) => x + y); |
| 20 | + AddExpression((x, y) => x * y); |
| 21 | + AddExpression((x, y) => x + y); |
| 22 | + AddExpression((a, b) => a * b); |
| 23 | + Console.WriteLine(); |
| 24 | + } |
| 25 | + |
| 26 | + static void AddExpression(Expression<Func<int,int,int>> exp) |
| 27 | + { |
| 28 | + if (expressions.Add(exp)) |
| 29 | + { |
| 30 | + Console.WriteLine("Added {0}, Count = {1}, HashCode = {2}", |
| 31 | + exp.ToString(), expressions.Count.ToString(), exp.GetHashCode()); |
| 32 | + } |
| 33 | + else |
| 34 | + { |
| 35 | + Console.WriteLine("An expression equal to {0} is already in the collection.", exp.ToString()); |
| 36 | + } |
| 37 | + } |
| 38 | +} |
| 39 | +/* This example produces the following output: |
| 40 | + * |
| 41 | + Expression equality: |
| 42 | + Added (x, y) => (x + y), Count = 1, HashCode = 9293733 |
| 43 | + Added (x, y) => (x * y), Count = 2, HashCode = 60803526 |
| 44 | + Added (x, y) => (x + y), Count = 3, HashCode = 4729318 |
| 45 | + Added (a, b) => (a * b), Count = 4, HashCode = 4497 |
| 46 | + * |
| 47 | +*/ |
| 48 | +``` |
| 49 | + |
| 50 | +The <xref:ExpressionTreeToolkit.ExpressionEqualityComparer.Default> implementation of the <xref:ExpressionTreeToolkit.ExpressionEqualityComparer> instead compares each node in the expression tree to determine if two <xref:System.Linq.Expressions.Expression>s are equal. |
| 51 | + |
| 52 | +```csharp |
| 53 | +using System; |
| 54 | +using System.Collections.Generic; |
| 55 | +using System.Linq.Expressions; |
| 56 | + |
| 57 | +public class Program |
| 58 | +{ |
| 59 | + static HashSet<Expression> expressions; |
| 60 | + public static void Main() |
| 61 | + { |
| 62 | + IEqualityComparer<Expression> comparer = ExpressionTreeToolkit.ExpressionEqualityComparer.Default; |
| 63 | + expressions = new HashSet<Expression>(comparer); |
| 64 | + |
| 65 | + Console.WriteLine("Expression equality:"); |
| 66 | + AddExpression((x, y) => x + y); |
| 67 | + AddExpression((x, y) => x * y); |
| 68 | + AddExpression((x, y) => x + y); |
| 69 | + AddExpression((a, b) => a * b); |
| 70 | + Console.WriteLine(); |
| 71 | + } |
| 72 | + |
| 73 | + static void AddExpression(Expression<Func<int,int,int>> exp) |
| 74 | + { |
| 75 | + if (expressions.Add(exp)) |
| 76 | + { |
| 77 | + Console.WriteLine("Added {0}, Count = {1}, HashCode = {2}", |
| 78 | + exp.ToString(), expressions.Count.ToString(), exp.GetHashCode()); |
| 79 | + } |
| 80 | + else |
| 81 | + { |
| 82 | + Console.WriteLine("An expression equal to {0} is already in the collection.", exp.ToString()); |
| 83 | + } |
| 84 | + } |
| 85 | +} |
| 86 | +/* This example produces the following output: |
| 87 | + * |
| 88 | + Expression equality: |
| 89 | + Added (x, y) => (x + y), Count = 1, HashCode = 10529038 |
| 90 | + Added (x, y) => (x * y), Count = 2, HashCode = 35464548 |
| 91 | + An expression equal to (x, y) => (x + y) is already in the collection. |
| 92 | + An expression equal to (a, b) => (a * b) is already in the collection. |
| 93 | + * |
| 94 | +*/ |
| 95 | +``` |
| 96 | + |
| 97 | +> [!NOTE] |
| 98 | +> The comparison logic is different for each <xref:System.Linq.Expressions.ExpressionType>. Please refers to the source code. |
0 commit comments