Foreach
-
Hello. I have folowing foreach loop
foreach(Process proc in GetProcessFromSystem())
{
}GetProcessFromSystem()
is a method that retrives all running processes, witch can easly change. Does foreach handles such thimg or is it dangereous to use in a such whay? -
Hello. I have folowing foreach loop
foreach(Process proc in GetProcessFromSystem())
{
}GetProcessFromSystem()
is a method that retrives all running processes, witch can easly change. Does foreach handles such thimg or is it dangereous to use in a such whay?A foreach loop can run on anything that implements IEnumerable - so, IMO, this should be fine, if that was your question. I dont know what has been implemented in
GetProcessFromSystem()
so this cannot be commented upon.The funniest thing about this particular signature is that by the time you realise it doesn't say anything it's too late to stop reading it. My latest tip/trick Visit the Hindi forum here.
-
A foreach loop can run on anything that implements IEnumerable - so, IMO, this should be fine, if that was your question. I dont know what has been implemented in
GetProcessFromSystem()
so this cannot be commented upon.The funniest thing about this particular signature is that by the time you realise it doesn't say anything it's too late to stop reading it. My latest tip/trick Visit the Hindi forum here.
is
GetProcessFromSystem()
called each loop? if so it can give a problem. -
Hello. I have folowing foreach loop
foreach(Process proc in GetProcessFromSystem())
{
}GetProcessFromSystem()
is a method that retrives all running processes, witch can easly change. Does foreach handles such thimg or is it dangereous to use in a such whay?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.
-
is
GetProcessFromSystem()
called each loop? if so it can give a problem.nothing inside the parentheses of foreach is called more than once. it would be different if you had a for, e.g.:
for(int i=0; i<GetProcessFromSystem().Count; i++) { ... }
would evaluate the termination test, and hence call the method, upon each iteration. :)
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.
-
Hello. I have folowing foreach loop
foreach(Process proc in GetProcessFromSystem())
{
}GetProcessFromSystem()
is a method that retrives all running processes, witch can easly change. Does foreach handles such thimg or is it dangereous to use in a such whay?Assuming, for the sake of this answer, that
GetProcessFromSystem()
returns aList
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 theforeach
loop, consuming resources. In addition the list could easily change, items might be added or even removed. Anything implementingIEnumerable
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.”
-
nothing inside the parentheses of foreach is called more than once. it would be different if you had a for, e.g.:
for(int i=0; i<GetProcessFromSystem().Count; i++) { ... }
would evaluate the termination test, and hence call the method, upon each iteration. :)
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.
Thank you, this was the answer i was looking.
-
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.
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 -
Assuming, for the sake of this answer, that
GetProcessFromSystem()
returns aList
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 theforeach
loop, consuming resources. In addition the list could easily change, items might be added or even removed. Anything implementingIEnumerable
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.”
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.
-
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 managerWhen 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.
-
Assuming, for the sake of this answer, that
GetProcessFromSystem()
returns aList
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 theforeach
loop, consuming resources. In addition the list could easily change, items might be added or even removed. Anything implementingIEnumerable
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.”
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 -
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.
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.”
-
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 KreskowiakThank 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.”
-
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.”
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.
-
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.
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.”
-
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.”
:laugh: Then what's with the elephant??
A guide to posting questions on CodeProject[^]
Dave Kreskowiak -
:laugh: Then what's with the elephant??
A guide to posting questions on CodeProject[^]
Dave Kreskowiakforeach(Elephant I in GetMyZoo()) I.CantRemember();
:-DLuc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.
-
:laugh: Then what's with the elephant??
A guide to posting questions on CodeProject[^]
Dave KreskowiakThe 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.”
-
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.”
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.
-
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.
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.”