Assuming my Awesome class has a Bazinga property, which is a String with a default value of "Default", I could go:
var enumerableOfAwesome = Enumerable .Range(1, 5) .Select(n => new Awesome()); foreach (var a in enumerableOfAwesome) a.Bazinga = "New value";
As you already know, recounting enumerableOfAwesome again will not make you happy if you expect to find the "New Value" strings safely hidden in all of Bazinga . Instead, their output to the console will look like this:
Default value
Default value
Default value
Default value
Default value
(.NET Fiddle here)
This is all good and dandy from some deferred point of execution, and this issue has already been discussed earlier, for example, here and. However, my question is why enumerator implementations do not return immutable objects; If perseverance over several enumerations is not guaranteed, then what is their ability to set values, except that people get confused?
This may not be a good question, but I'm sure your answers will be helpful.
source share