尝试更好地使用 Visual Studio!我如何找到有关排序方法的帮助?

发布于 2024-12-03 19:56:04 字数 954 浏览 0 评论 0原文

我正在阅读一本书,它提供了排序方法以及 Lambda 查询的示例。

一个例子是 {Product.Sort( (x,y) => x.Name.CompareTo(y.Name) );

这确实花了我一段时间来理解,因为我不明白如何 < code>.Sort 正在处理 lambda 上的两个输入。

我尝试单击 Sort 并按 F1 寻求帮助,但是,它没有提供任何信息,这对我来说很有意义

也许我只是不够好,无法理解这些示例,但是,我只是无法弄清楚这是如何工作的,直到我将 Lambda 更改为 x,y,z ,这给出了错误 Error委托“System.Comparison”不接受 3 个参数

这对我来说更有意义......无论如何,经过一段时间的环顾四周,我确信我理解了 Sort 方法,但是,它花了我比我更长的时间满意。

从比我优秀得多的人那里寻求帮助——在这种情况下,你会如何寻求帮助?

通过输入 Shift+Space,我还可以生成以下内容:

其归因于需要具有两个输入的 Lambda?

I am reading through a book and it gives an example of the Sort Method along with a Lambda query.

An example is {Product.Sort( (x,y) => x.Name.CompareTo(y.Name) );

This really took me a while to understand as I did not understand how .Sort was dealing with the two inputs on the lambda.

I tried clicking on Sort and pressing F1 for help, but, it didn't give anything, that made any sense to me.

Perhaps I am just not good enough to understand those examples, but, I just could not work out how this was working until I changed the Lambda to x,y,z which gave the error Error Delegate 'System.Comparison<ConsoleApplication1.Product>' does not take 3 arguments

Which made a lot more sense to me... Anyway, after a while of looking around, I am confident that I understand the Sort method, but, it took me a lot longer than I am happy with.

From people who are much better than me - given a situation like this, how would you search for help?

By typing Shift+SpaceI was also able to produce the following:

enter image description here

However, I am just wondering, as a C# learner, how can I attribute this to requiring a Lambda with two inputs?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

小兔几 2024-12-10 19:56:04

它不需要 lambda。它只需要一个比较类型的委托。例如,可能有一个方法:

int CompareProductNames(Product x, Product y)
{
    return x.Name.CompareTo(y.Name);
}

然后您可以像这样调用 Sort

productList.Sort(CompareProductNames);

Comparison 是一个委托,它接受两个 T 类型的参数并返回一个 <代码>int。

It does not require a lambda. It requires only a delegate of type Comparison. For example, there may be a method:

int CompareProductNames(Product x, Product y)
{
    return x.Name.CompareTo(y.Name);
}

and then you could call Sort like this:

productList.Sort(CompareProductNames);

Comparison<T> is a delegate that takes two parameters of type T and returns an int.

明月夜 2024-12-10 19:56:04

听起来您不需要解释正在发生的事情,而是需要如何弄清楚。将鼠标悬停在 Sort 部分上,您将看到正在调用的特定重载。在本例中,其 Sort(Comparison)) 映射到 排序(比较(T))

It sounds like you don't need an explanation of what's going on but instead how to figure it out. Hover your mouse over the Sort part and you'll see the specific overload being called. In this case its Sort(Comparison<Product>)) which maps to Sort(Comparison(T))

遗忘曾经 2024-12-10 19:56:04

其中一种方法是查看比较文档。这是一个委托,例如指向函数的指针,需要两个T 类型的参数并返回整数值。 T 类型是在 List 类的 Sort 方法中声明的,因此这里的 T 是一个 Product。 Lambda 表达式只是该函数的快捷方式。
要点是要理解这段代码相当于:

    public void DoSort()
    {
        list.Sort(Compare); //Pass method as a Comparison<Product>
    }

    public int Compare(Product x, Product y)
    {
        return x.Name.CompareTo(y.Name);
    }

One of the approaches is to check out the Comparison documentation. It's a delegate, e.g. pointer to a function, that takes two arguments of type T and returns integer value. T type is declare in Sort<T> method of the List<T> class, so T here is a Product. Lambda expressions is just a shortcut for this function.
The main point is to understand that this code is just an equivalent of:

    public void DoSort()
    {
        list.Sort(Compare); //Pass method as a Comparison<Product>
    }

    public int Compare(Product x, Product y)
    {
        return x.Name.CompareTo(y.Name);
    }
婴鹅 2024-12-10 19:56:04

如果您不明白,请在 Google 上查找 List.Sort,然后 Google 查找其参数。这就是当今大多数开发人员的工作方式。

就我而言,如果没有 Google 和像 SO 这样的网站的帮助,我就无法编写代码。

另外,您必须了解委托如何工作(在排序的情况下),因为 lambda 实际上是传递给委托的匿名方法。输入由 lambda 捕获,是委托的参数。

Look up List<T>.Sort on Google and then Google it's arguments if you don't understand. That's the way most dev's work these days.

I, for one, could not write code without the help of Google and sites like SO itself.

Also, you have to understand how delegates work, in the case of sort, as the lambda is really an anonymous method that is passed to the delegate. The input's are captured by the lambda, are arguments to the delegate.

俯瞰星空 2024-12-10 19:56:04

如果您知道 lambda 计算结果为单个委托值,那么理解 F1 附带的 MSDN 文档可能会更容易。 MSDN 文档显示了带有单个参数的 Sort 的两个重载;单击这些参数的类型将显示 Comparison 是一个带有两个参数的委托。

您必须在 MSDN 文档中执行几个步骤才能找到您要查找的信息。

It might be easier to understand the MSDN doc that comes up with F1 if you know that a lambda evaluates to a single delegate value. The MSDN doc shows two overloads for Sort with a single argument; clicking on the argument type of these will show that Comparison is a delegate taking two arguments.

You do have to drill in a few steps in the MSDN doc to find the information you are looking for.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文