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. General Programming
  3. C#
  4. Yield break

Yield break

Scheduled Pinned Locked Moved C#
phpasp-netcomperformancequestion
13 Posts 6 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.
  • B Brij

    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 overhead

    P Offline
    P Offline
    PIEBALDconsult
    wrote on last edited by
    #4

    Because you terminate the method before you get there.

    B 1 Reply Last reply
    0
    • P PIEBALDconsult

      Because you terminate the method before you get there.

      B Offline
      B Offline
      Brij
      wrote on last edited by
      #5

      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

      1 Reply Last reply
      0
      • B Brij

        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 overhead

        P Offline
        P Offline
        Pete OHanlon
        wrote on last edited by
        #6

        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

        My blog | My articles | MoXAML PowerToys | Onyx

        B 1 Reply Last reply
        0
        • P Pete OHanlon

          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

          My blog | My articles | MoXAML PowerToys | Onyx

          B Offline
          B Offline
          Brij
          wrote on last edited by
          #7

          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

          P 1 Reply Last reply
          0
          • B Brij

            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

            P Offline
            P Offline
            Pete OHanlon
            wrote on last edited by
            #8

            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

            My blog | My articles | MoXAML PowerToys | Onyx

            B L 2 Replies Last reply
            0
            • P Pete OHanlon

              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

              My blog | My articles | MoXAML PowerToys | Onyx

              B Offline
              B Offline
              Brij
              wrote on last edited by
              #9

              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

              1 Reply Last reply
              0
              • P Pete OHanlon

                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

                My blog | My articles | MoXAML PowerToys | Onyx

                L Offline
                L Offline
                Luc Pattyn
                wrote on last edited by
                #10

                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.

                P 1 Reply Last reply
                0
                • L Luc Pattyn

                  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.

                  P Offline
                  P Offline
                  Pete OHanlon
                  wrote on last edited by
                  #11

                  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

                  My blog | My articles | MoXAML PowerToys | Onyx

                  L 1 Reply Last reply
                  0
                  • P Pete OHanlon

                    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

                    My blog | My articles | MoXAML PowerToys | Onyx

                    L Offline
                    L Offline
                    Luc Pattyn
                    wrote on last edited by
                    #12

                    I agree, however I'm not surprised they did get it wrong at first: yield return statements are not really breaking program flow like regular returns would (they somewhat resemble a UNIX fork), however yield 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.

                    P 1 Reply Last reply
                    0
                    • L Luc Pattyn

                      I agree, however I'm not surprised they did get it wrong at first: yield return statements are not really breaking program flow like regular returns would (they somewhat resemble a UNIX fork), however yield 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.

                      P Offline
                      P Offline
                      Pete OHanlon
                      wrote on last edited by
                      #13

                      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

                      My blog | My articles | MoXAML PowerToys | Onyx

                      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