在调用的函数中修改的C#对象属性会影响调用者的值 [英] C# Object property modified in the called function affect caller value

查看:215
本文介绍了在调用的函数中修改的C#对象属性会影响调用者的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用以下定义定义了一个简单的类。
在此示例中,我定义了一个带有单个字符串参数的简单类。然后,我实例化了该类,并为该参数分配了一个值。我将该实例传递给另一个函数,而未指定 ref关键字。这意味着应该通过值而不是引用来传递它。但是我不明白的原因是输出结果显示第二次修改而不是第一次修改。

I defined a simple class with the following definition. In this sample, I defined a simple class with a single string parameter. Then I instantiated this class and assigned a value to the parameter. I passed that instance to another function without specifying "ref" keyword. It means it should be passed by value instead of reference. But what I don't understand is the reason that the output displays "Second Modification" value as a result instead of "First Modification".

我认为我的问题令人困惑。我知道引用和通过引用传递的工作方式。我需要知道为什么当我按值传递时,运行时仍会更改原始实例的值。顺便说一下,我在建议的链接中找不到我的答案。

I think my question was confusing. I know how "ref" and "pass by reference" working. I need to know why when I "pass by value", run-time still changes the value of the original instance. By the way, I couldn't find my answer in the proposed links.

I两年前问这个问题。现在,我了解对象的所有属性(类的实例)都是引用类型。这意味着当我将一个类的实例传递给另一个方法时,它们都充当对该类原始成员的引用。

I asked this question two years ago. Now I understand all properties of an object (instance of a class) are reference type. It means when I pass an instance of a class to another method, they all behave as a reference to original member of the class.

public class MyClass
{
    public String TestProperty { get; set; }
}

public class TestClass
{
    public TestClass()
    {
        var myClass = new MyClass();
        myClass.TestProperty = "First Modification";
        MyFunction(myClass);
        Console.WriteLine(myClass.TestProperty); // Output: "Second Modification"
    }
    void MyFunction(MyClass myClass)
    {
        myClass.TestProperty = "Second Modification";
    }
}


推荐答案

您可以更改传递给该方法的对象,但是如果没有 ref 关键字

You can change the object passed to the method, but you can't change the reference itself without the ref keyword

public class MyClass
{
    public String TestProperty { get; set; }
}

public class TestClass
{
    public TestClass()
    {
        var myClass = new MyClass();
        myClass.TestProperty = "First Modification";

        MyFunction(myClass);
        Console.WriteLine(myClass.TestProperty); // Output: "First Modification"

        MyRefFunction(ref myClass);
        Console.WriteLine(myClass.TestProperty); // Output: "Third Modification"
    }
    void MyFunction(MyClass myClass)
    {
        myClass = new MyClass() { TestProperty = "Second Modification"};
    }

    void MyRefFunction (ref MyClass myClass)
    {
        myClass = new MyClass() { TestProperty = "Third Modification"};
    }
}

如果要防止更改属性,您可以可以使设置器成为非公开的:

If you want to prevent the property being changed, you can make the setter non-public:

public String TestProperty { get; private set; }

这篇关于在调用的函数中修改的C#对象属性会影响调用者的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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