Avatar of jazzIIIlove
jazzIIIlove
Flag for Sweden asked on

foreach drawbacks?

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.
C#

Avatar of undefined
Last Comment
Subrat (C++ windows/Linux)

8/22/2022 - Mon
SOLUTION
Paul Jackson

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
SOLUTION
Rick

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
jazzIIIlove

ASKER
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.
ASKER CERTIFIED SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
jazzIIIlove

ASKER
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.
SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Subrat (C++ windows/Linux)

>>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.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy