在下面的代码中,我试图从类TestClass中获取一个方法(Work)来更改主程序中某些变量的值,而不必返回它们.变量通过TestClass构造函数中的引用传递.
class Program
{
static void Main(string[] args)
{
int a, b, c, d;
a = 5; b = 10; c = 20; d = 25;
Console.WriteLine("Main before TestClass: a=" + a + " b=" + b + " c=" + c + " d=" + d);
TestClass testObj = new TestClass(ref a,ref b,ref c,ref d);
testObj.Work();
Console.WriteLine("Main after TestClass: a=" + a + " b=" + b + " c=" + c + " d=" + d);
Console.ReadLine();
} …Run Code Online (Sandbox Code Playgroud)