Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace Core
- {
- /// <summary>
- /// Generice impl of <see cref="IEqualityComparer"/>
- /// </summary>
- /// <example>
- /// var treeComparer = new KeyEqualityComparer<TreeDefinition, int?>((t) => t.TreeID);
- /// </example>
- /// <typeparam name="T">The type of object comparing</typeparam>
- /// <typeparam name="TKey">The type of key comparing</typeparam>
- public class KeyEqualityComparer<T, TKey> : IEqualityComparer<T>
- {
- protected readonly Func<T, TKey> keyExtractor;
- public KeyEqualityComparer(Func<T, TKey> keyExtractor)
- {
- this.keyExtractor = keyExtractor;
- }
- public virtual bool Equals(T x, T y)
- {
- return this.keyExtractor(x).Equals(this.keyExtractor(y));
- }
- public int GetHashCode(T obj)
- {
- return this.keyExtractor(obj).GetHashCode();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment