3

I'm looking to see if putting a .ToList() on an IEnumerable which might or might not be a list will incur a performance hit if the Object is already a List.

My Goal was to look at the source code. and this is where the tunnel of questions begins. First I found SO Question Where can I view LINQ source code? [closed] this leads to the Full Framework version of DOTNet but digging into the code just show a small Enumerable class Enumerable.

Question

Where is the source code for the following Extention Method?

public static List<TSource> ToList<TSource>(this IEnumerable<TSource> source);

Sub-Question 1 Where is the full frameWork Version?

Sub-Question 2 Where is the .net core Version?

Luke Hammer
  • 2,076
  • 3
  • 18
  • 31
  • 2
    If you do this often, have a look at ReSharper, it has tooling to analyse these things. – Stefan Jun 17 '19 at 04:24
  • @Stefan the problem here is the at the input that is coming in is from something I don't control. so the Resharper static analysis would not work in this case – Luke Hammer Jun 17 '19 at 04:37
  • now I need to decide what is better. checking if it is already a list, or doing the enumeration twice. – Luke Hammer Jun 17 '19 at 04:43
  • Actually, it most likely better to just handle as the Enumerable. – Luke Hammer Jun 17 '19 at 04:43
  • 1
    I'd suggest using `IReadOnlyList` rather than `IEnumerable`. This allows consumers to not worry about double enumeration, but makes it clear you don't want them to edit the list. – mjwills Jun 17 '19 at 04:46
  • @mjwills I can see that in the full Frame work source public List ToList() { var list = new List(); foreach (TSource item in _source) { list.Add(_selector(item)); } return list; } – Luke Hammer Jun 17 '19 at 04:47
  • this will move the object to a new list – Luke Hammer Jun 17 '19 at 04:47
  • Answer to Q2: https://source.dot.net/ is a great online tool for .dot net (.net core) source code. The source code for `ToList()` depends upon the type. See https://source.dot.net/#q=Linq%20ToList – Richard Schneider Jun 17 '19 at 04:53
  • 3
    I meant: use the decompiler. – Stefan Jun 17 '19 at 05:36
  • @Stefan OK got it needed to does some setup found here https://www.jetbrains.com/help/resharper/Navigation_and_Search__Navigate_from_Here__Decompiled_Code.html – Luke Hammer Jun 17 '19 at 17:16

2 Answers2

10

Question 1 Where is the full frameWork Version?

https://github.com/microsoft/referencesource/blob/master/System.Core/System/Linq/Enumerable.cs#L947

Question 2 Where is the .net core Version?

https://github.com/dotnet/corefx/blob/8750960d3fafa46a9b838c351e995a01fa8b599f/src/System.Linq/src/System/Linq/ToCollection.cs#L23

Above are links for two ToList implementations (there are others).

They cover .NET Framework and .NET Core.

Where I'm looking to see if putting a .ToList() on an IEnumerable which might or might not be a list will incur a performance hit if the Object is already a List.

It definitely will create a new list, as per the docs:

Creates a List from an IEnumerable.

You can also test this for yourself, by by calling ToList on a List<int>. Then change the new List<int>. The original List<int> isn't changed - so it must have created a new List<int>.

mjwills
  • 23,389
  • 6
  • 40
  • 63
5

https://source.dot.net/ is a great online tool for .dot net (.net core) source code.

The source code for ToList<T>() depends upon the type. See https://source.dot.net/#q=Linq%20ToList

Richard Schneider
  • 34,944
  • 9
  • 57
  • 73
  • This is a Great tool thanks for the heads up. – Luke Hammer Jun 17 '19 at 05:16
  • Here it is: [`Enumerable.ToList`](https://source.dot.net/#System.Linq/System/Linq/ToCollection.cs,e276d6892241255b). What do you mean by *depends upon the type*? The result of the method is always a new `List`. – Theodor Zoulias Jun 17 '19 at 06:13
  • @mjwills all of these `ToList` are internal methods of .NET Core. Only the one I linked is public. – Theodor Zoulias Jun 17 '19 at 07:03
  • @mjwills I don't understand what ❝the source code depends upon the type❞ means. It is nonsensical to me. Btw I upvoted the answer too, for the [link](https://source.dot.net/). – Theodor Zoulias Jun 17 '19 at 07:51