Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. IEnumerable - why does this code work?

IEnumerable - why does this code work?

Scheduled Pinned Locked Moved C#
questiondockerxmlperformance
14 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K 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

    J Offline
    J Offline
    jeramyRR
    wrote on last edited by
    #3

    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?

    V 1 Reply Last reply
    0
    • J jeramyRR

      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);
                 }
             }
      
         }
      

      }

      A Offline
      A Offline
      Abhinav S
      wrote on last edited by
      #4

      Have a look at the definition of the XContainer.Elements method[^]. The type that this returns is an IEnumberable collection. This allows you to run a loop over the elements of the employees object you create. It actually has nothing to do with you implementing or not implementing the interface, since the class XContainer / XElement possibly itself takes care of this internally.

      V J 2 Replies Last reply
      0
      • J jeramyRR

        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?

        V Offline
        V Offline
        VJ Reddy
        wrote on last edited by
        #5

        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();

        J A W 3 Replies Last reply
        0
        • A Abhinav S

          Have a look at the definition of the XContainer.Elements method[^]. The type that this returns is an IEnumberable collection. This allows you to run a loop over the elements of the employees object you create. It actually has nothing to do with you implementing or not implementing the interface, since the class XContainer / XElement possibly itself takes care of this internally.

          V Offline
          V Offline
          VJ Reddy
          wrote on last edited by
          #6

          You beat me in speed. 5!

          1 Reply Last reply
          0
          • V VJ Reddy

            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();

            J Offline
            J Offline
            jeramyRR
            wrote on last edited by
            #7

            Thank you!

            V 1 Reply Last reply
            0
            • A Abhinav S

              Have a look at the definition of the XContainer.Elements method[^]. The type that this returns is an IEnumberable collection. This allows you to run a loop over the elements of the employees object you create. It actually has nothing to do with you implementing or not implementing the interface, since the class XContainer / XElement possibly itself takes care of this internally.

              J Offline
              J Offline
              jeramyRR
              wrote on last edited by
              #8

              Thank you. This cleared up my confusion.

              A 1 Reply Last reply
              0
              • J jeramyRR

                Thank you. This cleared up my confusion.

                A Offline
                A Offline
                Abhinav S
                wrote on last edited by
                #9

                You are welcome. Vote if it helped.

                1 Reply Last reply
                0
                • V VJ Reddy

                  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();

                  A Offline
                  A Offline
                  Abhinav S
                  wrote on last edited by
                  #10

                  That is correct. 5.

                  V 1 Reply Last reply
                  0
                  • J jeramyRR

                    Thank you!

                    V Offline
                    V Offline
                    VJ Reddy
                    wrote on last edited by
                    #11

                    You are welcome.

                    1 Reply Last reply
                    0
                    • A Abhinav S

                      That is correct. 5.

                      V Offline
                      V Offline
                      VJ Reddy
                      wrote on last edited by
                      #12

                      Thank you, Abhinav.

                      1 Reply Last reply
                      0
                      • V VJ Reddy

                        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();

                        W Offline
                        W Offline
                        Wonde Tadesse
                        wrote on last edited by
                        #13

                        5+

                        Wonde Tadesse

                        V 1 Reply Last reply
                        0
                        • W Wonde Tadesse

                          5+

                          Wonde Tadesse

                          V Offline
                          V Offline
                          VJ Reddy
                          wrote on last edited by
                          #14

                          Thank you, Wonde Tadesse.

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups