c# - Internal Implementation of AsEnumerable() in LINQ -


i have 2 questions:

question 1 background : noticed when looking @ implementation of 'asenumerable()' method in linq microsoft, was:

public static ienumerable<tsource> asenumerable<tsource>(this ienumerable<tsource> source) {     return source; }  

question 1: expecting kind of casting or here , returns value passed. how work ?

question 2/3 background : have been trying understand covariance , contravariance , invariant. think, have vague understanding 'in' , 'out' keywords determine polymorphic behavior when assigning subtype parent type.

question 2: know reading ienumerable covariant, , list invariant why not possible :

list<char> content = "teststring".asenumerable(); 

question 3:
if ilist implements ienumerable why not possible :

ienumerable<char> content1 = "teststring"; ilist<char> content2 = content1; 

please me understanding, thank in advance.

  1. the input argument known have type ienumerable<tsource>. why need cast anything? casting objects type tsource have no effect, since they're guaranteed of type (or more derived type).

  2. you can't assign value of type ienumerable<char> variable of type list<char>. think you're thinking in reverse here; list<char> derives ienumerable<char>, not other way around. has nothing list<t> being invariant. ienumerable<t> covariant (to more precise, type parameter t covariant), gives situation:

    ienumerable enumerable = enumerable.empty<string>(); // allowed ienumerable<string> genericenumerable = enumerable; // not allowed 
  3. again, ilist<char> inherits ienumerable<char>, not other way around. can this:

    ilist<char> content1 = "teststring".tolist(); ienumerable<char> content2 = content1; 

    what you're asking doesn't make sense, i'm afraid, , it's nothing covariance. fact ienumerable<t> covariant means you're allowed this:

    ienumerable<object> asobject = new list<string>() { "test" }; 

    but list<t> invariant, can't this:

    list<object> asobject = new list<string>() { "test" }; 

Comments

Popular posts from this blog

c++ - Creating new partition disk winapi -

Android Prevent Bluetooth Pairing Dialog -

VBA function to include CDATA -