c# - Reference of object in array not kept -
i having problem reference object in list lost, how elaborated code :
propertyobject[] myproperties = new propertyobject[200]; var objparent = new parent(); var objproperty = new propertyobject(); myproperties[0] = objproperty; objparent.property = myproperties[0];
now when modify objparent.property
not modify object in myproperties
array, workaround? need don't have iterate on array.
this how modify object :
public void modifyobject(ref parent objecttomodify) { objecttomodify.property.isthiscrazy = true; }
then invoke modifyobject
method.
struct
s meant immutable. assinging struct
variable cause struct
copied.
when assigning properties on 1 instance, properties of other other instance of struct
aren't changed. hence, don't see updated in other reference.
sample code demonstrating problem structs:
struct x { public string y { get; set; } public x(string y) : this() { y = y; } } x x = new x("abc"); x x2 = x; x2.y = "def"; console.writeline(x.y); console.writeline(x2.y);
with classes you'd expected x.y
, x2.y
same, not structs.
Comments
Post a Comment