Skip to content

Default comparer

George George

Asked Jun 13, 2008, 12:55 AM EDT2 answers
Share
LinkedInX

Hello everyone,


For a customized class, like I showed in my sample, what will Comparer<>.Default be returned? In my sample, there is System.ArgumentException.

[Code]
class Program
    {
        class Foo
        {

        }
       
        static void Main(string[] args)
        {
            IComparer<Foo> FooComparer = Comparer<Foo>.Default;
            Foo f1 = new Foo();
            Foo f2 = new Foo();

            // System.ArgumentException
            FooComparer.Compare(f1, f2);

            return;
        }
    }
[/Code]


thanks in advance,
George

2 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • Bechir Bejaoui
    0

    It's because compare method is not void it returns an integer.

    I invite you to take a look on my article about IComparable(CompareTo) ans IComparer(Compare) I can be helpful for you as well

    0
  • 0

    Thanks Bechir Bejaoui!

     

    Great article! Two more comments after study,

    1.

    I think Default.Compare method will convert current instance to IComparable and call IComparable.Compare?

    2.

    A customized class is not implementing IComparable as default?

     

    regards,

    George

    0