Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

C# C# Objects Loops and Final Touches Foreach Loops

What is the basic format of a foreach loop?

I see where the presenter coded foreach(Invader invader in invaders)

But, where is this

Invader invader in invaders

coming from?

From the MS documentation here, I see that you can break down this format to:

foreach("local variable type identifier" in "expression") { }

So here, "invader" would be the local variable, "Invader" would be the local variable type identifier" and "invaders" would be the expression. This foreach loop would loop through every "invader" in the collection.

Correct?

1 Answer

Christopher Rutter
Christopher Rutter
2,230 Points

Yes, because "Invader" is the type whereas "invader" is the variable. Both of these things together create "the local variable type identifier" mentioned in ms docs. Thus, the "invaders" collection would be iterated through, the iterator being the "invader".

TLDR; You are correct and answered your own question.