Implementing IEnumerable<> for a custom hash table data structure to be able to use LINQ
Hello guys, I needed to create a hash table from scratch. Now, I want to use that hash table with LINQ. I read that we need to implement the IEnumerable<>. That done, my IDE yell at me to use the follwing methods:
I believed it's mandatory to implement them since they are part of the interface. My question is, what is theur usage, why do we need them? The implementation given is what my IDE gave me, do I need to add more logic to it?
7 Replies
Unknown User•3w ago
Message Not Public
Sign In & Join Server To View
oh ok I see, thanks !
By the way for LINQ to work, do I need to adjust any logic? I just checked that from what my IDE gave me, that is the code above, intellisense now gave me possible methods to be use on LINQ but for them to work, is there additional logic that need to be added or it should work ?
yep, I know it's the starting point to enter a loop and keep moving until we reach the end (I think)
hmm normally if we implement the IEnumerable interface, we must have this:
Is there any logic to be adjusted here or everything is taken care behind the scenes?
oh ok, will just try things out and came back
you're always meant to remove throw new NotImplementedException();
Hmm, normally, we replace it with what kind of logic? I mean, I know the GetEnumerator will return an enumerator, so there should be some kind of enumerator being return?
hmm basically, me what I wanted to do, is jut to use LINQ with my custom collection, but I don't really understand how the GetEnumerator method works :cIt returns an IEnumerator.
Unknown User•3w ago
Message Not Public
Sign In & Join Server To View
an object that is enumerable defines a default way to go through its elements one by one
e.g. for an array the default way would be to go through them in order, from first to last
you know how e.g. in case of an array, you'd use a loop and an index that gets updated after each iteration?
more generally, whatever you use to be able to access the "current" element, which changes each iteration, may be called the iteration state
in case of an array, it would be the index variable
if you have an array of arrays for example, and wanted to go through each element, it would be 2 index variables
the second component to this is how the iteration state gets updated each iteration
in case of a single index and an array, it would be to just increase it by 1
which would move it to point to the next item in the array
in case of an array of arrays and two index variables, you'd try to move the first one, and if it goes outside the current array, move the other one, to point to the next array
which would emulate a nested loop
the point is, there is a way to advance the iteration state
and lastly, there's a way to check when you've gone through all the items
in case of an array, this happens when the index is outside the bounds of the array
in case of an array of arrays, it's when both indices are
so, to go through each possible iteration state, you need:
- the updateable iteration state (one or more variables)
- the rules that take a state to the next one (e.g. increment by 1)
- a way determine when you're done
an enumerator means all these + a reference to your data structure, the collection where you store the items
so you can access the current item corresponding to the current iteration state in a self contained way (without extra data)
its methods/properties:
-
Current
: access the item corresponding to the current iteration state
- MoveNext
: advances the iteration state + does the check if we're done (returns true if not done)
There's also Reset, but it's considered legacy.
and an enumerable just allows you to make one of these
via GetEnumerator
Linq uses enumerators to go through your things
while enumerables allow it to construct a sort of pipeline that transforms the items before they get to you, without actually going through them before you evaluate itOk, I've done a bit of reading, from what I've understood:
An Enumerable is something that can be traversed/loop through/iterated over.
An enumerator/iterator is like a "pointer" telling us our iteration state, where we actually are.
Now question:
Consider the following:
The
throw new NotImplementedException()
means that we need to add logic to this method. GetEnumerator should return an IEnumerator<V>.
This is where I'm a bit confused, how do we proceed to return an "IEnumerator<V>" ? (I've seen keywords like yield
, this is where they come into play?)
I think I understood... consider the following:
So the purpose of the GetEnumerator
is to return an enumerator, which can happen through the yield return value
statement. The enumerator is what makes looping and LINQ possible?At the most basic level you create a class that inherits from IEnumerable and implement the handful of methods required.
yield return is magic syntax that creates a class that inherits from IEnumerable/IEnumerator and implements the handful of methods required.
>The enumerator is what makes looping and LINQ possible?
Most non-Querayble Linq methods are just extension methods for IEnumerable. For instance,
Enumerable.Where<T>