Skip to main content
AI Assist is now on Stack Overflow. Start a chat to get instant answers from across the network. Sign up to save and share your chats.
added 226 characters in body
Source Link
BradleyDotNET
  • 61.5k
  • 10
  • 106
  • 124

foreach always works the same, so doing it over a form's controls doesn't change anything.

It doesn't really use an index either; foreach only works on instances of classes that implement IEnumerable which means they have a GetEnumerator method. foreach uses the returned IEnumerator to go through the collection one at a time; order is dependent on the actual implementation.

For List (or any array) this is in-order (ie index 0 is first, 1 second, and so on); for a random implementation of IEnumerable you would have to look at the source. In your specific example, look at the type of the Controls collection on Form. Its a ControlCollection so you'll want to look on Reference Source to find how it enumerates.

Based on the implementation it appears to be in-order with some safeguards against controls being removed during enumeration.

foreach always works the same, so doing it over a form's controls doesn't change anything.

It doesn't really use an index either; foreach only works on instances of classes that implement IEnumerable which means they have a GetEnumerator method. foreach uses the returned IEnumerator to go through the collection one at a time; order is dependent on the actual implementation.

For List (or any array) this is in-order (ie index 0 is first, 1 second, and so on); for a random implementation of IEnumerable you would have to look at the source. In your specific example, look at the type of the Controls collection on Form. Its a ControlCollection so you'll want to look on Reference Source to find how it enumerates.

foreach always works the same, so doing it over a form's controls doesn't change anything.

It doesn't really use an index either; foreach only works on instances of classes that implement IEnumerable which means they have a GetEnumerator method. foreach uses the returned IEnumerator to go through the collection one at a time; order is dependent on the actual implementation.

For List (or any array) this is in-order (ie index 0 is first, 1 second, and so on); for a random implementation of IEnumerable you would have to look at the source. In your specific example, look at the type of the Controls collection on Form. Its a ControlCollection so you'll want to look on Reference Source to find how it enumerates.

Based on the implementation it appears to be in-order with some safeguards against controls being removed during enumeration.

Source Link
BradleyDotNET
  • 61.5k
  • 10
  • 106
  • 124

foreach always works the same, so doing it over a form's controls doesn't change anything.

It doesn't really use an index either; foreach only works on instances of classes that implement IEnumerable which means they have a GetEnumerator method. foreach uses the returned IEnumerator to go through the collection one at a time; order is dependent on the actual implementation.

For List (or any array) this is in-order (ie index 0 is first, 1 second, and so on); for a random implementation of IEnumerable you would have to look at the source. In your specific example, look at the type of the Controls collection on Form. Its a ControlCollection so you'll want to look on Reference Source to find how it enumerates.