none
請問 Array 為何繼承IEnumerable<T> ? RRS feed

  • 問題

  • 各前前輩們:

    我看.net的Library的繼承樹,Array 只implement
     ICloneable, IList, ICollection, IEnumerable 這些類別
    但是當我執行下面程式碼時

     string[] stringArray = {"Morrowind", "BioShock",
            "Half Life 2: Episode 1", "The Darkness",
            "Daxter", "System Shock 2"};

    if (stringArray is IEnumerable<String>) {

    Console.WriteLine("stringArray is IEnumerable<String>);

    } else {

     Console.WriteLine("stringArray is not IEnumerable<String>);

    }


    答案竟然是:stringArray is IEnumerable<String>

    很顯然Array type 是 implemnet IEnumerable< T >,但看Library又沒有implemnet

    實在太奇怪了,煩請前輩們解答一下!


    謝謝!

    2009年3月25日 下午 06:32

解答

  • HelloJackTom 你好


    在LIBRARY 裡有說在.NET FRAMEWORK 2.0 時ARRAY CLASS IMPLEMENTS 了  System.Collections.Generic..::.IList<(Of <(T>)>), System.Collections.Generic..::.ICollection<(Of <(T>)>), and System.Collections.Generic..::.IEnumerable<(Of <(T>)>)
    generic interface
    所以你的RESULT 便是 stringArray is IEnumerable<String>

    czz5hkty.alert_caution(en-us,VS.90).gifImportant Note:

    In the .NET Framework version 2.0, the Array class implements the System.Collections.Generic..::.IList<(Of <(T>)>), System.Collections.Generic..::.ICollection<(Of <(T>)>), and System.Collections.Generic..::.IEnumerable<(Of <(T>)>) generic interfaces. The implementations are provided to arrays at run time, and therefore are not visible to the documentation build tools. As a result, the generic interfaces do not appear in the declaration syntax for the Array class, and there are no reference topics for interface members that are accessible only by casting an array to the generic interface type (explicit interface implementations). The key thing to be aware of when you cast an array to one of these interfaces is that members which add, insert, or remove elements throw NotSupportedException.



    For more information visit the following URL
    http://msdn.microsoft.com/en-us/library/system.array.aspx

    Please forgive me if my concept is wrong

    Hope this can help.

    Chi
    2009年3月25日 下午 09:32

所有回覆

  • HelloJackTom 你好


    在LIBRARY 裡有說在.NET FRAMEWORK 2.0 時ARRAY CLASS IMPLEMENTS 了  System.Collections.Generic..::.IList<(Of <(T>)>), System.Collections.Generic..::.ICollection<(Of <(T>)>), and System.Collections.Generic..::.IEnumerable<(Of <(T>)>)
    generic interface
    所以你的RESULT 便是 stringArray is IEnumerable<String>

    czz5hkty.alert_caution(en-us,VS.90).gifImportant Note:

    In the .NET Framework version 2.0, the Array class implements the System.Collections.Generic..::.IList<(Of <(T>)>), System.Collections.Generic..::.ICollection<(Of <(T>)>), and System.Collections.Generic..::.IEnumerable<(Of <(T>)>) generic interfaces. The implementations are provided to arrays at run time, and therefore are not visible to the documentation build tools. As a result, the generic interfaces do not appear in the declaration syntax for the Array class, and there are no reference topics for interface members that are accessible only by casting an array to the generic interface type (explicit interface implementations). The key thing to be aware of when you cast an array to one of these interfaces is that members which add, insert, or remove elements throw NotSupportedException.



    For more information visit the following URL
    http://msdn.microsoft.com/en-us/library/system.array.aspx

    Please forgive me if my concept is wrong

    Hope this can help.

    Chi
    2009年3月25日 下午 09:32
  •  謝謝大大的回答,原來array是在runtime時候implement Generic Interface,這樣就比較合理了,

    感恩!
    2009年3月26日 上午 09:25