I don't understand the logic in the using delegate in this syntax. I'm looking for ways to sort a string and came across this...
Array.Sort (thing, delegate (Things c1, Things c2)
{
return c1.Item.CompareTo(c2.Item);
});
The delegate, known as an anonymous function, allows you to declare a comparison mechanism without needing a completely separate function as below:
Here's an example on MSDN showing two versions of a sort, one with an anonymous function, and one with a declared function (as above).
As a side note, the explicit comparison of
c1.Item
toc2.Item
is necessary because .Net doesn't know how it should compare one "Things" instance to another. If you implement the IComparable interface, however, then your code becomes cleaner as you don't need the anonymous or separate function:Followed by: