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. LINQ
  4. List<T>.Find() vs Enumerable.FirstOrDefault()

List<T>.Find() vs Enumerable.FirstOrDefault()

Scheduled Pinned Locked Moved LINQ
visual-studiodata-structuresquestion
7 Posts 3 Posters 8 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.
  • A Offline
    A Offline
    Al Beback
    wrote on last edited by
    #1

    I have a List<T> object and I'm wondering what the difference is between calling List<T>.Find() and Enumerable.FirstOrDefault(). Which one should I use? Thanks in advance! Al

    Christianity: The belief that some cosmic Jewish zombie can make you live forever if you symbolically eat his flesh, drink his blood, and accept him as your master, so he can remove an evil force from your soul that is present in humanity because a rib-woman was convinced by a talking snake to eat from a magical tree. Makes perfect sense.

    P J 2 Replies Last reply
    0
    • A Al Beback

      I have a List<T> object and I'm wondering what the difference is between calling List<T>.Find() and Enumerable.FirstOrDefault(). Which one should I use? Thanks in advance! Al

      Christianity: The belief that some cosmic Jewish zombie can make you live forever if you symbolically eat his flesh, drink his blood, and accept him as your master, so he can remove an evil force from your soul that is present in humanity because a rib-woman was convinced by a talking snake to eat from a magical tree. Makes perfect sense.

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      Well, the most obvious thing is that FirstOrDefault will return you a default value if you don't find a match.

      Deja View - the feeling that you've seen this post before.

      My blog | My articles

      A 1 Reply Last reply
      0
      • A Al Beback

        I have a List<T> object and I'm wondering what the difference is between calling List<T>.Find() and Enumerable.FirstOrDefault(). Which one should I use? Thanks in advance! Al

        Christianity: The belief that some cosmic Jewish zombie can make you live forever if you symbolically eat his flesh, drink his blood, and accept him as your master, so he can remove an evil force from your soul that is present in humanity because a rib-woman was convinced by a talking snake to eat from a magical tree. Makes perfect sense.

        J Offline
        J Offline
        Judah Gabriel Himango
        wrote on last edited by
        #3

        In general, it's preferable to use the instance method rather than the extension method, as instance methods know the inner details of the type they're working on, whereas extension methods do not. (Following this logic, it would be recommended to use List.Find rather than IEnumerable.FirstOrDefault. That said, if you look at the implementation of List.Find, you'll see it's doing exactly the same thing as Enumerable.FirstOrDefault; they'll perform roughly the same.

        1 Reply Last reply
        0
        • P Pete OHanlon

          Well, the most obvious thing is that FirstOrDefault will return you a default value if you don't find a match.

          Deja View - the feeling that you've seen this post before.

          My blog | My articles

          A Offline
          A Offline
          Al Beback
          wrote on last edited by
          #4

          Pete O'Hanlon wrote:

          Well, the most obvious thing is that FirstOrDefault will return you a default value if you don't find a match.

          The docs for Find say the following: Return Value Type: T The first element that matches the conditions defined by the specified predicate, if found; otherwise, the default value for type T.

          Christianity: The belief that some cosmic Jewish zombie can make you live forever if you symbolically eat his flesh, drink his blood, and accept him as your master, so he can remove an evil force from your soul that is present in humanity because a rib-woman was convinced by a talking snake to eat from a magical tree. Makes perfect sense.

          P 1 Reply Last reply
          0
          • A Al Beback

            Pete O'Hanlon wrote:

            Well, the most obvious thing is that FirstOrDefault will return you a default value if you don't find a match.

            The docs for Find say the following: Return Value Type: T The first element that matches the conditions defined by the specified predicate, if found; otherwise, the default value for type T.

            Christianity: The belief that some cosmic Jewish zombie can make you live forever if you symbolically eat his flesh, drink his blood, and accept him as your master, so he can remove an evil force from your soul that is present in humanity because a rib-woman was convinced by a talking snake to eat from a magical tree. Makes perfect sense.

            P Offline
            P Offline
            Pete OHanlon
            wrote on last edited by
            #5

            I really wish there was an ironic icon. That was the effect I was trying to achieve. In practical terms, there are no real differences. As you are aware, one's just the Linq version of the other. If you remember that Linq is effectively just syntactic sugar, you get the idea as to what the FirstOrDefault emulates.

            Deja View - the feeling that you've seen this post before.

            My blog | My articles

            A 1 Reply Last reply
            0
            • P Pete OHanlon

              I really wish there was an ironic icon. That was the effect I was trying to achieve. In practical terms, there are no real differences. As you are aware, one's just the Linq version of the other. If you remember that Linq is effectively just syntactic sugar, you get the idea as to what the FirstOrDefault emulates.

              Deja View - the feeling that you've seen this post before.

              My blog | My articles

              A Offline
              A Offline
              Al Beback
              wrote on last edited by
              #6

              Pete O'Hanlon wrote:

              I really wish there was an ironic icon. That was the effect I was trying to achieve.

              Sorry to have missed that. I use :rolleyes: to express sarcasm, but I suppose it's not the same.

              Pete O'Hanlon wrote:

              In practical terms, there are no real differences. As you are aware, one's just the Linq version of the other. If you remember that Linq is effectively just syntactic sugar, you get the idea as to what the FirstOrDefault emulates.

              So then, in your code, which one would you use? I'm leaning toward FirstOrDefault just to be consistent across collections, but I would prefer to use Find if there's a performance advantage.

              Christianity: The belief that some cosmic Jewish zombie can make you live forever if you symbolically eat his flesh, drink his blood, and accept him as your master, so he can remove an evil force from your soul that is present in humanity because a rib-woman was convinced by a talking snake to eat from a magical tree. Makes perfect sense.

              P 1 Reply Last reply
              0
              • A Al Beback

                Pete O'Hanlon wrote:

                I really wish there was an ironic icon. That was the effect I was trying to achieve.

                Sorry to have missed that. I use :rolleyes: to express sarcasm, but I suppose it's not the same.

                Pete O'Hanlon wrote:

                In practical terms, there are no real differences. As you are aware, one's just the Linq version of the other. If you remember that Linq is effectively just syntactic sugar, you get the idea as to what the FirstOrDefault emulates.

                So then, in your code, which one would you use? I'm leaning toward FirstOrDefault just to be consistent across collections, but I would prefer to use Find if there's a performance advantage.

                Christianity: The belief that some cosmic Jewish zombie can make you live forever if you symbolically eat his flesh, drink his blood, and accept him as your master, so he can remove an evil force from your soul that is present in humanity because a rib-woman was convinced by a talking snake to eat from a magical tree. Makes perfect sense.

                P Offline
                P Offline
                Pete OHanlon
                wrote on last edited by
                #7

                If the rest of my code is using Linq, I'd use FirstOrDefault just to keep it consistent,

                Deja View - the feeling that you've seen this post before.

                My blog | My articles

                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