如何通过类的其他方法更改传递的引用变量的值。 我已将评论嵌入代码段中。
class A
{
int myVar = 0;
void SomeMethodOfA ()
{
B obj = new B(ref myVar);
Console.WriteLine(myVar); // Need updated value of myVar
}
}
class B
{
int newVar; // Intentionally not initialised.
public B()
{
//something
}
public B(ref int x) : this()
{
// Purpose is to change the value of 'x' by the other method's of this class.
newVar = x; //This is not assigning newVar the reference of x
// What I am missing here...?
}
private void buttonSomething_Click(object sender, EventArgs e)
{
newVar++; //This change in only local.
// I need this change to reflect in 'x'
}
}