c# - Impact of IEnumerable.ToList() -


i'm wondering goes on when calling .tolist() on ienumerable in c#. items copied new duplicated items on heap or new list refer original items on heap?

i'm wondering because told me it's expensive call tolist, whereas if it's assigning existing objects new list, that's lightweight call.

i've written fiddle https://dotnetfiddle.net/s7xic2 checking hashcode enough know?

ienumerable doesn't have contain list of anything. can (and does) resolve each current item @ time requested.

on other hand, ilist complete in-memory copy of items.

so answer is... depends. backing ienumerable? if file system yes, calling .tolist can quite expensive. if in-memory list already, no, calling .tolist not terribly expensive.

as example, lets created ienumerable generated , returned random number each time .next called. in case calling .tolist on ienumerable never return, , throw out of memory exception.

however, ienumerable of database objects has finite bounds (usually :) ) , long data fits in memory, calling .tolist entirely appropriate.


Comments

Popular posts from this blog

android - Gradle sync Error:Configuration with name 'default' not found -

java - Andrioid studio start fail: Fatal error initializing 'null' -

html - jQuery UI Sortable - Remove placeholder after item is dropped -