开发者

OrderByDescending() per MSDN, what on earth does this mean?

开发者 https://www.devze.com 2022-12-12 19:54 出处:网络
Can someone please help be take apart the elements here and help me understand what they are? public static IOrderedEnumerable<TSource> OrderByDescending<TSour开发者_如何学运维ce, TKey>(

Can someone please help be take apart the elements here and help me understand what they are?

public static IOrderedEnumerable<TSource> OrderByDescending<TSour开发者_如何学运维ce, TKey>(
    this IEnumerable<TSource> source,
    Func<TSource, TKey> keySelector
)

What is TSource and TKey? What is a keySelector? What the heck is an IOrderedEnumerable?

What does Func<> do??

Why is MSDN so cryptic?


Break Down

  • TSource: This is the type of elements in the collection which need to be ordered
  • TKey: The type key by which the elements are ordered.
  • Func<TSource,TKey>: Delegate which will return a key for a given element in the collection

This function is essentially a sorting function. As such it needs a way to compare the elements in the collection. This particular method assumes that for a given object there is a corresponding key value by which they can be sorted.

Take for example the following class Student

class Student { 
  string Name { get; set; }
  ...
}

If I wanted to sort a collection of Student instances by their name I could do the following

IEnumerable<Student> col = GetTheStudents();
var ordered = col.OrderByDescending( x => x.Name );

In this case the values would be as follows

  • TSource: Student
  • TKey: String
  • Func<TSource,TKey>: This is the passed in lambda expression x => x.Name


I just wonder, what is exactly unclear on MSDN? Here is the topic: http://msdn.microsoft.com/en-us/library/bb548916.aspx

And here are some answers to your questions from that topic:

Type Parameters

TSource - The type of the elements of source.

TKey - The type of the key returned by keySelector.

Parameters

source - A sequence of values to order.

keySelector - A function to extract a key from an element.

comparer - An IComparer to compare keys.

Return Value

An IOrderedEnumerable whose elements are sorted in descending order according to a key.

Also, there are remarks and an example. What you posted here is just a signature of the method.

0

精彩评论

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