class A
{
public int B { get; set; }
public int C { get; set; }
}
private static void function(A a)
{
if (a == null) a = new A();
a.B = 2;
a.C = 3;
}
A a = new A(); //a is not null
a = null; // a is null
function(a);
//a is still null
If I use C++ analogy what I understand is that although ‘a’ is a pointer points to an object of type ‘A’ which is accessed by the function by reference, the pointer itself is used as value type. The end result is the variable ‘a’ remains null.
So my question is, what if I want to instantiate the variable as if the pointer is passed to the function by reference? Is this doable?
You can pass the variable by reference:
Since you know C++, that's like a
A**
.Consider refactoring the function so that it returns the new value of
a
:If that is truly better depends on the concrete case.