If I pass an object to a method and change its properties, does the caller see the change?
Quick Answer
Yes for mutating the object's members (both variables point to the same instance). No for reassigning the parameter to a new object — that only changes the local copy of the reference, unless the parameter is ref.
It depends on what you do. If you change a property on the object — o.Total = 100 — yes, the caller sees it, because you both have a reference to the same object. If you reassign the parameter itself — o = new Order() — no, because C# passed a copy of the reference and you just repointed the copy. To make a reassignment visible you'd need ref. For a value type like a struct or an int, the whole thing is copied, so nothing you do inside the method is visible unless it's a ref parameter.
void Update(Order o) { o.Total = 100; }— caller seesTotal == 100(same object).void Replace(Order o) { o = new Order(); }— caller's variable unchanged (reassigned the copy).void Replace(ref Order o) { o = new Order(); }— now the caller's variable is replaced.- Value types (
struct,int) are copied entirely, so neither mutation nor reassignment is visible withoutref.