For each question.

I'm into array's in the CLR environment, and I had a question about the for each loop. I'll bold my question in my code.

// Sorting an array of Kyes(the names) and an array of objects(the weights)

#include "stdafx.h"

using namespace System;

int main(array<System::String ^> ^args)
{
array<String^>^ names = { "Jill", "Ted", "Mary", "Eve", "Bill", "Al"};
array<int>^ weights = { 103, 168, 128, 115, 180, 176};

Array::Sort( names,weights); my question arises in the for each loop. How does the compiler know that individual elements in the array "names" are to be treated individually by the loop? Does it understand that 's" means plural here and to take off the s means singular? What were to happen if I used a different variable name instead of names. say I named the array 'x' how would I write the for each loop for the array then?
for each(String^ name in names)
Console::Write(L"{0,10}", name);
Console::WriteLine();

for each(int weight in weights)
Console::Write(L"{0,10}", weight);
Console::WriteLine();
return 0;

}
So I just tried my own question and I replaced each instance of name or names with xs as the name of the array. Does this hold true for every variable? Why does the singular work in this instance ie why wouldn't it be

"

for each(String^ names in names)

as that is the name of the variable why does the singular work as well?
bump
It has nothing to do with how you name your variables, you can read a for each as for each element in array of elements (in you're case element is a String^). It would be valid to have for each(String^ x in y) as long as y is an array of String^
Topic archived. No new replies allowed.