c# - Why are IEnumerable Projections not Immutable? -


assuming awesome class has property bazinga, string default value of "default value", go:

    var enumerableofawesome = enumerable         .range(1, 5)         .select(n => new awesome());      foreach (var in enumerableofawesome)         a.bazinga = "new value"; 

as may aware, enumerating enumerableofawesome again not make happy, if expect find "new value" strings safely tucked away in bazinga. instead, outputting them console render:

default value
default value
default value
default value
default value

(.net fiddle here)

this , dandy deferred execution point-of-view, , issue has been discussed before, here , here. question why enumerator implementations not return immutable objects; if persistence on several enumerations not guaranteed, use there of ability set values, save making people confused?

maybe isn't question, i'm sure answers enlightening.

my question why enumerator implementations not return immutable objects

how expect them that? they're sequences of whatever type used - expect happen like:

var builders = new string[] { "foo", "bar" }.select(x => new stringbuilder(x)); 

? expect (whatever "it" in case) create new type automatically same api stringbuilder, magically making immutable? how detect mutations?

basically, you're asking infeasible - can have sequence element type mutable, while iterating through can mutate object yielded reference refers to. expecting else unrealistic, imo.


Comments

Popular posts from this blog

python - TypeError: start must be a integer -

c# - DevExpress RepositoryItemComboBox BackColor property ignored -

django - Creating multiple model instances in DRF3 -