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.
the input argument known have type
ienumerable<tsource>. why need cast anything? casting objects typetsourcehave no effect, since they're guaranteed of type (or more derived type).you can't assign value of type
ienumerable<char>variable of typelist<char>. think you're thinking in reverse here;list<char>derivesienumerable<char>, not other way around. has nothinglist<t>being invariant.ienumerable<t>covariant (to more precise, type parametertcovariant), gives situation:ienumerable enumerable = enumerable.empty<string>(); // allowed ienumerable<string> genericenumerable = enumerable; // not allowedagain,
ilist<char>inheritsienumerable<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
Post a Comment