From the course: Grasshopper and Rhino: C# Scripting

Unlock the full course today

Join today to access over 22,400 courses taught by industry experts or purchase this course individually.

Foreach looping

Foreach looping

From the course: Grasshopper and Rhino: C# Scripting

Start my 1-month free trial

Foreach looping

- [Instructor] The for loop in C# allows us to loop until a certain condition is met. Sometimes however, we may want to just loop over a collection of items, such as a list or an array, and to do that, we can use the foreach loop. The foreach loop works differently than the for loop in that we don't define the condition to end the loop. That is done for us, which is when we have completely looped through a collection of items. This means that the format is a little bit different and looks like this. We start with the keyword foreach followed by open and close parentheses. Within these parentheses, we include three parts. The first is declaring a temporary variable, which means we specify the type of object that we're looping over and the variable name that suits it. This will be updated with each item as we loop through the collection. Next, we have the keyword in, which is used to identify the collection we are…

Contents