I know a lot about C #, but that pushes me, and Google doesn't help.
I have an IEnumerable range of objects. I want to set the property on the first. I do this, but when I list the range of objects after modification, I do not see the changes.
Here is a good example of a problem:
public static void GenericCollectionModifier()
{
var range = Enumerable.Range(1, 10);
var items = range.Select(i => new SubItem() {Name = "foo", MagicNumber = i});
Write(items);
items.First().MagicNumber = 42;
Write(items);
}
public static void Write(IEnumerable<SubItem> items)
{
Console.WriteLine(string.Join(", ", items.Select(item => item.MagicNumber.ToString()).ToArray()));
}
public class SubItem
{
public string Name;
public int MagicNumber;
}
What aspect of C # stops changing my "MagicNumber = 42"? Is there a way I can force my changes to stick without doing any funky conversions to List <> or array?
Thank! -Mike
source
share