简体   繁体   English

C#在某种程度上是这个委托

[英]c# What is this delegate within a sort

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); 
            }); 

For certain classes which belong to the .NET framework, like the String class and the classes which represent a number (int, float, decimal, ...), the delegate is optional because the interface IComparable (see @Idle_Mind answer) is already implemented. 对于某些属于.NET Framework的类,例如String类和代表数字的类(int,float,decimal,...),委托是可选的,因为接口IComparable (请参阅@Idle_Mind答案)实现。

You might define this delegate if you want to implement a special behaviour or if you want to use the comparison mechanism on a non-editable class which doesn't implement IComparable . 如果要实现特殊行为,或者要在未实现IComparable的不可编辑类上使用比较机制,则可以定义此委托。

Considering you have to sort a string normally : 考虑到您必须正常地对字符串进行排序:

string s = "eabdfc"; //String to sort
char[] sa = s.ToCharArray(); //Convert to array
Array.Sort(sa); //Sort without any delegate
Console.WriteLine(new string(sa)); // The new string sorted

Considering you have to sort a string in the other direction, you can replace the Array.Sort line by : 考虑到必须在另一个方向上对字符串进行排序,可以将Array.Sort行替换为:

Array.Sort(sa, (x, y) => x.CompareTo(y) * -1);

Why ? 为什么呢 Because CompareTo must return : 因为CompareTo必须返回:

  • -1 if element x < y 如果元素x <y则为-1
  • 0 if element x = y 如果元素x = y,则为0
  • 1 if element x > y 如果元素x> y为1

So, if you want to use Array.Sort with any classes you have to : either implement IComparable interface (if it's an user defined class), or define the delegate. 因此,如果要对任何类使用Array.Sort,则必须:实现IComparable接口(如果它是用户定义的类),或者定义委托。

To sum up: the delegate offer more flexibility because we can write different behaviours at different places for the same class or the same object while IComparable offer a default behaviour for a class. 总结:委托提供了更大的灵活性,因为我们可以在同一地方为同一类或同一对象编写不同的行为,而IComparable为一个类提供默认行为。

I hope my english is understandable, feel free to correct me. 我希望我的英语是可以理解的,请随时纠正我。

The delegate, known as an anonymous function, allows you to declare a comparison mechanism without needing a completely separate function as below: 委托(称为匿名函数)使您可以声明比较机制,而无需完全独立的函数,如下所示:

    private void button1_Click(object sender, EventArgs e)
    {
        Things t1 = new Things();
        t1.Item = "z";
        Things t2 = new Things();
        t2.Item = "a";

        Things[] things = new Things[]{ t1, t2};
        Array.Sort(things, CompareThings);

        foreach(Things t in things)
        {
            Console.WriteLine(t.Item);
        }
    }

    private int CompareThings(Things c1, Things c2)
    {
        return c1.Item.CompareTo(c2.Item);
    }

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). 这是MSDN上的示例,其中显示了一个排序的两个版本,一个具有匿名函数,一个具有声明的函数(如上所述)。

As a side note, the explicit comparison of c1.Item to c2.Item is necessary because .Net doesn't know how it should compare one "Things" instance to another. 附带说明一下, c1.Itemc2.Itemc1.Item进行显式比较,因为.Net不知道如何将一个“ Things”实例与另一个“ Things”实例进行比较。 If you implement the IComparable interface, however, then your code becomes cleaner as you don't need the anonymous or separate function: 但是,如果实现IComparable接口,则代码将变得更加简洁 ,因为您不需要匿名或单独的函数:

public class Things : IComparable<Things>
{
    public string Item = "";

    int IComparable<Things>.CompareTo(Things other)
    {
        return this.Item.CompareTo(other.Item);
    }

}

Followed by: 其次是:

    private void button1_Click(object sender, EventArgs e)
    {
        Things t1 = new Things();
        t1.Item = "z";
        Things t2 = new Things();
        t2.Item = "a";

        Things[] things = new Things[]{ t1, t2};
        Array.Sort(things); // <-- the intenal implementation of CompareTo() we added to class Things will be used!

        foreach(Things t in things)
        {
            Console.WriteLine(t.Item);
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM