Sunday, 15 January 2012

c# - Comparer.Default Property -



c# - Comparer<T>.Default Property -

this question comes little wonders: comparer<t>.default . wondering author wrote in line:

if type want compare implements icomparable<t> or if type system.nullable<t> t implements icomparable, there class in system.collections.generic namespace called comparer<t> exposes property called default create singleton represents default comparer items of type.

so illustration :

i have class :

class foo : icomparable<foo> { ... } public class foocomparer : icomparer<foo> { ... }

comparer class implemented public abstract class comparer<t> : icomparer, icomparer<t> . question how default property works overall , , how works?

thanks

comparer<t>.default doesn't utilize foocomparer class. returns instance of internal class genericcomparer<t>. class has constraint t must implement icomparable<t> can delegate calls compare method compare methods of instances gets passed.

something this:

internal class genericcomparer<t> : comparer<t> t : icomparable<t> { public override int compare(t x, t y) { if (x != null) { if (y != null) homecoming x.compareto(y); homecoming 1; } else { if (y != null) homecoming -1; homecoming 0; } } // ... }

c#

No comments:

Post a Comment