小编pan*_*tra的帖子

C# - 通过引用传递参数到构造函数,然后从方法中使用它们

在下面的代码中,我试图从类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)

c# methods constructor reference parameter-passing

8
推荐指数
1
解决办法
1万
查看次数

标签 统计

c# ×1

constructor ×1

methods ×1

parameter-passing ×1

reference ×1