i have list<cat>
cat
struct
has id , name.
how change name of cat id 7?
i did (without thinking)
var mycat = catlist.single(c => c.id == 7); mycat.name = "dr fluffykins";
but of course, structs value types. possible use technique this, or have change .single
for
loop , store index replace updated struct?
since struct
s value types, , because value types copied, mycat
ends copy of cat list. need operate on struct
itself, not copy.
moreover, can modify fields of struct
s directly when single variables or parts of array. list<t>
's indexer returns copy, c# compiler produces "cannot modify value type" error.
the solution know (short of making cat
class
or re-assigning modified copy) making catlist
array:
var indexof = catarray .select((cat, index) => new {cat, index}) .single(p => p.cat.id == 7).index; catarray[indexof].name = "dr fluffykins";
Comments
Post a Comment