N
N
Nikolai Kuzyk2017-10-30 02:32:06
.NET
Nikolai Kuzyk, 2017-10-30 02:32:06

Using foreach iterators?

Hello everyone, the essence of the question:
there is a Foreach loop that actually iterates an object, how to understand why and when to use interfaces with their GetEnumerator () method; IEnumerable, IEnumerator, IEnumerable and IEnumerator
and what is the actual difference between using these interfaces with a loop? why can't I just use a loop without resorting to them ?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
C
cicatrix, 2017-10-30
@kolkinv

Interfaces are needed so that other classes can work with your own classes.
For example, you have implemented your own collection of objects with your own strange logic. Let's say you want this collection to be iterable with a foreach loop, but initially the framework has no idea how to iterate through the objects within your collection. That's when you declare that your class implements the IEnumerable interface and exposes the implementation of that interface to the outside. That's when the framework already knows how to iterate over all the elements of the collection.
IEnumerable implements both an array and a list and a dictionary and a linked list, that is, in order for you to be able to walk through any class using foreach, this class must implement IEnumerable. Think of an interface as a "collaboration agreement" between different classes that clearly defines the rules for interaction.

R
Roman, 2017-10-30
@yarosroman

Read https://habrahabr.ru/post/209914/

S
Stanislav Makarov, 2017-10-30
@Nipheris

There shouldn't be duck typing in a strongly typed language.

This is not duck typing, this is structural typing . Checks, as you may have noticed, are performed at compile time, not runtime - where is the duck typing? And the fact that it is not necessary to inherit from ODA. interface - so what's wrong with that? The language provides syntactic sugar in the form of foreach, the task of the compiler is to generate code containing calls to certain methods with a certain signature, nothing more. This is exactly the task for structural typing, when the compiler does not care what kind of class you have there and what interfaces it has, as long as you can generate the necessary code, and this is correct.
After all, templates in C++ work the same way. And for-range loops in C++ work in a similar way (require that the desired methods be implemented, or free functions that take an argument of the desired type). In C#, it is advised to implement an interface, because this gives additional possibilities: a) to pass an object where this interface is expected (because C# is still basically a language with nominative typing); b) make sure you don't forget to implement anything.
I disagree, it's quite controversial. Nominative typing says that if only the explicit statement that we are implementing some kind of interface gives us an is-a relationship between a class and an interface. If we DO NOT say that we implement IEnumerable, then we DO NOT implement it, even if we have the same methods there.
And yes, at the same time understand the difference between static and strong typing, if you didn’t make a reservation in the comment, but really don’t understand the difference.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question