In the class fooBase
there is SomeProperty
. In the class barBase
, I have a field of type fooBase
:
public class fooBase
{
public object SomeProperty { get; set; }
}
public class barBase
{
protected fooBase _foo;
}
It's clear that from barBase I can change SomeProperty by _foo.SomeProperty = whatever;
.
Now in the derived class fooChild
there is some unimportant logic on which the derived class barChild
operates. The barChild constructor gets an instance of fooChild and stores it.
public class fooChild : fooBase { /* someLogic */ }
public class barChild : barBase
{
public barChild(fooChild foo)
{
_foo = foo; // foo of [fooChild] type stored in _foo of [fooBase] type.
}
protected fooChild _getFoo // cast via 'as' to access fooChild logic
{ get { return _foo as fooChild; } }
}
Now to use the logic of fooChild, I need to access _foo as fooChild
(which _getFoo
does).
Question: Will the get { return ... as ... }
create a local copy of _foo, so that when I call SomeFunction()
in a derived class of barChild, the property change SomeProperty will not happen in the barBase._foo?
public class somewhereElse : barChild
{
public void SomeFunction()
{
_getFoo.SomeProperty = new object();
// now barBase:_foo.SomeProperty is still old object?
}
}
If yes, how can I avoid that?
If no, how can I tell?