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. Other Discussions
  3. Clever Code
  4. C# Yield issue

C# Yield issue

Scheduled Pinned Locked Moved Clever Code
csharphelpasp-netcomdesign
1 Posts 1 Posters 3 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.
  • R Offline
    R Offline
    Rama Krishna Vavilala
    wrote on last edited by
    #1

    Stumbled across this one when implementing a DataSourceControl for ASP.NET. Here is a simplified code, for a practical case where this bug may occur see ExecuteSelect[^] method.

    public class EnumeratorSpec
    {
    public int MaxRecords;
    };

    class Program
    {
    public static IEnumerable GetEnumerableAndCount(EnumeratorSpec spec)
    {
    spec.MaxRecords = 20;

        for (int i = 0; i < 20; i++)
        {
            yield return i + 1;
        }
    }
    
    static void Main(string\[\] args)
    {
        EnumeratorSpec spec = new EnumeratorSpec();
        IEnumerable results = GetEnumerableAndCount(spec);
    
        Console.WriteLine("Results {0}", spec.MaxRecords);
    
        if (spec.MaxRecords != 0)
        {
            foreach (int result in results)
            {
                Console.WriteLine(result);
            }
        }
    }
    

    }


    Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. -Brian Kernighan

    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