l 按值传递&通过引用传递

发布于 2024-09-03 11:23:34 字数 823 浏览 10 评论 0原文

您能否解释一下 C# 类的以下行为。我期望 classResult 为“Class Lijo”;但实际值已“更改”。

我们正在制作参考文献的副本。尽管副本指向相同的地址,但接收参数的方法不能更改原始地址。

那么为什么值会改变呢?

public partial class _Default : Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        String nameString = "string Lijo";

        Person p = new Person();
        p.Name = "Class Lijo";

        Utilityclass.TestMethod(nameString, p);
        string classResult = p.Name;
        Response.Write(nameString + "....." + classResult);
    }
}

public class Utilityclass
{
    public static void TestMethod(String nameString, Person k)
    {
        nameString = "Changed";
        k.Name = "Changed";
    }
}

public class Person
{
    public string Name
    {
        get; set;
    }
}

更新:当我传递一个字符串时,它实际上并没有改变。

Could you please explain the following behavior of C# Class. I expect the classResult as "Class Lijo"; but actual value is “Changed”.

We’re making a copy of the reference. Though the copy is pointing to the same address, the method receiving the argument cannot change original.

Still why the value gets changed ?

public partial class _Default : Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        String nameString = "string Lijo";

        Person p = new Person();
        p.Name = "Class Lijo";

        Utilityclass.TestMethod(nameString, p);
        string classResult = p.Name;
        Response.Write(nameString + "....." + classResult);
    }
}

public class Utilityclass
{
    public static void TestMethod(String nameString, Person k)
    {
        nameString = "Changed";
        k.Name = "Changed";
    }
}

public class Person
{
    public string Name
    {
        get; set;
    }
}

Update: When I pass a String, it does not get actually changed.

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

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

发布评论

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

评论(6

不再让梦枯萎 2024-09-10 11:23:34

最简短的答案是:阅读我关于参数传递的文章,其中公平地讨论了这一点详细程度。

稍微长一点的答案是比较这两种方法,这两种方法都使用参数:

public void ChangeMe(string x)
{
    x = "changed";
}

public void ChangeMe(Person x)
{
    x.Name = "changed";
}

在第一种情况下,您正在更改参数的。这与最初的论点完全无关。您无法更改字符串本身的内容,因为字符串是不可变的。

在第二种情况下,您将更改参数值所引用的对象的内容。这不会改变参数本身的值 - 它将是相同的引用。举一个现实世界的例子,如果有人送东西到你家,改变了你家的内容,但它不会改变你家的地址。

如果您将第二个方法更改为:

public void ChangeMe(Person x)
{
    x = new Person("Fred");
}

那么调用者将看不到任何更改。这更接近于您对字符串所做的事情 - 您使参数引用不同的对象,而不是更改现有对象的内容。

现在,当您使用 ref 参数时,调用者用作参数的变量将与该参数“别名” - 因此,如果您更改参数的值,也会更改参数的值以及。因此,如果我们像这样更改最后一个方法:

public void ChangeMe(ref Person x)
{
    x = new Person("Fred");
}

那么:

Person y = new Person("Eric");
ChangeMe(ref y);
Console.WriteLine(y.Name);

这将打印出“Fred”。

要理解的关键概念是变量的值永远不是对象 - 它要么是值类型值,要么是引用。如果对象的数据发生更改,则该更改将通过其他引用可见。一旦您了解复制引用与复制对象不同,其余的事情就很容易实现了。

The briefest answer is: read my article on parameter passing which goes into this in a fair amount of detail.

The slightly longer answer is to compare these two methods, both of which use value parameters:

public void ChangeMe(string x)
{
    x = "changed";
}

public void ChangeMe(Person x)
{
    x.Name = "changed";
}

In the first case, you are changing the value of the parameter. That is completely isolated from the original argument. You can't change the content of the string itself, because strings are immutable.

In the second case, you are changing the contents of the object which the parameter's value refers to. That's not changing the value of the parameter itself - it will be the same reference. To give a real world example, if someone delivers something to your house that changes the contents of your house, but it doesn't change your house's address.

If you changed the second method to this:

public void ChangeMe(Person x)
{
    x = new Person("Fred");
}

then the caller wouldn't see any change. This is closer to what you're doing with a string - you're making the parameter refer to a different object, rather than changing the contents of the existing object.

Now, when you use a ref parameter, the variable used by the caller as the argument is "aliased" with the parameter - so if you change the value of the parameter, that changes the value of the argument as well. So if we change the last method like this:

public void ChangeMe(ref Person x)
{
    x = new Person("Fred");
}

then:

Person y = new Person("Eric");
ChangeMe(ref y);
Console.WriteLine(y.Name);

this will print out "Fred".

The key concept to understand is that the value of a variable is never an object - it's either a value type value or a reference. If an object's data is changed, that change will be visible through other references. Once you understand that copying a reference isn't the same as copying an object, the rest falls into place reasonably easily.

埋葬我深情 2024-09-10 11:23:34

Person 是引用类型,因此无论您使用 refout 还是什么都不使用,您始终可以在方法内部对其进行修改。您永远不会将真实的 person 对象传递给该方法,而是将指针作为引用传递,而不是实际的 Personref 关键字对于值类型(例如结构、int、float、DateTime 等)非常有用。 它也可以与引用类型一起使用,但只能指示行为,但不能强制执行它。如果将它与引用类型一起使用,它允许您更改此引用指向的对象。

Person is a reference type, so no matter whether you use ref, out or nothing, you will always be able to modify it inside the method. You never pass the real person object to the method, you are passing the pointer as reference but not the actual Person. The ref keyword is useful with value types (such as structs, int, float, DateTime, ...). It could also be used with reference types but only to indicate behavior but cannot enforce it. If you use it with reference types it allows you to change the object this reference is pointing to.

爱你是孤单的心事 2024-09-10 11:23:34

当您将 P 传递给测试方法时,您传递的是它在内存中的位置,而不是对象的副本。在方法主体中获取引用并修改原始值。

When you pass P to a test method you pass its location in the memory, rather than the copy of the object. Reference gets picked up in the body of the method and the original value gets modified.

云淡月浅 2024-09-10 11:23:34

Utilityclass.TestMethod 无法更改局部变量 p 以指向不同的 Person 对象,因为您没有通过引用传递,但它仍然是免费的调用任何方法或更改所传递的对象的任何属性。因此可以在 Utilityclass.TestMethod 中修改 Name 属性。

Utilityclass.TestMethod cannot change the local variable p to point to a different Person object since you are not passing by reference, but it is still free to call any methods or change any properties on the object it is passed. So the Name property can be modified within Utilityclass.TestMethod.

一抹淡然 2024-09-10 11:23:34

这个问题已经大部分得到了回答,但我想你可能想尝试一下这个片段(如果你用整数尝试的话,会加分!)

class Program
{
    static void Main(string[] args)
    {            
        Person p = new Person();
        p.Name = "Class Lijo";

        Utilityclass.TestMethod(p);
        string classResult = p.Name;
        Console.WriteLine(classResult);
        Utilityclass.TestMethod2(ref p);
        classResult = p.Name;  // will bomb here           
        Console.WriteLine(classResult);
    }
}

public class Utilityclass
{
    public static void TestMethod(Person k)
    {
        k.Name = "Changed";
        k = null;
    }

    public static void TestMethod2(ref Person k)
    {
        k.Name = "Changed Again!";
        k = null;
    }
}

This question has been mostly answered, but I think you might like to try out this snippet (bonus points if you try this out with ints!)

class Program
{
    static void Main(string[] args)
    {            
        Person p = new Person();
        p.Name = "Class Lijo";

        Utilityclass.TestMethod(p);
        string classResult = p.Name;
        Console.WriteLine(classResult);
        Utilityclass.TestMethod2(ref p);
        classResult = p.Name;  // will bomb here           
        Console.WriteLine(classResult);
    }
}

public class Utilityclass
{
    public static void TestMethod(Person k)
    {
        k.Name = "Changed";
        k = null;
    }

    public static void TestMethod2(ref Person k)
    {
        k.Name = "Changed Again!";
        k = null;
    }
}
戏舞 2024-09-10 11:23:34

当您将引用类型参数传递给方法时,这意味着该方法可以直接访问该参数而不是它的副本......

因此结果已更改。

When you pass a reference type argument to a method, this means that the method has direct access to that argument not to a copy of it....

So the result is Changed.

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