Skip to main content

Posts

Showing posts with the label enum

Enumerator(s)

You've all used the foreach loop in .NET to get the values in a container. The foreach actually uses that objects Enumerator. In order to allow the use of the foreach on an object the class must implement the IEnumerable.GetEnumerator method which also means you have to implement the IEnumerable interface. Within the GetEnumerator override you need to return an instance of IEnumerator, so you need to create a class that implements the IEnumerator interface. see the code. This is useful if you class contains a member variable which is an or even a container. Let's say it's an array. When an instance of your class is create the array is also instantiated. But you don't want the array to be visible but you do want clients to be able to use a foreach on your class instance, this can be achieved by implementing the IEnumerable interface and overriding the GetEnumerator method. using System; using System.Collections.Generic; using System.Text; using System.Collections; namesp...