快捷搜索:  as  test  1111  test aNd 8=8  test++aNd+8=8  as++aNd+8=8  as aNd 8=8

和记娱乐博娱188下载_集报网



某天,小新问我这样一个问题:

类System.Collections.CollectionBase是从IList、ICollection承袭而来,IList是从ICollection和IEnumerable承袭而来,那CollectionBase为什么还要从ICollection承袭呢?

我们先来看看这些类和接口在MSDN文档中的声明:

public interface IEnumerable

public interface ICollection : IEnumerable

public in和记娱乐博娱188下载terface IList : ICollection, IEnumerable

public abstract class Col和记娱乐博娱188下载lectionBase : IList, ICollection, IEnumerable

根据接口承袭的规则,我们知道CollectionBase只必要声明实现IList,就必须同时实现ICollection,也就必须实现IEnumerable,那么,我们为什么还要明确地把所有的这些接口都写下来呢?

换句话说,下面两种声明没有实质的差别:

// Code #1

public interface IEnumerable

public interface ICollection : IEnumerable

public interface IList : ICollection, IEnumerable

public class ArrayList : IList, ICollection, IEnumerable, ICloneable

// Code #2

p和记娱乐博娱188下载ublic interface IEnumerable

public interface ICollection : IEnumerable

publ和记娱乐博娱188下载ic interface IList : ICollection

public class ArrayList : IList, ICloneable

那为何MSDN要应用上面那种呢?我和小新评论争论后,同等觉得这样做仅仅为了前进代码的可读性。为了验证我们的设法主见,我分手发邮件给Eric Gunnerson(Eric是C# Compiler Team的成员)和Kit George(Kit是BCL Team的成员)扣问这个问题,他们的复书如下:

Allen,

I think that readability would be the primary reason people would do this.

Eric

Allen, what you’re seeing is simply a doc thing. ArrayList actually only implements IList directly, but we decided in V1.0 of the docs, to highlight the interface hierarchy so you didn’t have to wonder ‘what does IList implement’, you get to see it there on the type.

There is no benef和记娱乐博娱188下载it to ACTUALLY doing this. Try this code, and you’ll see it compiles:

using System;

using System.IO;

public class Test : IBob2

{

void IBob.M() {}

}

interface IBob

{

void M();

}

interface IBob2 : IBob {}

Regards,

Kit

本日,我查看微软的Rotor源代码,发明ArrayList的声明切实着实是Code #1的做法,不过Mono(ver. 1.1.2 Development Version)就采纳了Code #2的做法。

以是,今后假如你再碰着到这样的环境,你可以轻松的笑一声:“这样做是为了前进代码的可读性的!”

免责声明:以上内容源自网络,版权归原作者所有,如有侵犯您的原创版权请告知,我们将尽快删除相关内容。

您可能还会对下面的文章感兴趣: