在构造函数中通过引用传递值,保存它,然后修改它,怎么做?

发布于 2024-11-18 20:12:44 字数 716 浏览 3 评论 0原文

我如何实现这个功能?我认为它不起作用,因为我将它保存在构造函数中? 我需要做一些装箱/拆箱的胡言乱语吗?

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

    }

How do I implement this functionality? I think its not working because I save it in the constructor?
Do I need to do some Box/Unbox jiberish?

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

    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

愁以何悠 2024-11-25 20:12:45

基本上你不能。不直接。 “按引用传递”别名仅在方法本身内有效。

最接近的是拥有一个可变包装器:

public class Wrapper<T>
{
    public T Value { get; set; }

    public Wrapper(T value)
    {
        Value = value;
    }
}

然后:

Wrapper<int> wrapper = new Wrapper<int>(1);
...
TestClass tc = new TestClass(wrapper);

Console.WriteLine(wrapper.Value); // 1
tc.ModifyWrapper();
Console.WriteLine(wrapper.Value); // 2

...

class TestClass
{
    private readonly Wrapper<int> wrapper;

    public TestClass(Wrapper<int> wrapper)
    {
        this.wrapper = wrapper;
    }

    public void ModifyWrapper()
    {
        wrapper.Value = 2;
    }
}

您可能会在 "ref 返回和 ref 局部变量" 有趣。

You can't, basically. Not directly. The "pass by reference" aliasing is only valid within the method itself.

The closest you could come is have a mutable wrapper:

public class Wrapper<T>
{
    public T Value { get; set; }

    public Wrapper(T value)
    {
        Value = value;
    }
}

Then:

Wrapper<int> wrapper = new Wrapper<int>(1);
...
TestClass tc = new TestClass(wrapper);

Console.WriteLine(wrapper.Value); // 1
tc.ModifyWrapper();
Console.WriteLine(wrapper.Value); // 2

...

class TestClass
{
    private readonly Wrapper<int> wrapper;

    public TestClass(Wrapper<int> wrapper)
    {
        this.wrapper = wrapper;
    }

    public void ModifyWrapper()
    {
        wrapper.Value = 2;
    }
}

You may find Eric Lippert's recent blog post on "ref returns and ref locals" interesting.

箹锭⒈辈孓 2024-11-25 20:12:45

你可以很接近,但这实际上只是乔恩的变相答案:

 Sub Main()

     Dim currentInt = 1

     'Should be 1
     Console.WriteLine(currentInt)
     'is 1

     Dim tc = New Action(Sub()currentInt+=1)

     'should be 1
     Console.WriteLine(currentInt)
     'is 1

     tc.Invoke()

     'should be 2
     Console.WriteLine(currentInt)
     'is 2  :)
 End Sub

You can come close, but it is really just Jon's answer in disguise:

 Sub Main()

     Dim currentInt = 1

     'Should be 1
     Console.WriteLine(currentInt)
     'is 1

     Dim tc = New Action(Sub()currentInt+=1)

     'should be 1
     Console.WriteLine(currentInt)
     'is 1

     tc.Invoke()

     'should be 2
     Console.WriteLine(currentInt)
     'is 2  :)
 End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文