c# - editing the iteration value in a foreach loop -
situation
i'm trying design simple remove method deletes object if incoming id parameter matches id on record:
for instance, if race had id "123" find race had id "123" , set null, removing it.
i've looked @ few methods online , yeah, seems recurring problem foreach specifically, i'd way if possible.
public void removerace(string raceid) { foreach (baserace br in races) { //if id entered method parameter matches id on record. if (raceid == br.raceid) { //set current id null, deleting record. //br = null; } } }
problem
my code, br = null;
doesn't work, since foreach loop won't let me edit iterator.
how can solve issue in way?
list<t>
provides method that:
public void removerace(string raceid) { races.removeall(br => raceid == br.raceid); }
if don't want use you'd have use for
loop instead of foreach
.
Comments
Post a Comment