What's the Difference between 'yield break' and 'break"?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • yyhhjj

    What's the Difference between 'yield break' and 'break"?

    I created a test program to implement an iterator.
    First, I used 'yield break' in the iterator, it worked normally.
    Then, I simply used 'break' in the places of 'yield break', it still worked
    normally.
    What's the difference between 'yield break' and 'break' here?
    Thanks!
  • yyhhjj

    #2
    Re: What's the Difference between 'yield break' and 'break"?

    Hi, Nicholas,

    Thank you for your reply. The follwoing is my source code.
    In the source code, I implemented 3 iterators. The first implements
    IEnumerable.Get Enumerator method. The second implements a named iterator
    MyCollection.Bu ildMyCollection . The third implements an iterator in a
    property.

    In the first 2 iterators, I tried to use 'yield break' and 'break'
    respectively. They seems to have the same effect.

    using System;
    using System.Collecti ons.Generic;
    using System.Text;

    namespace TestIterator
    {
    class Program
    {
    static void Main(string[] args)
    {
    TestIterator();
    }

    private static void TestIterator()
    {
    MyCollection col = new MyCollection();

    // Display the collection items:
    Console.WriteLi ne("Values in the collection are:");

    // Use iterator 1.
    foreach (int i in col)
    {
    Console.WriteLi ne(i);
    }

    // Use iterator 2.
    foreach (int i in col.BuildMyColl ection())
    {
    Console.WriteLi ne(i);
    }

    // Use property iterator
    foreach (int i in col.AllItems)
    {
    Console.WriteLi ne(i);
    }
    }
    }

    // Declare the collection:
    public class MyCollection
    {
    public int[] items;

    // Implement an iterator as a property.
    // It can only be the named iterator.
    public IEnumerable<int > AllItems
    {
    get
    {
    foreach (int i in items)
    {
    yield return i;
    }
    }
    }


    public MyCollection()
    {
    items = new int[5] { 5, 4, 7, 9, 3 };
    }

    // Iterator 1: Implement IEnumerable<T>. GetEnumerator method
    // or IEnumerable.Get Enumerator method.
    public IEnumerator<int > GetEnumerator()
    {
    for (int i = 0; i < items.Length; i++)
    {
    if (i >= 2)
    {
    //yield break;
    break;
    }
    yield return items[i];
    }
    }

    // Iterator 2: Named method
    // Implement a method which returns IEnumerable<T> or IEnumberable.
    public IEnumerable<int > BuildMyCollecti on()
    {
    for (int i = 0; i < items.Length; i++)
    {
    if (i >= 2)
    {
    //yield break;
    break;
    }
    yield return items[i];
    }
    }
    }
    }



    "Nicholas Paldino [.NET/C# MVP]" wrote:
    [color=blue]
    > Can you show your code?
    >
    > Chances are that your break statement is producing the end of the
    > production at the same time (and therefore, returning nothing).
    >
    > Without seeing your code, it's hard to say though.
    >
    >
    > --
    > - Nicholas Paldino [.NET/C# MVP]
    > - mvp@spam.guard. caspershouse.co m
    >
    > "yyhhjj" <yyhhjj@discuss ions.microsoft. com> wrote in message
    > news:FA31FBBA-5D51-4D2D-A969-E8ADD9B9D215@mi crosoft.com...[color=green]
    > >I created a test program to implement an iterator.
    > > First, I used 'yield break' in the iterator, it worked normally.
    > > Then, I simply used 'break' in the places of 'yield break', it still
    > > worked
    > > normally.
    > > What's the difference between 'yield break' and 'break' here?
    > > Thanks![/color]
    >
    >
    >[/color]

    Comment

    • Marcus Andrén

      #3
      Re: What's the Difference between 'yield break' and 'break&quot;?

      To understand the difference between yield break and break try the
      following iterator with both yield break and break:

      public IEnumerable<int > BuildMyCollecti on()
      {
      for (int i = 0; i < items.Length; i++)
      {
      if (i >= 2)
      {
      //yield break;
      break;
      }
      yield return items[i];
      }

      Console.WriteLi ne("You used break.");

      yield return 10;
      }

      Only if you use break will you get the console message and have 10
      added as the last item of the iterator.

      break only exits a for loop. yield break terminates the iterator, much
      like return terminates an ordinary method.

      In your simple iterators they did the same thing since you didn't
      yield any more items after the for loop.

      --
      Marcus Andrén


      Comment

      Working...