gpt4 book ai didi

.net - 为什么我们要递归实现接口(interface)?

转载 作者:行者123 更新时间:2023-12-04 13:55:21 25 4
gpt4 key购买 nike

我知道任何集合(这里我说的是常规的非泛型)都应该在常规对象集合的情况下实现 ICollection、IEnumerable 和 IList,在字典的情况下实现 IDictionary。

[不过,我问的问题并不特定于收藏]

IList 派生自 ICollection 和 IEnumerable

ICollection 派生自 IEnumerable

仅仅让一个集合(例如 ArrayList)实现 IList 还不够吗?

在对象浏览器中,它显示集合类(例如 ArrayList)正在实现 IList、ICollection 和 IEnumerator。

我知道即使我们指定了所有三个集合,.Net 也只会接受一次定义。

但我的问题是,

  • 是否有任何最佳实践或建议可以指导我们为集合类(或任何类似的类)指定所有三个接口(interface)?
  • 或者它只是对象浏览器的属性,将其显示为 3 个单独的实现? 【刚查了一下,发现不是Object浏览器的属性。对象浏览器只显示类定义中指定的接口(interface)]
  • 最佳答案

    我相信它只是以这种方式显示它的对象浏览器。我刚刚试过这个:

    using System.Collections;
    using System.Collections.Generic;

    public class Foo : IEnumerable<int>, IEnumerable
    {
    public IEnumerator<int> GetEnumerator() { return null; }
    IEnumerator IEnumerable.GetEnumerator() { return null; }

    }

    public class Bar : IEnumerable<int>
    {
    public IEnumerator<int> GetEnumerator() { return null; }
    IEnumerator IEnumerable.GetEnumerator() { return null; }
    }

    将其加载到对象浏览器中会显示两个类的两个接口(interface)。

    请注意,如果您从另一个实现继承,有时可能需要重新声明一个接口(interface) - 如果它之前已显式或以非虚拟方式实现,它允许您重新实现该接口(interface)。我不相信这里是这种情况,但值得一提。

    但总的来说,您绝对不必指定所有接口(interface),而且我通常不会这样做。

    关于.net - 为什么我们要递归实现接口(interface)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3496593/

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