[Mono-bugs] [Bug 415644] New: Enumerable.Take() invokes IEnumerator<T>.MoveNext() too many times.

bugzilla_noreply at novell.com bugzilla_noreply at novell.com
Thu Aug 7 22:19:56 EDT 2008


           Summary: Enumerable.Take() invokes IEnumerator<T>.MoveNext() too
                    many times.
           Product: Mono: Class Libraries
           Version: 1.2.6
          Platform: x86-64
        OS/Version: openSUSE 11.0
            Status: NEW
          Severity: Normal
          Priority: P5 - None
         Component: Sys.Core
        AssignedTo: jbevain at novell.com
        ReportedBy: jpryor at novell.com
         QAContact: mono-bugs at lists.ximian.com
          Found By: ---

Enumerable.Take() invokes IEnumerator<T>.MoveNext() too many times.

Consider the following program, which turns a Stream into an IEnumerable<byte>:

  using System;
  using System.Collections.Generic;
  using System.IO;
  using System.Linq;
  using System.Text;

  class Test {
    public static void Main (string[] args)
      MemoryStream ms = new MemoryStream (
          Encoding.UTF8.GetBytes ("Hello"));
      ms.Position = 0;
      foreach (byte b in AsEnumerable (ms).Take (2))
        Console.WriteLine ((char) b);
      Console.WriteLine ("Final Position: {0}", ms.Position);

    static IEnumerable<byte> AsEnumerable (Stream s)
      int n;
      while ((n = s.ReadByte()) >= 0)
        yield return (byte) n;

When run under Mono, the Final Position is 3 -- we've *skipped* a character.

Under .NET, the final position is 2 -- no character is skipped.

The problem is due to Enumerable.Take(), which always calls
IEnumerator<T>.MoveNext() count+1 times instead of just count times.

Configure bugmail: https://bugzilla.novell.com/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the QA contact for the bug.

More information about the mono-bugs mailing list