C#:从其基类的实例返回继承的类(通用列表)

发布于 2024-09-19 18:16:03 字数 615 浏览 5 评论 0原文

这可能是我只是完全倒退地记住了事情,但我想更多地了解我做错了什么......

我已经声明一个类只不过是从通用列表直接继承(这样做是为了简化命名) ),像这样:

public class FooList : List<Foo> {}

现在在与此类完全分离的另一个方法中,我尝试返回此类的实例,但是我想根据标准过滤该类,因此我使用 lambda 表达式:

var list = new FooList(); // imagine this fills it with different items
var filtered = list.FindAll(c => c.Something == "filter criteria");

现在根据对于 FindAll 方法,这应该返回一个 List[Foo]。但是,我想将此对象作为 FooList 返回,而不是 List[Foo]。我是否必须创建 FooList 的新实例并从 List[Foo] 复制项目?

如果是这样,为什么?为什么我不能直接将 List 转换为 FooList,因为它们是同一个对象?

如果可以做到这一点,我该怎么做?

非常感谢!

This is probably me just remembering things completely backwards, but I'd like to know more about what I'm doing wrong...

I have declared a class to be nothing more than a direct inheritance from a generic list (done to simplify naming), something like this:

public class FooList : List<Foo> {}

now in another method completely separate from this class, I am trying to return an instance of this class, however I want to filter the class based on a criterion, so I'm using a lambda expression:

var list = new FooList(); // imagine this fills it with different items
var filtered = list.FindAll(c => c.Something == "filter criteria");

now according to the FindAll method, this SHOULD return a List[Foo]. However, I want to return this object as a FooList, not a List[Foo]. Do I have to create a new instance of FooList and copy the items from the List[Foo]?

If so, why? why can't I convert a List to a FooList directly, since they are the same object?

If this CAN be done, how do I do it?

many thanks!

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

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

发布评论

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

评论(3

懵少女 2024-09-26 18:16:03

您的问题是您使用了错误的工具来完成这项工作。 继承不是一种简化命名的机制;继承是一种机制,主要设计用于 (1) 对业务域对象类之间的“一种”关系进行建模,以及 (2) 实现相关类型之间实现细节的共享和专门化。

如果您想简化命名,那么正确的工具是将其放在

using FooList = System.Collections.Generic.List<Foo>;

每个文件的顶部。

Your problem is that you are using the wrong tool for the job. Inheritance is not a mechanism for simplifying naming; inheritance is a mechanism primarily designed for (1) modeling "is a kind of" relationships between classes of business domain objects, and (2) enabling the sharing and specialization of implementation details amongst related types.

If you want to simplify naming then the right tool for the job is to put

using FooList = System.Collections.Generic.List<Foo>;

at the top of every file.

岛歌少女 2024-09-26 18:16:03

它们不是同一件事。 FooList 是一个 List,但也是一个 List,它由 FindAll() 返回code> 函数继承自 List不是 FooList。您需要构造一个新的 FooList

您可以执行类似为 FooList 创建一个构造函数的操作,该构造函数采用 IEnumerable 对象,如下

class FooList : List<foo>
{
   public FooList(IEnumerable<foo> l)
        : base(l)
   {  }
}

:或者您也可以执行以下操作:

 var list = new FooList();
 var filtered = list.FindAll(c => c.Something == "filter criteria");
 var newList = new FooList();
 newList.AddRange(filtered);
 return newList;

所示 其他答案,如果您不添加任何功能,那么您应该使用 using 关键字创建一个别名。

They are not the same thing. A FooList is a List<foo> but a List<foo>, which is being returned by the FindAll() function inherited from List<foo>, is not a FooList. You will need to construct a new FooList.

You could do something like create a Constructor for FooList that takes a IEnumerable<foo> object like this:

class FooList : List<foo>
{
   public FooList(IEnumerable<foo> l)
        : base(l)
   {  }
}

Or you could also do something like this:

 var list = new FooList();
 var filtered = list.FindAll(c => c.Something == "filter criteria");
 var newList = new FooList();
 newList.AddRange(filtered);
 return newList;

However as mentioned in some of the other answers, if you are not adding any functionality then you should just create an alias with the using keyword.

一抹苦笑 2024-09-26 18:16:03

不要声明新类 FooList。只需使用别名即可。在源文件的顶部写入:

using FooList = System.Collections.Generic.List<Foo>;

现在您可以在任何地方编写 FooList,它将被视为与 List 完全相同。

Don’t declare a new class FooList. Just use an alias. At the top of your source file, write:

using FooList = System.Collections.Generic.List<Foo>;

Now you can write FooList everywhere and it will be treated to be exactly identical to List<Foo>.

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