Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Internal Implementation of AsEnumerable() in LINQ

I have two questions:

Question 1 Background : I noticed when looking at the implementation of 'AsEnumerable()' method in LINQ from Microsoft, which was:

public static IEnumerable<TSource> AsEnumerable<TSource>(this IEnumerable<TSource> source)
{ 
   return source;
} 

Question 1: I was expecting some kind of casting or something here , but it simply returns the value it was passed. How does this work ?

Question 2/3 Background : I have been trying to understand Covariance , contravariance and Invariant. I think, I have a vague understanding that 'in' and 'out' keywords determine the polymorphic behavior when assigning a subtype to a parent type.

Question 2: I know from reading that IEnumerable is covariant, and List is invariant then why is this not possible :

List<char> content = "testString".AsEnumerable();

Question 3:
If IList implements IEnumerable then why is this not possible :

IEnumerable<char> content1 = "testString";
IList<char> content2 = content1;

Please help me understanding, thank you in advance.

like image 363
RaM Avatar asked Jul 31 '13 13:07

RaM


Video Answer


1 Answers

  1. The input argument is already known to have the type IEnumerable<TSource>. Why would it need to cast anything? Casting the objects to the type TSource would have no effect, since they're already guaranteed to be of that type (or a more derived type).

  2. You can't assign a value of type IEnumerable<char> to a variable of type List<char>. I think you're thinking in reverse here; List<char> derives from IEnumerable<char>, not the other way around. This has nothing to do with List<T> being invariant. IEnumerable<T> is covariant (to be more precise, the type parameter T is covariant), which gives us this situation:

    IEnumerable enumerable = Enumerable.Empty<string>(); // Allowed
    IEnumerable<string> genericEnumerable = enumerable; // Not allowed
    
  3. Again, IList<char> inherits from IEnumerable<char>, not the other way around. You can do this:

    IList<char> content1 = "testString".ToList();
    IEnumerable<char> content2 = content1;
    

    What you're asking for doesn't make sense, I'm afraid, and it's nothing to do with covariance. The fact that IEnumerable<T> is covariant means that you're allowed to do this:

    IEnumerable<object> asObject = new List<string>() { "test" };
    

    But List<T> is invariant, so you can't do this:

    List<object> asObject = new List<string>() { "test" };
    
like image 93
2 revs Avatar answered Oct 30 '22 08:10

2 revs