介面繼承的聲明問題 [C#, BCL]

來源:互聯網
上載者:User

介面繼承的聲明問題

 

Written by Allen Lee

 

某天,小新問我這樣一個問題:

類System.Collections.CollectionBase是從IList、ICollection繼承而來,IList是從ICollection和IEnumerable繼承而來,那CollectionBase為什麼還要從ICollection繼承呢?

我們先來看看這些類和介面在MSDN文檔中的聲明:

public interface IEnumerable

public interface ICollection : IEnumerable

public interface IList : ICollection, IEnumerable

public abstract class CollectionBase : 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

public interface IEnumerable

public interface ICollection : IEnumerable

public 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 benefit 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的做法。

所以,以後如果你再碰到到這樣的情況,你可以輕鬆的笑一聲:“這樣做是為了提高代碼的可讀性的!”

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.