Special form of return statement can only be used

Info icon This preview shows pages 283–285. Sign up to view the full content.

View Full Document Right Arrow Icon
special form of return statement can only be used inside a method that returns either an enumerable or an enumerator object—you’ll get a compiler error if you try to use it anywhere else. It works rather differently from a normal return. A normal return state- ment indicates that the method has finished, and would like to return control to the caller (returning a value, if the method’s return type was not void ). But yield return effectively says: “I want to return this value as an item in the collection, but I might not be done yet—I could have more values to return.” The yield return in Example 7-32 is in the middle of a foreach loop. Whereas a normal return would break out of the loop, in this case the loop is still running, even though the method has returned a value. This leads to some slightly surprising flow of execu- tion. Let’s look at the order in which this code runs. Example 7-33 modifies the AddNumbers method from Example 7-32 by adding a few calls to Console.Writeline , so we can see exactly how the code runs. It also includes a Main method with a foreach loop iterating over the collection returned by AddNumbers , again with some Con sole.WriteLine calls to keep track of what’s going on. Example 7-33. Exploring yield return class Program { static IEnumerable<string> AddNumbers(IEnumerable<string> names) { Console.WriteLine("Starting AddNumbers"); int i = 0; foreach (string currentName in names) { Console.WriteLine("In AddNumbers: " + currentName); yield return string.Format("{0}: {1}", i, currentName); i += 1; } Console.WriteLine("Leaving AddNumbers"); } static void Main(string[] args) { string[] eventNames = { "Swing Dancing at the South Bank", "Saturday Night Swing", "Formula 1 German Grand Prix", "Swing Dance Picnic", "Stompin' at the 100 Club" }; Console.WriteLine("Calling AddNumbers"); IEnumerable<string> numberedNames = AddNumbers(eventNames); Console.WriteLine("Starting main loop"); foreach (string numberedName in numberedNames) { Console.WriteLine("In main loop: " + numberedName); Collections and Polymorphism | 259
Image of page 283

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
} Console.WriteLine("Leaving main loop"); } } Here’s the output: Calling AddNumbers Starting main loop Starting AddNumbers In AddNumbers: Swing Dancing at the South Bank In main loop: 0: Swing Dancing at the South Bank In AddNumbers: Saturday Night Swing In main loop: 1: Saturday Night Swing In AddNumbers: Formula 1 German Grand Prix In main loop: 2: Formula 1 German Grand Prix In AddNumbers: Swing Dance Picnic In main loop: 3: Swing Dance Picnic In AddNumbers: Stompin' at the 100 Club In main loop: 4: Stompin' at the 100 Club Leaving AddNumbers Leaving main loop Even though the main method calls AddNumbers only once, before the start of the loop, you can see from the output that the code flits back and forth between the main loop and AddNumbers for each item in the list.
Image of page 284
Image of page 285
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern