List<T>.Find() vs Enumerable.FirstOrDefault()
-
I have a List<T> object and I'm wondering what the difference is between calling
List<T>.Find()
andEnumerable.FirstOrDefault()
. Which one should I use? Thanks in advance! AlChristianity: 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.
-
I have a List<T> object and I'm wondering what the difference is between calling
List<T>.Find()
andEnumerable.FirstOrDefault()
. Which one should I use? Thanks in advance! AlChristianity: 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.
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.
-
I have a List<T> object and I'm wondering what the difference is between calling
List<T>.Find()
andEnumerable.FirstOrDefault()
. Which one should I use? Thanks in advance! AlChristianity: 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.
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.
-
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.
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.
-
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.
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.
-
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.
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.
-
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.
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.