IEnumerable - why does this code work?
-
So basically the code below just creates an XML document in memory and then parses it and spits the values back out. The question I have is about the IEnumerable statements. I never implemented a class that uses the interface, but yet it still works in this code. Does the compiler automagically create a container or something? How does this work? Does this work for all interfaces?
class Program
{
static void Main(string[] args)
{
XDocument employeeDoc =
new XDocument(
new XElement("Employees",
new XElement("Employee",
new XElement("Name" , "Bob Smith"),
new XElement("Phone" , "408-555-1000"),
new XElement("Phone" , "408-555-1001")),
new XElement("Employee",
new XElement("Name", "Sally Jones"),
new XElement("Phone", "415-555-2000"),
new XElement("Phone", "415-555-2001"))
)
);XElement root = employeeDoc.Element("Employees"); IEnumerable employees = root.Elements(); foreach (XElement emp in employees) { XElement empNameNode = emp.Element("Name"); Console.WriteLine(empNameNode.Value); IEnumerable empPhones = emp.Elements("Phone"); foreach (XElement phone in empPhones) { Console.WriteLine(" {0}", phone.Value); } } }
}
-
So basically the code below just creates an XML document in memory and then parses it and spits the values back out. The question I have is about the IEnumerable statements. I never implemented a class that uses the interface, but yet it still works in this code. Does the compiler automagically create a container or something? How does this work? Does this work for all interfaces?
class Program
{
static void Main(string[] args)
{
XDocument employeeDoc =
new XDocument(
new XElement("Employees",
new XElement("Employee",
new XElement("Name" , "Bob Smith"),
new XElement("Phone" , "408-555-1000"),
new XElement("Phone" , "408-555-1001")),
new XElement("Employee",
new XElement("Name", "Sally Jones"),
new XElement("Phone", "415-555-2000"),
new XElement("Phone", "415-555-2001"))
)
);XElement root = employeeDoc.Element("Employees"); IEnumerable employees = root.Elements(); foreach (XElement emp in employees) { XElement empNameNode = emp.Element("Name"); Console.WriteLine(empNameNode.Value); IEnumerable empPhones = emp.Elements("Phone"); foreach (XElement phone in empPhones) { Console.WriteLine(" {0}", phone.Value); } } }
}
See these lines..
Quote:
IEnumerable employees = root.Elements(); foreach (XElement emp in employees) {
Here you have used foreach loop. SO, the definition says that, you can use foreach loop only on those objects or classes which implement IEnumerable interface. Therefore, in order to loop out all through the elements, you have taken the IEnumerable instance. hope it helps.
with regards Karthik Harve
-
See these lines..
Quote:
IEnumerable employees = root.Elements(); foreach (XElement emp in employees) {
Here you have used foreach loop. SO, the definition says that, you can use foreach loop only on those objects or classes which implement IEnumerable interface. Therefore, in order to loop out all through the elements, you have taken the IEnumerable instance. hope it helps.
with regards Karthik Harve
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? -
So basically the code below just creates an XML document in memory and then parses it and spits the values back out. The question I have is about the IEnumerable statements. I never implemented a class that uses the interface, but yet it still works in this code. Does the compiler automagically create a container or something? How does this work? Does this work for all interfaces?
class Program
{
static void Main(string[] args)
{
XDocument employeeDoc =
new XDocument(
new XElement("Employees",
new XElement("Employee",
new XElement("Name" , "Bob Smith"),
new XElement("Phone" , "408-555-1000"),
new XElement("Phone" , "408-555-1001")),
new XElement("Employee",
new XElement("Name", "Sally Jones"),
new XElement("Phone", "415-555-2000"),
new XElement("Phone", "415-555-2001"))
)
);XElement root = employeeDoc.Element("Employees"); IEnumerable employees = root.Elements(); foreach (XElement emp in employees) { XElement empNameNode = emp.Element("Name"); Console.WriteLine(empNameNode.Value); IEnumerable empPhones = emp.Elements("Phone"); foreach (XElement phone in empPhones) { Console.WriteLine(" {0}", phone.Value); } } }
}
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. -
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. -
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. -
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