C# 中的接口和列表

发布于 2024-10-12 13:12:43 字数 510 浏览 2 评论 0原文

我对以下接口和类有问题:

public interface IRelated
{

}

public class BaseItem:IRelated
{
      public string Name{get;set;}
      public List<IRelated> RelatedItems{get;set;}
}

现在,当我尝试在其他类中执行以下操作时,它会给我一个编译错误:

List<IRelated> listofrelateditems=new List<BaseItem>();

无法隐式转换类型 ListList

接口的原因是将来也许我会有另一个类可以与这个 BaseItem 相关。

im having issues with the following interface and a class:

public interface IRelated
{

}

public class BaseItem:IRelated
{
      public string Name{get;set;}
      public List<IRelated> RelatedItems{get;set;}
}

Now when i try to do in other classes the following it gives me a compilation error:

List<IRelated> listofrelateditems=new List<BaseItem>();

Cannot implicity convert type
List<BaseItem> to List<IRelated>

The reason of the interface is that in the future maybe i will have another class that can be Related to this BaseItem.

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

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

发布评论

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

评论(3

他是夢罘是命 2024-10-19 13:12:43

你就是不能这样做 - 即使 .NET 4 中的通用协方差也无法帮助你,因为 List 是一个类,甚至 IList是不变的,因为它有 T 进来和出去。

正是因为您将来可能会有一个新的IRelated实现,所以您不能这样做。考虑一下:

List<IRelated> listOfRelatedItems = new List<BaseItem>();
listOfRelatedItems.Add(new OtherRelatedItem());

其中 OtherRelatedItem 实现 IRelatedBaseItem 派生。现在您已经有了一个 List,其中包含 BaseItem 以外的内容!换句话说,它破坏了类型安全。

基本上,您必须创建一个 List 而不是 List

有关通用方差的更多信息以及为什么它有时适用有时不适用,请访问 NDC 2010 视频页面搜索“variance”即可找到我去年就该主题进行的演示的视频。

You just can't do that - even the generic covariance in .NET 4 won't help you, because List<T> is a class and even IList<T> is invariant as it has T coming "in" as well as going out.

It's precisely because you might have a new implementation of IRelated in the future that you can't do that. Consider:

List<IRelated> listOfRelatedItems = new List<BaseItem>();
listOfRelatedItems.Add(new OtherRelatedItem());

where OtherRelatedItem implements IRelated but doesn't derive from BaseItem. Now you've got a List<BaseItem> which contains something other than a BaseItem! In other words, it breaks type safety.

Basically you'll have to create a List<IRelated> instead of a List<BaseItem>.

For more on generic variance and why it's sometimes applicable and sometimes not, go to the NDC 2010 videos page and search for "variance" to find the video of a presentation I gave on the topic last year.

灯角 2024-10-19 13:12:43

就像您自己所说:您可能有另一个可以是 IRelated 的类。如何将此类的实例添加到 List 中?

你必须写:

List<IRelated> listofrelateditems = new List<IRelated>();

Like you yourself said: You may have another class that can be IRelated. How are you going to add an instance of such a class to a List<BaseItem>?

You will have to write:

List<IRelated> listofrelateditems = new List<IRelated>();
谁把谁当真 2024-10-19 13:12:43

更改为:

List<IRelated> listofrelateditems=new List<IRelated>();

Change to:

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