Answered how to reference foreach object

sacchi

New member
Joined
Aug 25, 2020
Messages
2
Programming Experience
10+
Hi all,

I have two lists of string.
C#:
List<string> myData1 = new List<string>();
List<string> myData2 = new List<string>();

After filling them with some data, I do some work on them:

C#:
foreach (string a in myData1)
{
    foreach (string b in myData2)
    {
        // do some stuff
    }
}

If a check is succesful inside both loops, I have to mark the current myData1 and myData2 by adding a "*" at the beginning.
How can I reference the current myData1 and myData2 string in order to add the "*" character, please?

Best regards,

Lorenzo
 
Last edited by a moderator:
You cannot. You are not allowed to modify the current item that the foreach is currently iterating over. Your best bests are to either build new lists, or use for loops instead of foreach loops.

Also be aware that C# strings are immutable. You can't change a string value. You'll need to make a new string with the asterisk.
 
Back
Top Bottom