c# - ForEach loop not changing property of class -
i've seen couple questions this, , done research.
my understanding when run foreach on ienumerable: if t reference type (e.g. class) should able modify properties of object within loop. if t value type (e.g. struct) not work since iteration variable local copy.
i working on windows store app next code:
my class:
public class webresult { public string id { get; set; } public string title { get; set; } public string description { get; set; } public string displayurl { get; set; } public string url { get; set; } public string tilecolor { { string[] colorarray = { "ffa200ff", "ffff0097", "ff00aba9", "ff8cbf26", "ffa05000", "ffe671b8", "fff09609", "ff1ba1e2", "ffe51400", "ff339933" }; random random = new random(); int num = random.next(0, (colorarray.length - 1)); homecoming "#" + colorarray[num]; } } public string keywords { get; set; } }
the code:
ienumerable<webresult> results = r in doc.descendants(xmlnsm + "properties") select new webresult { id = r.element(xmlns + "id").value, title = r.element(xmlns + "title").value, description = r.element(xmlns + "description").value, displayurl = r.element(xmlns + "displayurl").value, url = r.element(xmlns + "url").value, keywords = "setting keywords here" }; foreach (webresult result in results) { result.keywords = "these, are, my, keywords"; } if (control gridview) { (control gridview).itemssource = results; }
once results displayed "keywords" property "setting keywords here". if set break point in foreach loop can see results object not getting modified...
any ideas going on? missing obvious? ienumerable behave differently in .net windows store apps?
this known deferred execution
; results
query executed every time iterate on it. in case it's evaluated twice, 1 time in loop, , sec time when it's databound.
you can verify doing this
var results2 = results.tolist(); foreach (webresult result in results2) { result.keywords = "these, are, my, keywords"; } if (control gridview) { (control gridview).itemssource = results2; }
you should see changes persisted.
c# foreach windows-store-apps ienumerator
No comments:
Post a Comment