NET PR

I have the following code:

foreach (string word in words)

{

//do stuff to each word, as go through the loop

}

My question is: i want to check inside the loop if the word = "car", and if it does, then i want to concatenate that word with the next word in the words array.

My code assumes all mentions of the word car is part of the word carpool, and some users enter it as two words.



Re: Visual C# General How to increment the array element in a FOREACH loop

Mark Dawson

Hi,

you cannot increment past an item in a foreach loop, you will need to use a standard for loop in this case where you can increment the index manually.

Mark.






Re: Visual C# General How to increment the array element in a FOREACH loop

Indian Ocean

Hi,



int i=0;
foreach (string word in words)
{
if(word == "car")
{
words[i+1] += word;
}
i++;
}

Or use standard "for" loop,

HTH,