开发者

Trying to use Visual Studio better! How would I find help on the sort method?

开发者 https://www.devze.com 2023-04-03 02:31 出处:网络
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) );

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:

Trying to use Visual Studio better! How would I find help on the sort method?

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


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.


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


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


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号