为什么我不能在扩展 List 的类中调用 OrderBy?

发布于 2024-08-18 22:01:56 字数 374 浏览 6 评论 0 原文

我有一个类 Deck,其中包含一个名为 Shuffle 的方法。

我正在努力重构 Deck 以扩展 List,而不是让 List卡作为一种财产。但是,虽然 Cards.OrderBy (a => Guid.NewGuid ()) 有效,但 OrderBy (a => Guid.NewGuid ()) 却不起作用:

错误 CS0103:当前上下文中不存在名称“OrderBy”(CS0103)

为什么这不起作用?

I have a class, Deck, that contains a method called Shuffle.

I'm working on refactoring Deck to extend List<Card>, rather than having List<Card> Cards as a property. However, while Cards.OrderBy (a => Guid.NewGuid ()) worked, OrderBy (a => Guid.NewGuid ()) does not:

Error CS0103: The name 'OrderBy' does not exist in the current context (CS0103)

Why does this not work?

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

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

发布评论

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

评论(3

物价感观 2024-08-25 22:01:56

this 添加到 OrderBy 的前面,因为

this.OrderBy(a => Guid.NewGuid()); // a random ordering

OrderByIEnumerable 上的扩展方法,而不是List 上的公共方法。如果您在没有上下文的情况下键入 OrderBy,编译器将查找名为 OrderBy 的实例或静态方法。仅当您在 OrderBy 前添加 IEnumerable 实例时,编译器才会找到 OrderBy。作为Deck:ListList :IEnumerable,使用关键字 this(对当前实例的引用)将为编译器提供定位方法 Enumerable.OrderBy 所需的上下文>。

List 继承被认为是 不好的做法; 在公共 API 中。首先,List 不是为继承而设计的,可能应该被密封;现在为时已晚。一般来说,在使用框架类时,您应该更倾向于组合而不是继承。

Add this to the front of OrderBy as in

this.OrderBy(a => Guid.NewGuid()); // a random ordering

OrderBy is an extension method on IEnumerable<T> and is not a public method on List<T>. If you type OrderBy with no context the compiler will look for an instance or static method named OrderBy. It is only if you prefix OrderBy with an instance of IEnumerable<T> will the compiler find OrderBy. As Deck : List<Card> and List<Card> : IEnumerable<Card>, using the keyword this (a reference to the current instance) will give the compiler the context it needs to locate the method Enumerable.OrderBy.

It is considered bad practice to inherit from List<T> in a public API. First, List<T> was not designed for inheritance and probably should have been sealed; too late for that now. In general, you should favor composition over inheritance when using framework classes.

一场春暖 2024-08-25 22:01:56

OrderBy 是一种扩展方法,因此它只能与 IEnumerable 类型的限定符一起使用。

您需要编写this.OrderBy。 (thisDeck 类型的限定符,它间接继承 IEnumerable

请注意,OrderBy 不是就地排序;如果要对现有实例进行排序,请调用 Sort((a, b) => 2 - 2 * rand.Next(0, 1)),其中 randRandom 类的实例。


注意:这是不好的做法继承List。相反,您应该继承 System.Collections.ObjectModel.Collection

OrderBy is an extension method, so it can only be used with a qualifier of type IEnumerable<T>.

You need to write this.OrderBy. (this is a qualifier of type Deck, which indirectly inherits IEnumerable<Card>)

Note that OrderBy is not an in-place sort; if you want to sort the existing instance, call Sort((a, b) => 2 - 2 * rand.Next(0, 1)) where rand is an instance of the Random class.


Note: It is bad practice to inherit List<T>. Instead, you should inherit System.Collections.ObjectModel.Collection<T>.

得不到的就毁灭 2024-08-25 22:01:56

OrderBy 不是 List 上的方法 - 相反,它被定义为扩展方法 Enumerable.OrderBy

因为它不是类的方法,所以你需要让编译器看到它。您可以通过调用来做到这一点:

this.OrderBy(a => Guid.NewGuid());

但是,我建议重新考虑您的方法。子类化 List 不是一个好主意 - 通过封装 List 实例来实现 IList 会好得多。 List 应该是实现细节,而不是 API 本身的一部分。

OrderBy is not a method on List<T> - rather, it's defined as an extension method Enumerable.OrderBy.

Because it's not a method of the class, you need to make the compiler see this. You can do that by calling:

this.OrderBy(a => Guid.NewGuid());

However, I recommend rethinking your approach here. Subclassing List<T> is a bad idea - it's much better to implement IList<T> by encapsulating your List<T> instance. List<T> should be an implementation detail, and not part of the API itself.

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