C# - 通过引用传递参数的构造函数,然后使用他们的方法 [英] C# - passing parameters by reference to constructor then using them from method

查看:222
本文介绍了C# - 通过引用传递参数的构造函数,然后使用他们的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的代码,我想有从类中的方法(工作)TestClass的改变在主程序中的一些变量的值,而无需归还。这些变量由TestClass的构造函数引用传递。

In the following code, I am trying to have a method(Work) from class TestClass change the values of some variables in the main program without having to return them. The variables are passed by reference in the TestClass constructor.

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();
    }
}

public class TestClass
{
    int _a, _b, _c, _d;
    public TestClass(ref int a, ref int b, ref int c, ref int d)
    {
        _a = a; _b = b; _c = c; _d = d;
    }

    public void Work()
    {
        Console.WriteLine("Work before changing: a=" + _a + " b=" + _b + " c=" + _c + " d=" + _d);
        _a = 0; _b = 1; _c = 2; _d = 3;
        Console.WriteLine("Work after changing: a=" + _a + " b=" + _b + " c=" + _c + " d=" + _d);
    }
}



不过这个代码返回:

However this code returns :

Main before TestClass: a=5 b=10 c=20 d=25
Work before changing: a=5 b=10 c=20 d=25
Work after changing: a=0 b=1 c=2 d=3
Main after TestClass: a=5 b=10 c=20 d=25

有什么办法能有方法改变主程序中变量的值?
谢谢!

Is there any way to have the method change the values of the variables in the Main program? Thank you!

推荐答案

您会为了更好创造过的Int32自己的封装更改得以体现的,因为一旦这些值被分配给类的字段,它们不再引用,但为Int32而不同的实例。考虑下面的代码:

You would be better off creating your own wrappers over Int32 in order for changes to be reflected, because once the values are assigned to fields of the class, they are no longer references, but rather different instances of Int32. Consider the following code:

class Integer {
    public int Value;

    public Integer(int value) {
        Value = value;
    }

    public override string ToString() {
        return Value.ToString();
    }
}

class TestClass {
    Integer _a, _b, _c, _d;

    public TestClass(Integer a, Integer b, Integer c, Integer d) {
        _a = a;
        _b = b;
        _c = c;
        _d = d;
    }

    public void Work() {
        _a.Value = 111;
        _b.Value = 222;
        _c.Value = 333;
        _d.Value = 444;
    }
}



所以,现在你有一个整数 - 一个包装类以上的Int32。用法将为您带来的结果:

So now you have an Integer -- a wrapper class over Int32. The usage will bring you the results:

Integer a = new Integer(0), b = new Integer(0), c = new Integer(0), d = new Integer(0);
Console.WriteLine("a: {0}, b: {1}, c: {2}, d: {3}", a, b, c, d);
new TestClass(a, b, c, d).Work();
Console.WriteLine("a: {0}, b: {1}, c: {2}, d: {3}", a, b, c, d);



输出是:

The output is:

a: 0, b: 0, c: 0, d: 0
a: 111, b: 222, c: 333, d: 444

您也可以发现它有用在C#中,如阅读更多关于类和结构 http://msdn.microsoft.com/en-us/library/ms173109.aspx 。 (的Int32是一个结构,而在你的情况,你可能需要一个类)

You may also find it useful to read more about classes and structs in C#, e.g. http://msdn.microsoft.com/en-us/library/ms173109.aspx . (Int32 is a struct, whereas in your case you probably need a class)

这篇关于C# - 通过引用传递参数的构造函数,然后使用他们的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆