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.
  • 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
      • 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
        #7

        Thank you. This cleared up my confusion.

        A 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
          #8

          Thank you!

          V 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