集合&lt;T&gt;与 List<T>你应该在你的界面上使用什么? [英] Collection&lt;T&gt; versus List&lt;T&gt; what should you use on your interfaces?

查看:22
本文介绍了集合&lt;T&gt;与 List<T>你应该在你的界面上使用什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码如下:

namespace Test
{
    public interface IMyClass
    {
        List<IMyClass> GetList();
    }

    public class MyClass : IMyClass
    {
        public List<IMyClass> GetList()
        {
            return new List<IMyClass>();
        }
    }
}

当我运行代码分析时,我得到以下建议.

When I Run Code Analysis i get the following recommendation.

警告 3 CA1002:Microsoft.Design:将IMyClass.GetList()"中的List"更改为使用 Collection、ReadOnlyCollection 或 KeyedCollection

Warning 3 CA1002 : Microsoft.Design : Change 'List' in 'IMyClass.GetList()' to use Collection, ReadOnlyCollection or KeyedCollection

我应该如何解决这个问题以及这里有什么好的做法?

How should I fix this and what is good practice here?

推荐答案

回答问题的为什么"部分,为什么不List,原因是面向未来和 API 简单.

To answer the "why" part of the question as to why not List<T>, The reasons are future-proofing and API simplicity.

面向未来

List 不是为了通过子类化而易于扩展的设计;它旨在为内部实现快速.您会注意到它的方法不是虚拟的,因此不能被覆盖,并且它的 Add/插入/Remove 操作.

这意味着,如果您将来需要更改集合的行为(例如,拒绝人们尝试添加的空对象,或者在发生这种情况时执行其他工作,例如更新您的类状态),那么您需要将您返回的集合类型更改为您可以子类化的集合类型,这将是一个重大的接口更改(当然,更改诸如不允许 null 之类的事物的语义也可能是一个接口更改,但诸如更新内部类状态之类的事情不会).

This means that if you need to alter the behavior of the collection in the future (e.g. to reject null objects that people try to add, or to perform additional work when this happens such as updating your class state) then you need to change the type of collection you return to one you can subclass, which will be a breaking interface change (of course changing the semantics of things like not allowing null may also be an interface change, but things like updating your internal class state would not be).

因此,通过返回一个可以轻松子类化的类,例如 Collection 或诸如 IListICollectionIEnumerable 您可以将内部实现更改为不同的集合类型来满足您的需求,而不会破坏消费者的代码,因为它仍然可以作为他们期望的类型返回.

So by returning either a class that can be easily subclassed such as Collection<T> or an interface such as IList<T>, ICollection<T> or IEnumerable<T> you can change your internal implementation to be a different collection type to meet your needs, without breaking the code of consumers because it can still be returned as the type they are expecting.

API 简单性

List 包含很多有用的操作,例如BinarySearchSort 等等.但是,如果这是您要公开的集合,那么您很可能会控制列表的语义,而不是消费者.因此,虽然您的类在内部可能需要这些操作,但您的类的使用者不太可能想要(甚至应该)调用它们.

List<T> contains a lot of useful operations such as BinarySearch, Sort and so on. However if this is a collection you are exposing then it is likely that you control the semantics of the list, and not the consumers. So while your class internally may need these operations it is very unlikely that consumers of your class would want to (or even should) call them.

因此,通过提供更简单的集合类或接口,您可以减少 API 用户看到的成员数量,并使他们更容易使用.

As such, by offering a simpler collection class or interface, you reduce the number of members that users of your API see, and make it easier for them to use.

这篇关于集合&lt;T&gt;与 List<T>你应该在你的界面上使用什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆