IEnumerable and IEnumerator

When you're working with collections of data, whether List, Dictionary, Stack, or others, you'll typically want to iterate (or traverse) all items in the list or at least some items, based on a specific criteria. In some cases, you'll want to loop through all items in sequence or some items. Most often, you'll want to traverse the items forwards in sequence, but as we've seen, there are times when reverse traversing is also suitable. You can loop through items using a standard for loop. However, this raises some annoyances that the interfaces of IEnumerable and IEnumerator can help us solve. Let's see what the annoyances are. Consider the for loop in the following code sample 6-5:

 //Create a total variable int Total ...

Get Mastering Unity Scripting now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.