Avatar of jazzIIIlove
jazzIIIloveFlag 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)
SOLUTION
Avatar of Paul Jackson
Paul Jackson
Flag of United Kingdom of Great Britain and Northern Ireland image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
SOLUTION
Avatar of Rick
Rick

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
SOLUTION
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of jazzIIIlove
jazzIIIlove
Flag of Sweden image

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
Avatar of kaufmed
kaufmed
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of jazzIIIlove
jazzIIIlove
Flag of Sweden image

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
Avatar of kaufmed
kaufmed
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
>>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.
C#
C#

C# is an object-oriented programming language created in conjunction with Microsoft’s .NET framework. Compilation is usually done into the Microsoft Intermediate Language (MSIL), which is then JIT-compiled to native code (and cached) during execution in the Common Language Runtime (CLR).

98K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo