2

I know that the base abstract Array class doesn't implement generic IEnumerable as

public abstract class Array : ICloneable, IList, ICollection, IEnumerable, IStructuralComparable, IStructuralEquatable
{
 ...
}

so when I create a derive array class like int[] i; or string[] s;, do they implement IEnumerable<T>? And how can I see the source code of []?

René Vogt
  • 43,056
  • 14
  • 77
  • 99

3 Answers3

3

Official word here

Array Overview

An array has the following properties:

  • An array can be Single-Dimensional, Multidimensional or Jagged.

  • The number of dimensions and the length of each dimension are established when the array instance is created. These values can't be changed during the lifetime of the instance.

  • The default values of numeric array elements are set to zero, and reference elements are set to null.

  • A jagged array is an array of arrays, and therefore its elements are reference types and are initialized to null.

  • Arrays are zero indexed: an array with n elements is indexed from 0 to n-1.

  • Array elements can be of any type, including an array type.

  • 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#.

TheGeneral
  • 79,002
  • 9
  • 103
  • 141
  • This is a mighty fine summary of `Array`, but it doesn't answer the question "And how can I see the source code of `[]`?" – Enigmativity Jun 06 '19 at 06:50
  • @TheGeneral is it the reason we can use extension methods in `System.Linq.Enumerable` because the derived array types do implement IEnumerable despite the fact that base Array class doesn't? –  Jun 06 '19 at 06:51
  • @Enigmativity and there be the fly in the anointment (again). technically there isn't really its compiled – TheGeneral Jun 06 '19 at 06:52
3

There is special type SZArrayHelper - wrapper around array. Here is source code: https://referencesource.microsoft.com/#mscorlib/system/array.cs,aa97964558672440

Also, array implements IList, and it implements ICollection and it implements IEnumerable. So, Array implements IEnumerable

Backs
  • 24,430
  • 5
  • 58
  • 85
  • 1
    You got to love the constructor `Contract.Assert(false, "Hey! How'd I get here?");` someone at Microsoft has a sense of humour, also plus 1 – TheGeneral Jun 06 '19 at 06:46
  • @TheGeneral i've found it after investigating another problem: http://blog.rogatnev.net/2017/07/14/IndexOf-with-IEquatable.html – Backs Jun 06 '19 at 07:56
2

You can check it simply like this:

var type = typeof(int[]); // or any other type
foreach (var @interface in type.GetInterfaces())
    Console.WriteLine(@interface);

Result is:

System.ICloneable
System.Collections.IList
System.Collections.ICollection
System.Collections.IEnumerable
System.Collections.IStructuralComparable
System.Collections.IStructuralEquatable
System.Collections.Generic.IList`1[System.Int32]
System.Collections.Generic.ICollection`1[System.Int32]
System.Collections.Generic.IEnumerable`1[System.Int32]
System.Collections.Generic.IReadOnlyList`1[System.Int32]
System.Collections.Generic.IReadOnlyCollection`1[System.Int32]
CSDev
  • 3,177
  • 6
  • 19
  • 37