I
I
Ivan Balan2014-08-31 17:05:15
JavaScript
Ivan Balan, 2014-08-31 17:05:15

How is IEnumerable implemented?

Let's take for example an ordinary list List, it implements the IEnumerable interface, which is inherited from IEnumerator
. If you create an IEnumerator interface variable,
IEnumerator<int> ien = (IEnumerator<int>) list
you can call the MoveNext method, and run through the elements of the collection. But why can I access these methods through an interface variable, but not directly through a list.
That is, if I write list.MoveNext - the compiler will report an error

System.Collections.Generic.List<int>' does not contain a definition for 'MoveNext'

In turn, I can call the GetEnumerator() method, which gives us the IEnumerable interface. What is the salt?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
N
Nicholas, 2016-07-28
@healqq

No, and part of the fault lies with the person who doesn't put curly braces in if/else. 1. In general, what does the function do? Checks if the specified string occurs in the text. To do this, we call the function on the root element, and if it is not text, we call the function recursively in the loop until we reach the text element. As soon as we got there, we check the string for a match, and if a match is found, we return true and exit the function. If no match was found in all descendants, return false. (note that false is returned after the loop), i.e. if at least one match is found, then everything is OK and true is returned.

B
BigObfuscator, 2014-09-11
@Toddams

Interfaces can be implemented in two ways: implicit and explicit.
In the first case, interface methods are directly accessible to the class. In the second case, the methods are available only after an explicit casting of the object to an interface type.
An explicit implementation of an interface allows a class to implement multiple interfaces with methods of the same name.

L
lam0x86, 2014-08-31
@lam0x86

I'm sorry, but IEnumerable doesn't inherit from IEnumerator. Your list casting code to IEnumerator should not compile. The IEnumerator and IEnumerable interfaces are tightly integrated with the compiler, which sometimes causes confusion: both interfaces can be used in yield-statements, but in fact only IEnumerable can be used in foreach-statements (and in fact, it's even more complicated - in foreach you can use any type that has a GetEnumerator () method, but that's a completely different story).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question