gpt4 book ai didi

c# - OrderByDescending() 根据 MSDN,这到底是什么意思?

转载 作者:太空狗 更新时间:2023-10-29 22:15:16 24 4
gpt4 key购买 nike

谁能帮我拆开这里的元素并帮助我理解它们是什么?

public static IOrderedEnumerable<TSource> OrderByDescending<TSource, TKey>(
this IEnumerable<TSource> source,
Func<TSource, TKey> keySelector
)

什么是TSource和TKey?什么是键选择器? IOrderedEnumerable 到底是什么?

Func<> 是做什么的??

为什么 MSDN 如此神秘?

最佳答案

分解

  • TSource:这是集合中需要排序的元素类型
  • TKey:元素排序的类型键。
  • Func<TSource,TKey> : 代表将返回集合中给定元素的键

这个函数本质上是一个排序函数。因此,它需要一种方法来比较集合中的元素。这个特定的方法假定对于给定的对象,有一个相应的键值可以用来对它们进行排序。

以下面这个类(class)Student为例

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

如果我想对 Student 的集合进行排序通过他们的名字实例我可以做以下事情

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

在这种情况下,值如下

  • 来源:Student
  • key :String
  • Func<TSource,TKey> : 这是传入的 lambda 表达式 x => x.Name

关于c# - OrderByDescending() 根据 MSDN,这到底是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1827224/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com