-1

Take a look for example at the accepted answer here Which method performs better: .Any() vs .Count() > 0?. Now I wonder if LINQ functions (e.g. Last, FirstOrDefault) are completely naive and all they do is enumerating/checking IEnumerable or they check what data structure is beneath (at least for basic collections like Array, List) and the execution is optimized for such structures?

Community
  • 1
  • 1
greenoldman
  • 16,895
  • 26
  • 119
  • 185
  • 3
    To save you *wondering*, http://referencesource.microsoft.com/#System.Core/Microsoft/Scripting/Utils/CollectionExtensions.cs,172d0a44454c0d97 – Christian Phillips May 24 '15 at 09:34
  • If you have read the accepted answer there you know it does so at least some of the time. And sometimes not. – H H May 24 '15 at 09:35
  • @christiandev, thank you, weird those extensions are marked as `internal` though. – greenoldman May 24 '15 at 09:45
  • @HenkHolterman, of course I am not interested in particular case, but general rule. But it seems, the rule is "there is no rule" :-). – greenoldman May 24 '15 at 09:46

1 Answers1

1

They do where it makes sense, typically not for the actual type but for an interface (for example they check for IList when it helps speedup)

Ronan Thibaudau
  • 3,413
  • 3
  • 29
  • 78