Comparer.cs 905 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. //
  2. // System.Collections.Comparer
  3. //
  4. // Author:
  5. // Sergey Chaban ([email protected])
  6. //
  7. using System;
  8. using System.Collections;
  9. namespace System.Collections {
  10. public sealed class Comparer : IComparer {
  11. public static readonly Comparer Default;
  12. // Class constructor
  13. static Comparer ()
  14. {
  15. Default = new Comparer ();
  16. }
  17. // Public instance constructor
  18. private Comparer ()
  19. {
  20. }
  21. // IComparer
  22. public int Compare (object a, object b)
  23. {
  24. int res=0;
  25. if (a is IComparable) {
  26. res = (a as IComparable).CompareTo (b);
  27. } else if (b is IComparable) {
  28. res = (b as IComparable).CompareTo (a);
  29. } else {
  30. throw new ArgumentException ("Neither a nor b IComparable");
  31. }
  32. return res;
  33. }
  34. public override string ToString ()
  35. {
  36. return "mono::System.Collections.Comparer";
  37. }
  38. }
  39. }