收藏< T>与列表< T>你应该在你的接口使用什么? [英] Collection<T> versus List<T> what should you use on your interfaces?

查看:126
本文介绍了收藏< T>与列表< T>你应该在你的接口使用什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在code看起来如下:

The code looks like below:

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

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

当我运行code分析,我得到了以下建议。

When I Run Code Analysis i get the following recommendation.

警告3 CA1002:Microsoft.Design:改变名单中的IMyClass.GetList()使用集合,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?

推荐答案

要回答这个问题的为什么的一部分,为什么不能名单,其中,T&GT; ,该理由是适应未来发展和API简单。

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

面向未来

名单,其中,T&GT; 不是设计成通过继承它容易扩展;它被设计成快于内部实现。你会发现这些方法就可以了不是虚拟,因此不能被覆盖,并且没有钩到它的添加 / 插入 / 删除操作。

List<T> is not designed to be easily extensible by subclassing it; it is designed to be fast for internal implementations. You'll notice the methods on it are not virtual and so cannot be overridden, and there are no hooks into its Add/Insert/Remove operations.

这意味着,如果你需要改变的集合,在未来的行为(例如拒绝空的对象,人们尝试添加,或执行发生这种情况时,如更新类状态的其他工作),那么你需要改变集合返回一个类型,那么可以继承,这将是一个破接口的改变(当然改变的东西就像不允许空也可能是接口的改变的语义,但像更新内部类的状态不会)。

This means that if you need to alter the behaviour 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).

因此​​,通过返回一个类可以很容易地创建子类,例如收藏&LT; T&GT; 或如的IList℃的接口; T&GT; 的ICollection&LT; T&GT; 的IEnumerable&LT; T&GT; 你可以改变你的内部实现是一个不同的集合类型,以满足您的需求,而不会破坏消费者的code,因为它仍然可以返回他们期待的类型。

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简单

名单,其中,T&GT; 包含了很多有用的操作,例如二分查找排序等。但是,如果这是你露出那么很可能你控制列表的语义,而不是消费者的集合。所以,当你的类在内部可能需要这些操作是不太可能,消费者类的会想(甚至应该)给他们打电话。

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;与列表&LT; T&GT;你应该在你的接口使用什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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