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. Foreach

Foreach

Scheduled Pinned Locked Moved C#
question
21 Posts 5 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.
  • L Luc Pattyn

    for this to work, GetProcessFromSystem() has to return an IEnumerable. If it does, the foreach will be happy. However, if the enumerable would change while foreach is iterating over it, an exception would be thrown. Whether GetProcessFromSystem() implements and returns the information you hope to get cannot be determined without seeing it. :)

    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.

    S Offline
    S Offline
    Saksida Bojan
    wrote on last edited by
    #8

    Luc Pattyn wrote:

    Whether GetProcessFromSystem() implements and returns the information you hope to get cannot be determined without seeing it.

    If you want, it returns List, every time is called. If some app would crash and dissapear. Everytime this method calles, it construct the list from scratch. The only wory for me is that it wouldn't dissapear from the list, while it is in loop. It seems i am going to localize it first before passing to foreach loop. Also that method is a standart as you would get the same list from task manager

    L 1 Reply Last reply
    0
    • H Henry Minute

      Assuming, for the sake of this answer, that GetProcessFromSystem() returns a List it would be better to do:

      List procs = GetProcessFromSystem();

      foreach (Process proc in procs)
      {
      ............
      ............
      }

      Doing it the way you first proposed would be more greedy for system resources as (I think) GPFS() would be called on each iteration of the foreach loop, consuming resources. In addition the list could easily change, items might be added or even removed. Anything implementing IEnumerable can give unpredictable results, especially if things are removed from the list.

      Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

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

      Can't agree with that. In for (int x=0; x<bitmap.Width; x++) the Width property is fetched over and over, once per iteration, and returning the same value all the time; so it makes a lot of sense to use a local variable. Inforeach(type someVar in someExpressionYieldingAnEnumerator) the expression is evaluated once, and the resulting enumerator is worked with (i.e. its MoveNext method is called once per iteration). Using a local variable for holding the enumerator does not change a thing, except it adds to the typing and the risk of errors. :)

      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.

      H 1 Reply Last reply
      0
      • S Saksida Bojan

        Luc Pattyn wrote:

        Whether GetProcessFromSystem() implements and returns the information you hope to get cannot be determined without seeing it.

        If you want, it returns List, every time is called. If some app would crash and dissapear. Everytime this method calles, it construct the list from scratch. The only wory for me is that it wouldn't dissapear from the list, while it is in loop. It seems i am going to localize it first before passing to foreach loop. Also that method is a standart as you would get the same list from task manager

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

        When the method is returning a List, and no one is altering the list, then the list remains as is. Some process crashing, being killed or getting started will not modify the list. If you don't understand or don't trust how foreach works, then I can only suggest you study it, or stop using it. :)

        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.

        1 Reply Last reply
        0
        • H Henry Minute

          Assuming, for the sake of this answer, that GetProcessFromSystem() returns a List it would be better to do:

          List procs = GetProcessFromSystem();

          foreach (Process proc in procs)
          {
          ............
          ............
          }

          Doing it the way you first proposed would be more greedy for system resources as (I think) GPFS() would be called on each iteration of the foreach loop, consuming resources. In addition the list could easily change, items might be added or even removed. Anything implementing IEnumerable can give unpredictable results, especially if things are removed from the list.

          Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

          D Offline
          D Offline
          Dave Kreskowiak
          wrote on last edited by
          #11

          Henry Minute wrote:

          Doing it the way you first proposed would be more greedy for system resources as (I think) GPFS() would be called on each iteration of the foreach loop,

          No, it wouldn't. Your code is functionally identical to the OP's code. The code in the foreach's paranthesis is executed only once and the code in the curly braces is executed for each item in the collection returned by the paranthesis code. Now, if the collection we modified by a background thread or by the code in the curly braces, then you've got a problem...

          A guide to posting questions on CodeProject[^]
          Dave Kreskowiak

          H 1 Reply Last reply
          0
          • L Luc Pattyn

            Can't agree with that. In for (int x=0; x<bitmap.Width; x++) the Width property is fetched over and over, once per iteration, and returning the same value all the time; so it makes a lot of sense to use a local variable. Inforeach(type someVar in someExpressionYieldingAnEnumerator) the expression is evaluated once, and the resulting enumerator is worked with (i.e. its MoveNext method is called once per iteration). Using a local variable for holding the enumerator does not change a thing, except it adds to the typing and the risk of errors. :)

            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.

            H Offline
            H Offline
            Henry Minute
            wrote on last edited by
            #12

            Thanks! I stand corrected. (Well actually I'm sitting)

            Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

            L 1 Reply Last reply
            0
            • H Henry Minute

              Thanks! I stand corrected. (Well actually I'm sitting)

              Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

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

              still facing South? :)

              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.

              H 1 Reply Last reply
              0
              • D Dave Kreskowiak

                Henry Minute wrote:

                Doing it the way you first proposed would be more greedy for system resources as (I think) GPFS() would be called on each iteration of the foreach loop,

                No, it wouldn't. Your code is functionally identical to the OP's code. The code in the foreach's paranthesis is executed only once and the code in the curly braces is executed for each item in the collection returned by the paranthesis code. Now, if the collection we modified by a background thread or by the code in the curly braces, then you've got a problem...

                A guide to posting questions on CodeProject[^]
                Dave Kreskowiak

                H Offline
                H Offline
                Henry Minute
                wrote on last edited by
                #14

                Thank you. I'll try to remember that and probably fail. :)

                Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

                D 1 Reply Last reply
                0
                • L Luc Pattyn

                  still facing South? :)

                  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.

                  H Offline
                  H Offline
                  Henry Minute
                  wrote on last edited by
                  #15

                  Yup, I haven't rearranged the furniture.

                  Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

                  1 Reply Last reply
                  0
                  • H Henry Minute

                    Thank you. I'll try to remember that and probably fail. :)

                    Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

                    D Offline
                    D Offline
                    Dave Kreskowiak
                    wrote on last edited by
                    #16

                    :laugh: Then what's with the elephant??

                    A guide to posting questions on CodeProject[^]
                    Dave Kreskowiak

                    L H 2 Replies Last reply
                    0
                    • D Dave Kreskowiak

                      :laugh: Then what's with the elephant??

                      A guide to posting questions on CodeProject[^]
                      Dave Kreskowiak

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

                      foreach(Elephant I in GetMyZoo()) I.CantRemember(); :-D

                      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.

                      1 Reply Last reply
                      0
                      • D Dave Kreskowiak

                        :laugh: Then what's with the elephant??

                        A guide to posting questions on CodeProject[^]
                        Dave Kreskowiak

                        H Offline
                        H Offline
                        Henry Minute
                        wrote on last edited by
                        #18

                        The Elephant is Henry Minute. I have to do the typing for him because we cannot find a large enough keyboard.

                        Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

                        L 1 Reply Last reply
                        0
                        • H Henry Minute

                          The Elephant is Henry Minute. I have to do the typing for him because we cannot find a large enough keyboard.

                          Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

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

                          You should buy him an iPhone then. There is this little app that lets him trumphet in morse code, each character gets translated automatically in a regular keystroke. If he dislikes the iPhone (or when the neighbors object), there is an alternative based on a Wii Fit. That requires flapping the ears, this time using what amounts to the semaphore alphabet. :)

                          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.

                          H 1 Reply Last reply
                          0
                          • L Luc Pattyn

                            You should buy him an iPhone then. There is this little app that lets him trumphet in morse code, each character gets translated automatically in a regular keystroke. If he dislikes the iPhone (or when the neighbors object), there is an alternative based on a Wii Fit. That requires flapping the ears, this time using what amounts to the semaphore alphabet. :)

                            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.

                            H Offline
                            H Offline
                            Henry Minute
                            wrote on last edited by
                            #20

                            We tried your second suggestion. Never again. He got a little excited once and it took 3 weeks and a whole case of baby-oil to get his ears untangled.

                            Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

                            L 1 Reply Last reply
                            0
                            • H Henry Minute

                              We tried your second suggestion. Never again. He got a little excited once and it took 3 weeks and a whole case of baby-oil to get his ears untangled.

                              Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

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

                              is there a YouTube reference, or any other proof of bodily harm, discomfort, or damage; so others could benefit and maybe start a class action suit against Nintendo? :wtf:

                              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.

                              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