Can someone explain IEnumerable and IEnumerator to me?
1 ah ah ah, two ah ah ah. Ahem
I’ll try to explain without using any code.
Let’s say you are running an airline company. And in each plane you want to know information about the passengers flying in the plane. Basically you want to be able to “traverse” the plane. In other words, you want to be able to start at the front seat, and to then work your way towards the back of the plane, asking passengers some information: who they are, where they are from etc. An aeroplane can only do this, by IEnumerable aviation law, if it is:
- countable, and
- if it has a counter.
What does countable mean?
If an airline is “countable”, this means that (according to the civil aviation authority) there MUST be a flight attendant present on the plane, who’s sole job is to count. By CAA Law, this counter MUST count in the manner prescribed the authority:
-
The counter MUST start before the first passenger.
-
Then he MUST “move next” up the aisle to the first seat.
-
He is to then record: (i) who the person is in the seat, and (ii) his current location in the aisle.
Counting Procedures
The captain of the airline wants a report on every passenger as and when they are investigated or counted. So after speaking to the person in the first seat, the counter then reports to the captain, and when the report is given, the counter remembers his exact position in the aisle and continues counting right where he left off.
In this manner the captain is always able to have information on the current person being investigated. That way, if he finds out that this individual barracks for Manchester City, then he can give that passenger preferential treatment etc.
- The counter keeps going till he reaches the end of the plane.
Summary
In other words, something is countable if it has a counter. And counter must (basically): (i) remember its place (state), (ii) be able to move next, (iii) and know about the current person he is dealing with.
Enumerable is just a fancy word for “countable”. In other words, an enumerable allows you to ‘enumerate’.
I hope that makes sense? Now if you read the above answers you should have more of a general idea what is going on.