IEnumerable - why does this code work?
-
Karthik, I'm still a little confused. I know that the foreach statement requires a type that has implemented the IEnumerable or IEnumerator interface, what I don't understand is why the line
IEnumerable employees = root.Elements()
even works. I didn't implement the IEnumerable interface in a class anywhere. I just used that statement. In the book I'm reading it says that interfaces have to be a part of the classes declaration, for instance:public class Myclass : IEnumerable
Since I didn't do that, does the compiler make an object with the proper code for me, from the first code example?The XElement.Elements Method Returns a collection of the child elements of this element or document, in document order. as given here. http://msdn.microsoft.com/en-us/library/system.xml.linq.xelement.elements.aspx[^]. This collection implements IEnumerable, hence you could write the statement
IEnumerable employees = root.Elements();
-
Have a look at the definition of the
XContainer.Elements
method[^]. The type that this returns is anIEnumberable
collection. This allows you to run a loop over the elements of theemployees
object you create. It actually has nothing to do with you implementing or not implementing the interface, since the classXContainer
/XElement
possibly itself takes care of this internally. -
Have a look at the definition of the
XContainer.Elements
method[^]. The type that this returns is anIEnumberable
collection. This allows you to run a loop over the elements of theemployees
object you create. It actually has nothing to do with you implementing or not implementing the interface, since the classXContainer
/XElement
possibly itself takes care of this internally. -
The XElement.Elements Method Returns a collection of the child elements of this element or document, in document order. as given here. http://msdn.microsoft.com/en-us/library/system.xml.linq.xelement.elements.aspx[^]. This collection implements IEnumerable, hence you could write the statement
IEnumerable employees = root.Elements();
-
The XElement.Elements Method Returns a collection of the child elements of this element or document, in document order. as given here. http://msdn.microsoft.com/en-us/library/system.xml.linq.xelement.elements.aspx[^]. This collection implements IEnumerable, hence you could write the statement
IEnumerable employees = root.Elements();
-
The XElement.Elements Method Returns a collection of the child elements of this element or document, in document order. as given here. http://msdn.microsoft.com/en-us/library/system.xml.linq.xelement.elements.aspx[^]. This collection implements IEnumerable, hence you could write the statement
IEnumerable employees = root.Elements();
5+
Wonde Tadesse
-
5+
Wonde Tadesse