foreach drawbacks?

jazzIIIlove
jazzIIIlove used Ask the Experts™
on
Hi there;

I get used to for and while loops. but when it comes to foreach, I couldn't figure out when or how I should use them.

Is there any drawbacks in foreach statements, that, a scenario that they are not applicable and applicable?

Kind regards.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Paul JacksonSoftware Engineer
Top Expert 2011
Commented:
A foreach statement is used when you want to repeat a group of statements for each element in an array or object collection

http://msdn.microsoft.com/en-us/library/aa288254(v=vs.71).aspx
http://msdn.microsoft.com/en-us/library/aa288257(v=vs.71).aspx
Commented:
Well, a foreach statement should not be used to add or remove items from the collection it is looping through.

Depending on the circumstances, foreach can be slower than for

.
>>Is there any drawbacks in foreach statements
There is actually no drawback in foreach, but it depends upon the situation when to use what(for/foreach)

Suppose, I say print nos from 1 to 100,
Here for is the best choice than foreach.
for(int i = 1, i<=100; i++)
C.W(i);
You shuldn't expect foreach to do this job for you. You can consider it as a drawback.But actually it's not.foreach's invention is not for this purpose as it's name suggest.

foreach is used when you are traversing through each element of an array or collections.

We can say for each integer in an array, do something.
Ex:
int[ ] A = new int[ ] {1,2,3,4,5};
foreach(int i in A)
  C.W(i);

We also can say for each object in a collection, do something

Thanks for the explanations,

so for each int in an array or collection is the phrase for 'foreach'.

So, in practice, I can loop through for the following:

for(int i = 0;i i < 100; i++)
with for each.

Kind regards.
Glanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015
Commented:
A foreach can be a bit cleaner in its readability. For example, both of these snippets do the same thing. If you had never programmed before, however, and you were shown these, which one might make more sense to you?

Given
List<string> myList = // an initialized list

Open in new window



for (int i = 0; i < myList.Length; i++)
{
    Console.WriteLine(myList[i]);
}

Open in new window


foreach (string item in myList)
{
    Console.WriteLine(item);
}

Open in new window


So, in practice, I can loop through for the following:

Open in new window

Yes, provided you don't try to do:

foreach (int i in intList)
{
    intList.Remove(i);
}

Open in new window

i couldn't get the last code snippet which is;

foreach (int i in intList)
{
    intList.Remove(i);
}

Is this an equivalent for;

for(int i = 0;i < 100; i++)

?

And what is the type of intList as I assume it's a list, if so, then, that lntList one is not simialar to < 100

Kind regards.
kaufmedGlanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015
Commented:
Is this an equivalent for;
Not verbatim. It all depends on what you are doing in your for loop. If you are trying to generate a series of numbers from 1 to 100, then no, a foreach would not work. You have to have some collection to iterate over when using a foreach. A collection can be an array, a collection, an ArrayList, etc. My example in http:#codeSnippet20-36309685-5 was to reinforce what rick_gwu stated earlier ( http:#36309423 ).
kaufmedGlanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015
Commented:
A collection can be an array, a collection, an ArrayList, etc.
Maybe I shouldn't use the target word in its definition...  Let me try again:

A collection can be an array, a List, an ArrayList, etc.
>>i couldn't get the last code snippet which is;

>>foreach (int i in intList)
>>{
    >>intList.Remove(i);
>>}
intList is a List of Integers
This code is removing each elements/integers from the list.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial