Làm cách nào để triển khai chức năng này? Tôi nghĩ rằng nó không hoạt động bởi vì tôi lưu nó trong constructor? Tôi có cần phải làm một số Box/Unbox jiberish?giá trị truyền theo tham chiếu trong hàm tạo, lưu nó, sau đó sửa đổi nó sau, làm thế nào?
static void Main(string[] args)
{
int currentInt = 1;
//Should be 1
Console.WriteLine(currentInt);
//is 1
TestClass tc = new TestClass(ref currentInt);
//should be 1
Console.WriteLine(currentInt);
//is 1
tc.modInt();
//should be 2
Console.WriteLine(currentInt);
//is 1 :(
}
public class TestClass
{
public int testInt;
public TestClass(ref int testInt)
{
this.testInt = testInt;
}
public void modInt()
{
testInt = 2;
}
}
Cảm ơn, đó là thực sự hữu ích. –