IEnumerable Interface

Introduction

Now’s a good time to explore how the foreach loop works. When we iterate using a for loop, we determine how that iteration works. How does the foreach loop know what the first and subsequent items are? How does it determine when there are no more items in the collection?

Know that foreach loops aren’t magic. Types that want to be iterated using a foreach loop must implement the IEnumerable interface:

Get hands-on with 1200+ tech skills courses.