Created
January 19, 2016 14:46
-
-
Save zharro/78339bec3af4b07bf728 to your computer and use it in GitHub Desktop.
Factory class for creating instances of IComparer (из книги Сергея Теплякова "Паттерны проектирования на платформе .NET")
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.Collections.Generic; | |
namespace zharro.gists | |
{ | |
class ComparerFactory | |
{ | |
public static IComparer<T> Create<T>(Comparison<T> comparer) | |
{ | |
return new DelegateComparer<T>(comparer); | |
} | |
private class DelegateComparer<T> : IComparer<T> | |
{ | |
private readonly Comparison<T> _comparer; | |
public DelegateComparer(Comparison<T> comparer) | |
{ | |
_comparer = comparer; | |
} | |
public int Compare(T x, T y) | |
{ | |
return _comparer(x, y); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage of ComparerFactory: