Re: IEnumerable interface implemented by System.Array

From: Peter van der Goes (p_vandergoes_at_toadstool.u)
Date: 11/16/04


Date: Tue, 16 Nov 2004 11:35:57 -0600


<jamal@docUSA.com> wrote in message
news:%23fj9cd$yEHA.1260@TK2MSFTNGP12.phx.gbl...
> In the design of .NET Framework Class Library, the IEnumerable is used to
> iterate through elements of collections. That iteration is not necessarily
> sequential and also for some special classes sequential iteration does not
> make sense.
> System.Array implements IEnumerable interface and in most cases it is
useful
> to access it's elements sequentially. I'd like to use IEnumerable
interface
> (using foreach keyword in most cases) in order to iterate but I did not
find
> it documented that System.Array's implementation of that interface
performs
> sequential iteration.
> .NET Framework v1.1 does it sequentially but I am looking for this to be
> documented; because I feel it that microsoft rarely violates the
> documentation of older versions.
>
> I like my codes to run in future versions of the product. So I do not use
> foreach on arrays where sequential iteration is required only if I find an
> explicit documentation of the issue.
>
> Anybody could help?
>
Is this what you are looking for?
<quote>
A type that implements IEnumerable is also a collection type, even if it
does not satisfy the conditions above. (This is possible if it implements
some of the IEnumerable members by means of explicit interface member
implementation, as described in Section 13.4.1.)

The System.Array type (Section 12.1.1) is a collection type, and since all
array types derive from System.Array, any array type expression is permitted
in a foreach statement. The order in which foreach traverses the elements of
an array is as follows: For single-dimensional arrays, elements are
traversed in increasing index order, starting with index 0 and ending with
index Length - 1.

<unquote>

Found in:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csspec/html/vclrfcsharpspec_8_8_4.asp

-- 
Peter [MVP Visual Developer]
Jack of all trades, master of none.


Relevant Pages

  • Re: Readline using foreach and while
    ... condition is checked at the start of each iteration, if the array ... map doesn't recheck the count ... the passed list for each iteration. ... @ary, which comes from a scope outside this sub, now contains (h, a, b, ...
    (comp.lang.perl.misc)
  • Re: Memory Leak Problem
    ... And each iteration I generated about 700000 random numbers. ... The variable that contains these random numbers is a pointer, ... compiler which does not allow REAL valued array indexes. ... to use rout, which has a pointer attribute, as an argument and then ...
    (comp.lang.fortran)
  • Re: Filter multi-dimensional array
    ... would fail to find all elements, but that the iteration order ... You have just become the first person to mention equivalence in this ... you can be sure that the array passed to your function contains ... the users of third-party libraries know that they are ...
    (comp.lang.javascript)
  • Re: can this code be improved
    ... Fisher-Yates shuffle. ... swapped with a randomly selected element between i and the end of the array. ... At the end of every iteration, ... @param count ...
    (comp.lang.java.programmer)
  • Loops performance contradictions.
    ... happen on each iteration (unless it will change during the loop). ... Creating an array of length 0x3B9ACA0. ... Set the array data in about 3438380 ticks. ...
    (microsoft.public.dotnet.framework.performance)

Loading