Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. yield return

yield return

Scheduled Pinned Locked Moved ASP.NET
question
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    vishwjeet
    wrote on last edited by
    #1

    Hi, I am using a enumerable function using "yield return". This allows me to iterate through the items using for each loop. Since this is a lazy evaluation, after each iteration the control returns to the calling function. How can I use the enumerable function without for each loop? public static IEnumerable GetValuesUpto(int n) { for(int ctr = 1;ctr<=n;ctr++) { yield return ctr; } } So, when I call this function i.e. int[] arr = GetValuesUpto(10); What would be the result, will it work like a lazy evaluation and will return only the first value, or will it return the desired result? Waiting for your replies guys!! Vishwjeet

    A 1 Reply Last reply
    0
    • V vishwjeet

      Hi, I am using a enumerable function using "yield return". This allows me to iterate through the items using for each loop. Since this is a lazy evaluation, after each iteration the control returns to the calling function. How can I use the enumerable function without for each loop? public static IEnumerable GetValuesUpto(int n) { for(int ctr = 1;ctr<=n;ctr++) { yield return ctr; } } So, when I call this function i.e. int[] arr = GetValuesUpto(10); What would be the result, will it work like a lazy evaluation and will return only the first value, or will it return the desired result? Waiting for your replies guys!! Vishwjeet

      A Offline
      A Offline
      Abhishek Sur
      wrote on last edited by
      #2

      Iterator blocks are called when you want to fetch the data from the Enumerable. Actually if you call the the function, it will not be called. Rather if you do a foreach on the Enumerable object the function will be called and will return you 10. ;) Just change your code a bit as I did:

      IEnumerable arr = Documented.GetValuesUpto(10);
      MessageBox.Show(arr.ToList().Count.ToString());

      public static IEnumerable GetValuesUpto(int n)
      {

      for (int ctr = 1; ctr <= n; ctr++)
      {
      
         yield return ctr;
      }
      

      }

      Put a breakpoint in the line IEnumerable arr = Documented.GetValuesUpto(10);. and also a breakpoint within GetValueUpto block. You will see the function is called when we try to call ToList() rather than the actual call to GetValueUpto. This is the main advantage of iterator blocks. :rose::rose:

      Abhishek Sur


      My Latest Articles **Create CLR objects in SQL Server 2005 C# Uncommon Keywords Read/Write Excel using OleDB

      **Don't forget to click "Good Answer" if you like t

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups