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 operator with argument validation

C# yield operator with argument validation

Scheduled Pinned Locked Moved Clever Code
csharphtmlcomquestion
6 Posts 3 Posters 7 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.
  • J Offline
    J Offline
    Judah Gabriel Himango
    wrote on last edited by
    #1

    IEnumerable<int> GetEvenIntegers(IEnumerable<int> integers)
    {
    if(integers == null)
    {
    throw new ArgumentNullException("The integers parameter cannot be null.");
    }

    foreach(int i in integers)
    {
    if(i % 2 == 0)
    {
    yield return i;
    }
    }
    }

    // What exception does this throw?
    IEnumerable<int> evenNumbers = GetEvenIntegers(null);

    Tech, life, family, faith: Give me a visit. I'm currently blogging about: God-as-Judge, God-as-Forgiver The apostle Paul, modernly speaking: Epistles of Paul Judah Himango

    R 1 Reply Last reply
    0
    • J Judah Gabriel Himango

      IEnumerable<int> GetEvenIntegers(IEnumerable<int> integers)
      {
      if(integers == null)
      {
      throw new ArgumentNullException("The integers parameter cannot be null.");
      }

      foreach(int i in integers)
      {
      if(i % 2 == 0)
      {
      yield return i;
      }
      }
      }

      // What exception does this throw?
      IEnumerable<int> evenNumbers = GetEvenIntegers(null);

      Tech, life, family, faith: Give me a visit. I'm currently blogging about: God-as-Judge, God-as-Forgiver The apostle Paul, modernly speaking: Epistles of Paul Judah Himango

      R Offline
      R Offline
      Rama Krishna Vavilala
      wrote on last edited by
      #2

      Nothing. http://www.codeproject.com/Feature/SubtleBugs.asp?msg=1683788#xx1683788xx[^];)


      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

      J I 2 Replies Last reply
      0
      • R Rama Krishna Vavilala

        Nothing. http://www.codeproject.com/Feature/SubtleBugs.asp?msg=1683788#xx1683788xx[^];)


        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

        J Offline
        J Offline
        Judah Gabriel Himango
        wrote on last edited by
        #3

        Oh, you beat me then. Dang. :)

        Tech, life, family, faith: Give me a visit. I'm currently blogging about: God-as-Judge, God-as-Forgiver The apostle Paul, modernly speaking: Epistles of Paul Judah Himango

        1 Reply Last reply
        0
        • R Rama Krishna Vavilala

          Nothing. http://www.codeproject.com/Feature/SubtleBugs.asp?msg=1683788#xx1683788xx[^];)


          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

          I Offline
          I Offline
          Ian MacLean
          wrote on last edited by
          #4

          that indeed is a subtle bug, .. how does it throw nothing?

          R J 2 Replies Last reply
          0
          • I Ian MacLean

            that indeed is a subtle bug, .. how does it throw nothing?

            R Offline
            R Offline
            Rama Krishna Vavilala
            wrote on last edited by
            #5

            Because until you call on GetEnumerator on the returned enumerator the code within the function does not get executed.


            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
            • I Ian MacLean

              that indeed is a subtle bug, .. how does it throw nothing?

              J Offline
              J Offline
              Judah Gabriel Himango
              wrote on last edited by
              #6

              Ian MacLean wrote:

              how does it throw nothing

              This is something borrowed from functional programming -- the delayed execution part, not the no-exception throw. Under the hood, the C# compiler generates a class that implements IEnumerator, containing all captured local variables. Each time foreach is iterated (which corresponds to IEnumerator.MoveNext() method call), the next line would be yielded. Because of this delayed execution, you can pull of some pretty amazing things, such as yielding every even integer without having to allocate an array of size int.Maximum, or yield recursive hierarchies without having to allocate lists, and so on. But it does introduce subtle bugs like the one above.

              Tech, life, family, faith: Give me a visit. I'm currently blogging about: God-as-Judge, God-as-Forgiver The apostle Paul, modernly speaking: Epistles of Paul Judah Himango

              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