473,396 Members | 1,917 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 8707
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.