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. The Weird and The Wonderful
  4. Nice usage of foreach

Nice usage of foreach

Scheduled Pinned Locked Moved The Weird and The Wonderful
19 Posts 12 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.
  • M Marc Clifton

    You're sure it wasn't something like this?

    foreach (User user in users)
    {
    yield return user;
    }

    Marc

    Available for consulting and full time employment. Contact me. Interacx

    A Offline
    A Offline
    Andrew Chan
    wrote on last edited by
    #8

    Wow, I didn't know about the yield operator till now. Thanks for bringing this to my attention. :thumbsup: As for whether the code itself had a yield operator, it was so long ago I cannot remember. I'll give the programmer the benefit of the doubt.

    1 Reply Last reply
    0
    • L Luc Pattyn

      What is horrible about giving preferential treatment to the first user? By the time the app has more users, they probably will have introduced a new keyword yielding some attention to more recent customers... :)

      Luc Pattyn [Forum Guidelines] [My Articles]


      - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets


      J Offline
      J Offline
      Jeremy Tierman
      wrote on last edited by
      #9

      Just as long as it isn't alphabetical...I got tired of being at the end of the line in Elementary :-\

      1 Reply Last reply
      0
      • M Marc Clifton

        You're sure it wasn't something like this?

        foreach (User user in users)
        {
        yield return user;
        }

        Marc

        Available for consulting and full time employment. Contact me. Interacx

        R Offline
        R Offline
        Rob Grainger
        wrote on last edited by
        #10

        I think Luc may have been hinting at this - he'd better remember not to make such hints so subtle next time... "they probably will have introduced a new keyword yielding some attention to more recent customers..."

        L P 2 Replies Last reply
        0
        • R Rob Grainger

          I think Luc may have been hinting at this - he'd better remember not to make such hints so subtle next time... "they probably will have introduced a new keyword yielding some attention to more recent customers..."

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

          :thumbsup:

          Luc Pattyn [Forum Guidelines] [My Articles]


          - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets


          1 Reply Last reply
          0
          • R Rob Grainger

            I think Luc may have been hinting at this - he'd better remember not to make such hints so subtle next time... "they probably will have introduced a new keyword yielding some attention to more recent customers..."

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

            Party pooper.

            L 1 Reply Last reply
            0
            • P PIEBALDconsult

              Party pooper.

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

              :thumbsup::thumbsup:

              Luc Pattyn [Forum Guidelines] [My Articles]


              - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets


              1 Reply Last reply
              0
              • A Andrew Chan

                I once saw a programmer write this. foreach (User user in users) { return user; } :laugh:

                S Offline
                S Offline
                Soundman32 2
                wrote on last edited by
                #14

                I have seen this when the array doesn't contain a indexing property. return users[0]; // Doesn't work I seem to remember this being the default behaviour for non generic Lists in early .net (2?).

                J 1 Reply Last reply
                0
                • S Soundman32 2

                  I have seen this when the array doesn't contain a indexing property. return users[0]; // Doesn't work I seem to remember this being the default behaviour for non generic Lists in early .net (2?).

                  J Offline
                  J Offline
                  JasonPSage
                  wrote on last edited by
                  #15

                  I've seen that - and I've also done this "kludge" when I literally didn't have time to look it up: e.g. Must get done fast, bigger fish to fry.. this works - move on. Not ideal - but - depends what's going on.. schedule.. time constraints. It's still readable at least. --Jason

                  Know way to many languages... master of none!

                  1 Reply Last reply
                  0
                  • M Marc Clifton

                    You're sure it wasn't something like this?

                    foreach (User user in users)
                    {
                    yield return user;
                    }

                    Marc

                    Available for consulting and full time employment. Contact me. Interacx

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #16

                    hey 10x for teling abt yield operator

                    Ravie Busie Coding is my birth-right and bugs are part of feature my code has!

                    1 Reply Last reply
                    0
                    • J J4amieC

                      IEnumerable.FirstOrDefault[^] aside of course.

                      C Offline
                      C Offline
                      cpkilekofp
                      wrote on last edited by
                      #17

                      J4amieC wrote:

                      IEnumerable.FirstOrDefault[^] aside of course.

                      If, in fact, you are using .NET 3.5, as otherwise this doesn't exist.

                      1 Reply Last reply
                      0
                      • P PIEBALDconsult

                        If users provides only the IEnumerable interface then you kinda have to do that. We'd need to see more of the code to determine just how horrible it is.

                        D Offline
                        D Offline
                        dojohansen
                        wrote on last edited by
                        #18

                        Ouch, you sure stepped in the salad! That code necessarily returns the first item in enumerating "users". Had it been a yield return things would be different though.

                        P 1 Reply Last reply
                        0
                        • D dojohansen

                          Ouch, you sure stepped in the salad! That code necessarily returns the first item in enumerating "users". Had it been a yield return things would be different though.

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

                          Ummm... yeah, so? :confused:

                          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