472,983 Members | 2,636 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,983 software developers and data experts.

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!
Nov 17 '05 #1
2 8651
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.GetEnumerator method. The second implements a named iterator
MyCollection.BuildMyCollection. 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.Collections.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.WriteLine("Values in the collection are:");

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

// Use iterator 2.
foreach (int i in col.BuildMyCollection())
{
Console.WriteLine(i);
}

// Use property iterator
foreach (int i in col.AllItems)
{
Console.WriteLine(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.GetEnumerator 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> BuildMyCollection()
{
for (int i = 0; i < items.Length; i++)
{
if (i >= 2)
{
//yield break;
break;
}
yield return items[i];
}
}
}
}

"Nicholas Paldino [.NET/C# MVP]" wrote:
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]
- mv*@spam.guard.caspershouse.com

"yyhhjj" <yy****@discussions.microsoft.com> wrote in message
news:FA**********************************@microsof t.com...
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!


Nov 17 '05 #2
To understand the difference between yield break and break try the
following iterator with both yield break and break:

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

Console.WriteLine("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
Nov 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Ken Varn | last post by:
Sometimes when I try to close my managed C++ application, the following dialog displays in Win 2000 Pro: The title of the dialog is "Server Busy". The message is "This action cannot be completed...
5
by: Jarek | last post by:
Hi all! I'm optimizing my C++ multi-threaded application (linux). My application consumes huge amout of memory from unknown reason. There are no memory leaks, or other allocation bugs,...
1
by: blue | last post by:
Looking at code from the following: http://www.gamedev.net/reference/programming/features/enginuity2/page3.asp I'm confused though... CMMPointer(T *o) { obj=0; *this=0;
5
by: junky_fellow | last post by:
Each time i submit some pattern to "google", it shows search took 0.XX seconds for exploring millions of web pages. When i look for efficient ways of searching a string, they always say compare...
3
by: Fei Li | last post by:
Hi, take string class as an example, who can explain the difference? Thanks
2
by: Lasse Edsvik | last post by:
Hello I was wondering if you guys could tell me if: cmd.ExecuteReader(CommandBehavior.CloseConnection); returns a dataset, datatable or what?
6
by: John Pass | last post by:
What is the difference between a While and Do While/Loop repetition structure. If they is no difference (as it seems) why do both exist?
9
by: Dullme | last post by:
i can hardly know what is the code for this. I have researched palindromes, but unfortunately, I only have read about integers not for characters..I need some help..
11
by: O.B. | last post by:
Does C# support anything like PHP's break command that optionally accepts a parameter specifying how many loops to break out of?
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.