Multidimensional arrays do not implement IEnumerable, or do they?

后端 未结 4 1360
广开言路
广开言路 2021-01-13 14:55

For the reasons that I still do not understand (see this SO question) multidimensional arrays in CLR do not implement IEnumerable. So the following doe

4条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-13 15:32

    The MSDN page for Array includes this:

    Important Note: In the .NET Framework version 2.0, the Array class implements the System.Collections.Generic.IList, System.Collections.Generic.ICollection, and System.Collections.Generic.IEnumerable generic interfaces. The implementations are provided to arrays at run time,

    Note the final words in the quote... it appears this generation does not happen for multi-dimensional arrays (so a documentation bug).

    But as others have noted, what would T be? A good case can be made for T[] (or, these days with LINQ, IEnumerable).

    In the end, if you want to iterate all the array's members just stick with IEnumerable and Cast extension. Otherwise easy to write your own.

提交回复
热议问题