I am trying to update the variable ref container
that was passed via constructor to set to equal to a new object in a Run
method. Of course, in Run
method I am updating the reference of to Foo::_container
not the variable container
that was defined in Main
. So I am wondering if there is a way to save the container reference that was passed via constructor and subsequently update the value of reference. Thank you.
public class Foo
{
private IContainer _container;
public Foo(ref IContainer container)
{
_container = container;
}
public void Run()
{
var temp = new Container();
_container = temp;
}
}
static class Main
{
static void Main()
{
IContainer container = null;
(new Foo(ref container)).Run();
// container is still null
Console.WriteLine(container);
}
}