how to change element of struct in a list c#? -
this question has answer here:
public struct rectangle { public decimal a; }
that struct , have list of struct rectangles
list<rectangle> datarectangle= new list<rectangle>();
now have added structs list change value of struct present in list
datarectangle[0].a=0;
it says
cannot modify return value ' list<form4.rectangle>.this[int] ' because not variable
help why coming , there way modify
a struct
value type. in contrast reference type class
don't pass around references instance, pass around value itself.
so returned indexer datarectangle[0]
not reference struct
copy of struct
. , it's passed on stack no variable itself.
to solve this, can either declare rectangle
class
(which prefer mutable type:
public struct rectangle { public decimal {get; set;} }
or (as ugly looks) have reassignment like
datarectangle[0] = new rectangle{a=0};
Comments
Post a Comment