c# - Some updates are discarded out of a method -
i have set of objects inherits 1 baseobject have global primitive properties (indexes etc). now, have 2 objects, 1 of them (the target one) have values base properties , other (the source, 1 of set of objects) have values other properties (retrieved 3rd party app), base one's.
i'm trying copy source object's properties target one, keep target's base properties' values. in other words – i'm trying equal 2 object's properties' values without deleting anything… target = source; delete target's base indexes… so, made method gets arguments 2 objects (casted baseobject) , copy target's indexes's values source befor copy, this:
public void copy(baseobject source, baseobject target) { //copy primitive indexes here... source.index = target.index; source.reference = target.reference; etc… //copy updated object target one... target = source; }
it seems ok in debug mode inside method, – when code exits method surprised see although source object correctly updated both, inherited , non-inherited properties, target 1 values left unchanged. so, had copy (updated) source target again, outside method, this:
inheritedobject sourceobj = createobjectwithprimitiveindexes(); inheritedobject targetobj = getobjectwithnoindexesfrom3rdparty(); targetobj.copy(source: sourceobj, target: targetobj); //the targetobject not updated, have re-copy outside copy() method targetobj = sourceobj;
can explain me why sourceobj updated, sent copy() method ref, target obj behave sent val , of updates ignored outside method…???
sould use 'ref', 'out' key words nthe method signature etc??
if assign parameter of method, assignment visible caller, parameter must have ref
(or out
) modifier. see ref (c# reference).
example:
// doesn't anything! void copy(baseobject target) { ... target = something; } // ref, assignment *same* variable caller gave void copy(ref baseobject target) { ... target = something; }
addition:
as link provided notes:
do not confuse concept of passing reference concept of reference types
these 2 concepts "orthogonal", following table illustrates:
| | | byval (neither | byref (ref or | ref nor out): | out keyword): | | --------------------------------------------+-------------------+---------------------- value type (struct, enum) | entire object | no copy, argument | copied | must variable, | | same variable used --------------------------------------------+-------------------+---------------------- reference type (class, interface, delegate) | reference copied; | no copy, argument | new reference | must variable, | same object | same variable used --------------------------------------------+-------------------+----------------------
if don't use mutable structs, variables of struct type can ever change through re-assignemnt, , following rule-of-thumb useful both value types , reference types:
you need
ref
(orout
) keyword if , if method assigns (including compund assignment+=
) parameter in question.
Comments
Post a Comment