I am trying to do a homework assignment that requires using a foreach loop to add items to an array. I did it using a for loop but cannot figure it out with a foreach loop.
Here is what I need, just in a foreach loop instead.
for (int i = 0; i < 5; i++)
{
Console.Write("\tPlease enter a score for {0} <0 to 100>: ", studentName[i]);
studentScore[i] = Convert.ToInt32(Console.ReadLine());
counter = i + 1;
accumulator += studentScore[i];
}
Sorry if this has been asked but I could not find an answer that helped me.
foreachis used with an array or list or anything of that sort.foreachkeyword works. You could also google the MSDN for it to see some examples.int[] studentScore = new int[5];and then do something likeforeach(var i in Enumerable.Range(0,5)), technically you are using aforeachloop, but it would be utterly pointless. Perhaps you meant to loop overstudentName? That collection, I assume, is already populated?