0

Let's say I have the following method, which accepts an IEnumerable and returns an IEnumerable. What implementation does the Linq query actually return? IEnumerable is an interface, and interfaces can't be instantiated, so under the hood it has to be making something. However if you inspect it after return it just looks like an IEnumerable, even at runtime.

public IEnumerable<SelectListItem> CreateDropdownList(IEnumerable<Release> releases)
{
  return releases.Select(r => 
    new SelectListItem { 
      Value = r.UniqRelease.ToString(), 
      Text = r.DescriptionOf
  });
}
Qorbani
  • 5,825
  • 2
  • 39
  • 47
ztorstri
  • 369
  • 6
  • 12
  • 2
    Use `instance.GetType().FullName` or `instance.GetType().AssemblyQualifiedName` in your debugger or as a log statement. – Igor Aug 08 '19 at 18:38
  • 1
    [One of these, depending on what type you pass in](https://referencesource.microsoft.com/#System.Core/System/Linq/Enumerable.cs,38) – canton7 Aug 08 '19 at 18:43

1 Answers1

0

The point in using interfaces is exactly that no users of the methods need to worry about the implementation.

But of course there is a implementation. In LINQ there is usually an extension method that can handle any collection in a generic manner. And specific types might choose to make a local implementation which knows about the inner workings of the collection and thus make a more efficient realization. Given and array it might involve keeping track of an index. In a linked lidt it would likely involve a 'current' pointer and following a 'next' reference on 'current'.

The compiler can even create implementations for you given yield statements.

The point is that the method implementation depends on the class.

faester
  • 14,886
  • 5
  • 45
  • 56
  • Consider this more of an academic inquiry. I know why it's beneficial to return an interface, but was curious what it returned. Based on @Igor's comment it is "System.Linq.Enumerable" – ztorstri Aug 08 '19 at 19:15
  • @ztoratri No, it's a child type of Enumerable – canton7 Aug 08 '19 at 19:28