为什么IList的< T>实现IEnumerable< T>和ICollection的< T>而ICollection的< T>本身实现IEnumerable< T> [英] Why does IList<T> implement IEnumerable<T> and ICollection<T> while ICollection<T> itself implements IEnumerable<T>

查看:236
本文介绍了为什么IList的< T>实现IEnumerable< T>和ICollection的< T>而ICollection的< T>本身实现IEnumerable< T>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么IList的定义是这样?

Why is IList defined like this?

public interface IList<T> : ICollection<T>, IEnumerable<T>, IEnumerable

public interface ICollection<T> : IEnumerable<T>, IEnumerable

public interface IEnumerable<T> : IEnumerable

难道不是只是

Couldn't it just be

public interface IList<T> : ICollection<T>

所以,测试我创造了这些接口,只是要确定其是否正常工作!

So, to test I created these interfaces, just to be sure if that works!

public interface IOne
{
    string One();
}

public interface ITwo : IOne
{
    string Two();
}

public interface IThree : ITwo, IOne
{
    string Three();
}

而其完美的罚款,ReSharper的抱怨冗余接口。

While its perfectly fine, Resharper complains about "Redundant interfaces".

为什么微软继续着这种实现任何想法?

Any ideas why Microsoft went on with this implementation?

推荐答案

接口继承是软件工程中最误导性词语之一。你没有继承下蹲,接口没有任何实现,所以你不能继承它的。你只继承的需求的实施方法。

Interface "inheritance" is one of the most misleading terms in software engineering. You didn't inherit squat, interfaces don't have any implementation so you can't inherit it either. You only inherit the demand to implement the methods.

添加到这种需求通过重复的接口声明不会改变任何东西,你已经有了需求,增加一个额外的需求使得任何差别。如此以来,没关系,微软有益只是重复的界面让你可以在一个告诉一举什么接口,比方说,清单实施。你不必深入到接口声明的看到列表实现IEnumerable为好。这是一个自文档的编码风格,推荐。

Adding to that demand by repeating the interface declaration doesn't change anything, you already had the demand and adding an extra demand makes no difference whatsoever. So since it doesn't matter, Microsoft helpfully just repeats the interface so you can tell in one fell swoop what interfaces are implemented by, say, List. You don't have to drill down to the interface declaration to see that List implements IEnumerable as well. It is a self-documenting coding style, recommended.

千万要小心这个奖牌的另一边,用完全相同的方法,两个不同的接口可以只是一个的的实施办法执行。虽然这是非常有用的,有时这是不是你想要的。再说了,ICowboy和IPainter,他们都有一个draw()方法。它不应该做同样的事情:)然后,您可以回落到一个明确的执行,以避免歧义。

Do beware of the other side of this medal, two distinct interfaces with the exact same method can be implemented with just a single method implementation. While that is often useful, sometimes that is not what you want. Say, ICowboy and IPainter, they both have a Draw() method. It should not do the same thing :) You then have to fall back to an explicit implementation to avoid the ambiguity.

解决了ReSharper的抱怨,这是不是非常有帮助的课程。 ReSharper的倾向于认为,从一个程序员最糟糕的。但是如果你想关闭它,那么你需要从IThree继承列表中删除IONE,它是多余的。同样的事情实现IThree类,你也想需要从继承列表中删除ITwo的和卓智。或者只是关闭警告。

Addressing the Resharper complaint, it isn't very helpful of course. Resharper tends to assume the worst from a programmer. But if you want to shut it up then you need to remove IOne from the IThree inheritance list, it is redundant. Same thing for the class that implements IThree, you'd also need to remove ITwo and IOne from the inheritance list. Or just turn off the warning.

这篇关于为什么IList的&LT; T&GT;实现IEnumerable&LT; T&GT;和ICollection的&LT; T&GT;而ICollection的&LT; T&GT;本身实现IEnumerable&LT; T&GT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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