l 按值传递&通过引用传递
您能否解释一下 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
最简短的答案是:阅读我关于参数传递的文章,其中公平地讨论了这一点详细程度。
稍微长一点的答案是比较这两种方法,这两种方法都使用值参数:
在第一种情况下,您正在更改参数的值。这与最初的论点完全无关。您无法更改字符串本身的内容,因为字符串是不可变的。
在第二种情况下,您将更改参数值所引用的对象的内容。这不会改变参数本身的值 - 它将是相同的引用。举一个现实世界的例子,如果有人送东西到你家,改变了你家的内容,但它不会改变你家的地址。
如果您将第二个方法更改为:
那么调用者将看不到任何更改。这更接近于您对字符串所做的事情 - 您使参数引用不同的对象,而不是更改现有对象的内容。
现在,当您使用
ref
参数时,调用者用作参数的变量将与该参数“别名” - 因此,如果您更改参数的值,也会更改参数的值以及。因此,如果我们像这样更改最后一个方法:那么:
这将打印出“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:
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:
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:then:
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.
Person
是引用类型,因此无论您使用ref
、out
还是什么都不使用,您始终可以在方法内部对其进行修改。您永远不会将真实的 person 对象传递给该方法,而是将指针作为引用传递,而不是实际的Person
。ref
关键字对于值类型(例如结构、int、float、DateTime 等)非常有用。它也可以与引用类型一起使用,但只能指示行为,但不能强制执行它。如果将它与引用类型一起使用,它允许您更改此引用指向的对象。Person
is a reference type, so no matter whether you useref
,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 actualPerson
. Theref
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.当您将 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.
Utilityclass.TestMethod
无法更改局部变量p
以指向不同的Person
对象,因为您没有通过引用传递,但它仍然是免费的调用任何方法或更改所传递的对象的任何属性。因此可以在Utilityclass.TestMethod
中修改Name
属性。Utilityclass.TestMethod
cannot change the local variablep
to point to a differentPerson
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 theName
property can be modified withinUtilityclass.TestMethod
.这个问题已经大部分得到了回答,但我想你可能想尝试一下这个片段(如果你用整数尝试的话,会加分!)
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!)
当您将引用类型参数传递给方法时,这意味着该方法可以直接访问该参数而不是它的副本......
因此结果已更改。
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.