0

I recently answered this question on Code Review: Exists Method Implementation for Multidimensional Array in C#. The question is about determining whether an array of arbitrary dimension contains a specific element.

Based on this statement found in Arrays (C# Programming Guide) / Array overview:

Array types are reference types derived from the abstract base type Array. Since this type implements IEnumerable and IEnumerable<T>, you can use foreach iteration on all arrays in C#.

and given the declaration int[,,,] array4, I first gave the answer:

bool result = array4.Any(x => x == 1);

However, according to a comment of @JimmyHu, the compiler generates a:

Error CS1061 'int[,,,]' does not contain a definition for 'Any' and no accessible extension method 'Any' accepting a first argument of type 'int[,,,]' could be found (are you missing a using directive or an assembly reference?)

Apparently, the correct answer must be:

bool result = array4.Cast<int>().Any(x => x == 1);

This other attempt

bool result2 = ((IEnumerable<int>)array4).Any(x => x == 1);

does not work either and yields:

Error CS0030 Cannot convert type 'int[,,,]' to 'System.Collections.Generic.IEnumerable<T>'

Question: is the C# Programming Guide wrong on this point or I am missing something. Do arrays derive from Array which implement the generic interface or not?

Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
  • 1
    Does this answer your question? [Why do C# multidimensional arrays not implement IEnumerable?](https://stackoverflow.com/questions/275073/why-do-c-sharp-multidimensional-arrays-not-implement-ienumerablet) – Franz Gleichmann Mar 19 '21 at 15:05
  • 1
    Does this answer the question about the programming guide? Even Jon Skeet says: *" it seems pretty weird to me"*. – Olivier Jacot-Descombes Mar 19 '21 at 15:07
  • 1
    The guide is an over-simplification -- only single-dimensional arrays implement `IEnumerable` – canton7 Mar 19 '21 at 15:08
  • 1
    it kinda does answer it. the guide claims _all_ arrays implement IEnumerable _and_ IEnumerable, whereas the answer says _multidimensional_arrays only implement IEnumerable. so yes, technically the guide is wrong - or at least imprecise. – Franz Gleichmann Mar 19 '21 at 15:17
  • 1
    It looks like the [bottom few comments of this issue](https://github.com/dotnet/docs/issues/13010) discuss this – canton7 Mar 19 '21 at 15:18
  • 2
    I posted this as a new issue on github: [Text unclear about arrays implementing IEnumerable #23399](https://github.com/dotnet/docs/issues/23399). – Olivier Jacot-Descombes Mar 19 '21 at 15:24
  • 1
    I think that's unnecessary -- it was already mentioned in the issue I linked, and I added a comment linking to your SO post – canton7 Mar 19 '21 at 15:25
  • 1
    They're actually both wrong, even a single-dimension vector array does not implement it. It is implemented for 1-dim arrays by a class called `SZArrayHelper`, which is magically hacked into the array's method table. See https://referencesource.microsoft.com/mscorlib/R/aa97964558672440.html also see https://stackoverflow.com/questions/2773740/why-do-arrays-in-net-only-implement-ienumerable-and-not-ienumerablet So it *appears* that it can be cast to `IEnumerable` but in fact it is SZArrayHelper implementing it. – Charlieface Mar 19 '21 at 15:51
  • The [Array overview](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/arrays/#array-overview) of the docs has been [fixed](https://github.com/dotnet/docs/issues/23399) back in April 2021. – Olivier Jacot-Descombes Feb 16 '23 at 14:04

0 Answers0