In this example we look at how array backed properties can serve as an item to be iterated over. This can be useful for calculating an average, or managing various different properties relating to a single object. Giving us the ability to cycle through them seamlessly rather than having to access them individually, yet at the same time allowing you the option to do so if desired. This isn't really an iterator, but does relate to iteration and can be very useful.
- Creature
- The creature type implements the IEnumerable interface of type int and contains several different properties which their values are stored in an array and used to calculate a stats average. The properties are Strength, Agility, Intelligence, and AverageStat. The backing array length is 3 which is the amount of stat properties contained in the type and is used by AverageState, an Indexer, and the GetEnumerator method so the array can be used in a foreach loop, property values can be accessed individually, or calculated effectively without breaking future changes.
Design Patterns in C# and .NET - Iterator: Array Backed Properties Iterator