hello , i am learning OOP C# and im having a really hard time wrapping my head around Ienumerator and Ienumrable interfaces in terms of the logic + implementations and ive been searching for explenations online and even read microsofts documentation and still dont fully understand the syntex, is there any place that explains it in a very understandable and practical way ?
what i do know is that an ienumrable is used to iterate in a foreach loop and ienumerator iterates through the ienumrable list ? thats about as far i ihave been able to understand in terms of logic but i have a hard time understanding the syntex of classfying an object as ienumrable and how can i loop over it in a foreach loop and the ienumerator completely flies over my head
An enumerable can be enumerated. An enumerator stores the position as you are doing so.
As a newb, dont try to mess with the ienumerable interface. Just know that it allows loops in C# to work and that you can use LINQ with it.
Do you know the ‘yield return’ keyword? It is a way to make an enumerable.
Yield return pauzes the method until the next value is requested by a enumarator. Then it continues until it reaches yield return again. This continues until the function reaches its end and then the enumerator will end also.
That function will return an IEnumerable that you can use in a foreach loop for example.
A good way to think about enumerables is that it is a thing that implements a way to get the next value.
Edit: wrong button. Was meant to be a top level comment.
What is tripping you up? It has a property called Current that has current item. MoveNext tries to get the next item. If it can it sets current and returns true. So basically this is a foreach loop
Yup, and the reason there are two interfaces is because multiple pieces of code might want to enumerate the thing at once.
So you call items.GetEnumerator()
and this gives you your own private IEnumerator
that keeps track of where you are up to, lets you MoveNext
at your own pace, etc.
That actually clears up a lot thank you
I’ve never had to actually implement either of those interfaces on my custom classes and from all my experience and code reviews, have not seen them manually implemented. As they are generic interfaces, I always use the appropriate built-in collection for my usecase. I would not worry about it too much. Under the hood, IEnumerables use IEnumerators to return things (Ienumerator.GetNext) and this is important because not all collections can be accessed by index. For example, you cannot do a for loop through a hashset to iterate its contents
Inspect the interfaces ( f12 ) to see what it’s behavior is
I would start with “Generator” and “Iterator” programing keywords on wikipedia. These interfaces are just the C# way to do that.
https://en.wikipedia.org/wiki/Generator_(computer_programming)#C#
https://en.wikipedia.org/wiki/Iterator#C#_and_other_.NET_languages
Next I would look in to how are the IEnumerable used in LINQ and Extension methods to do usefull stuff like:
The it start making sense why you would like to use the “Take, Where, Sum …” (extension)methods on Lists, Arrays, generators and custom types to do the useful things.
As an example: you can scrape the tweets by someone and display the last one. If you scraper can return IEnumerator<string> you can do the display logic in simple foreach loop. (and do the testing by using an string[] as value for the method..)
Old fart here. I cannot remember what enumerable and enumerator do so I have to search every time. Naming sucks.
If you can consume something it is consumable. If you are actually consuming something you are a consumer. They are really the correct terms. An enumerable can be enumerated and an enumerator actually enumerates.
Do you mean how to make an object Enumerable ?
fromm what i learned is that Enumerable and Enumerator are both interfaces i think i just dont have a good base of understanding what they are so if someone could share a clear understandable source that explains Enumerable and Enumerator would be a great help if possible with codeo examples as well!
Interfaces are contracts that guarantee some known behavior and/or structure at design time. You can build a class using any interface contract which enforces and guarantees certain behavior and structure. This allows the IDE/compiler to ‘know’ how to treat your code based on the interface.
IEnumerable is a contract that says this class will always have specific Properties and Methods to handle iterating over objects made from it.
List implements IList that implements IEnumerable. You should read the documentation.