c# - Implementation of WhereListIterator.ToList() -


in piece of code like

list<int> foo = new list<int>() { 1, 2, 3, 4, 5, 6 }; ienumerable<int> bar = foo.where(x => x % 2 == 1); 

bar of type system.linq.enumerable.wherelistiterator<int> due deferred execution. since implements ienumerable<int> possible convert list<int>using tolist(). however, have been unable identify parts of code run when tolist() called. using dotpeek decompiler , first time attempting such thing, correct me if made mistakes on way.

i describe found far below (all assemblies version 4.0.0.0):

  1. enumerable.wherearrayiterator<tsource> implemented in file enumerable.cs of namespace system.linq in assembly system.core. class neither defines tolist() nor implement ienumerable<tsource>. implements enumerable.iterator<tsource> located in same file. enumerable.iterator<tsource> implement ienumerable<tsource>.

  2. tolist() extension mewthod located in enumerable.cs. null checking , calling constructor of list<tsource> argument.

  3. list<t> defined in file list.cs of namespace system.collections.generic in assembly mscorlib. constructor called tolist() has signature public list(ienumerable<t> collection). once again null checks , casts argument icollection<t>. if collection has no elements, creates new list of empty array, otherwise uses icollection.copyto() method create new list.

  4. icollection<t> defined in mscorlib \ system.collections.generic \ icollection.cs. implements ienumerable in generic , non-generic form.

this stuck. neither enumerable.wherearrayiterator<tsource> nor enumerable.iterator<tsource> implement icollection, somewhere, cast has happen , unable locate code run when copyto() called.

i think you're getting confused as operator. it's safe cast. it's equivalent this, bit faster:

myendtype x = null; if (myvarwithas myendtype) x = (myendtype)myvarwithas; 

now, let's @ code again now.

 public list(ienumerable<t> collection)     {       if (collection == null)         throwhelper.throwargumentnullexception(exceptionargument.collection);       icollection<t> collection1 = collection icollection<t>;       if (collection1 != null)       {         int count = collection1.count;         if (count == 0)         {           this._items = list<t>._emptyarray;         }         else         {           this._items = new t[count];           collection1.copyto(this._items, 0);           this._size = count;         }       }       else       {         this._size = 0;         this._items = list<t>._emptyarray;         foreach (t obj in collection)           this.add(obj);       }     } 

as can see, in if checks if it's null. if it's null, means not icollection<t>, then goes else. else set default, , then adds in manually. when pass in ienumerable<t> not icollection<t> (like in example) go through else path.


Comments

Popular posts from this blog

How to mention the localhost in android -

php - Calling a template part from a post -

c# - String.format() DateTime With Arabic culture -