Yield break
-
Hi all, I was learning
yield
operator. And made a function like this.public IEnumerable<Guid> GetIds(int count, DateTime time)
{Console.WriteLine("Start"); { if (DateTime.Now > time) yield break; else yield return Guid.NewGuid(); } Console.WriteLine("End"); }
But End never get printed.Can anyone tell why? Because after break statement it should get out of the loop and print it.
Cheers!! Brij My Blog:http://brijbhushan.wordpress.com
Check my latest Article :ViewState - Various ways to reduce performance overheadFrom the relevant MSDN page for
break
:The break statement terminates the closest enclosing loop or switch statement in which it appears. Control is passed to the statement that follows the terminated statement, if any.
So
break
breaks out ofloops
andswitch
statements. I am guessing that in this case it breaks out of your method. Same foryield
:Used in an iterator block to provide a value to the enumerator object or to signal the end of iteration.
Once again yield is meant to be used in an iteration.
if
is not strictly speaking an iteration, although I suspect that yourelse
block would run. The pages I looked at did not say what these statements do when not used in iterations.Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”
-
Hi all, I was learning
yield
operator. And made a function like this.public IEnumerable<Guid> GetIds(int count, DateTime time)
{Console.WriteLine("Start"); { if (DateTime.Now > time) yield break; else yield return Guid.NewGuid(); } Console.WriteLine("End"); }
But End never get printed.Can anyone tell why? Because after break statement it should get out of the loop and print it.
Cheers!! Brij My Blog:http://brijbhushan.wordpress.com
Check my latest Article :ViewState - Various ways to reduce performance overheadBecause you terminate the method before you get there.
-
Because you terminate the method before you get there.
Thanks to all for answers. I got the point. Whenever we write
yield
it returns IEnumerable to the caller.And once yield statement get executed, no statements get executed. Thanks again..Cheers!! Brij My Blog:http://brijbhushan.wordpress.com
Check my latest Article :ViewState - Various ways to reduce performance overhead -
Hi all, I was learning
yield
operator. And made a function like this.public IEnumerable<Guid> GetIds(int count, DateTime time)
{Console.WriteLine("Start"); { if (DateTime.Now > time) yield break; else yield return Guid.NewGuid(); } Console.WriteLine("End"); }
But End never get printed.Can anyone tell why? Because after break statement it should get out of the loop and print it.
Cheers!! Brij My Blog:http://brijbhushan.wordpress.com
Check my latest Article :ViewState - Various ways to reduce performance overheadOne thing - when you built the code, did it not warn you that an unreachable condition had been detected? It's important to pay attention to warnings as well.
I have CDO, it's OCD with the letters in the right order; just as they ruddy well should be
Forgive your enemies - it messes with their heads
-
One thing - when you built the code, did it not warn you that an unreachable condition had been detected? It's important to pay attention to warnings as well.
I have CDO, it's OCD with the letters in the right order; just as they ruddy well should be
Forgive your enemies - it messes with their heads
Pete O'Hanlon wrote:
did it not warn you that an unreachable condition had been detected?
I am using VS2008 and there was no warning.
Cheers!! Brij My Blog:http://brijbhushan.wordpress.com
Check my latest Article :ViewState - Various ways to reduce performance overhead -
Pete O'Hanlon wrote:
did it not warn you that an unreachable condition had been detected?
I am using VS2008 and there was no warning.
Cheers!! Brij My Blog:http://brijbhushan.wordpress.com
Check my latest Article :ViewState - Various ways to reduce performance overheadI find that surprising. Certainly, in VS2010 there is a warning - check to make sure that you haven't deselected the warnings checkbox.
I have CDO, it's OCD with the letters in the right order; just as they ruddy well should be
Forgive your enemies - it messes with their heads
-
I find that surprising. Certainly, in VS2010 there is a warning - check to make sure that you haven't deselected the warnings checkbox.
I have CDO, it's OCD with the letters in the right order; just as they ruddy well should be
Forgive your enemies - it messes with their heads
Pete O'Hanlon wrote:
Certainly, in VS2010 there is a warning
If I had had VS2010, I would've checked in VS2010. But in VS 2008, there is no warning for this.I got warning for something else(just to check, whether it is showing warning or not) And if it is showing warning in VS2010, then might be a Bug in VS2008 :)
Cheers!! Brij My Blog:http://brijbhushan.wordpress.com
Check my latest Article :ViewState - Various ways to reduce performance overhead -
I find that surprising. Certainly, in VS2010 there is a warning - check to make sure that you haven't deselected the warnings checkbox.
I have CDO, it's OCD with the letters in the right order; just as they ruddy well should be
Forgive your enemies - it messes with their heads
No warning in VS2008 Express Edition (warning level = 4). :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.
-
No warning in VS2008 Express Edition (warning level = 4). :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.
It seems to be a problem with it being in the if block - put it outside and it warns, inside and it doesn't - but it should.
I have CDO, it's OCD with the letters in the right order; just as they ruddy well should be
Forgive your enemies - it messes with their heads
-
It seems to be a problem with it being in the if block - put it outside and it warns, inside and it doesn't - but it should.
I have CDO, it's OCD with the letters in the right order; just as they ruddy well should be
Forgive your enemies - it messes with their heads
I agree, however I'm not surprised they did get it wrong at first:
yield return
statements are not really breaking program flow like regularreturn
s would (they somewhat resemble a UNIX fork), howeveryield break
does break the flow. :)Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.
-
I agree, however I'm not surprised they did get it wrong at first:
yield return
statements are not really breaking program flow like regularreturn
s would (they somewhat resemble a UNIX fork), howeveryield break
does break the flow. :)Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.
Indeed, which is why I'd have expected yield break to throw a warning.
I have CDO, it's OCD with the letters in the right order; just as they ruddy well should be
Forgive your enemies - it messes with their heads