C# 通过值/引用传递?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/436986/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# pass by value/ref?
提问by masfenix
Common question but I could use an "english" explanation.
常见问题,但我可以使用“英语”解释。
Is it like Java where
是不是像 Java 那样
Cat myCat
actually is a pointer to Cat
?
实际上是指向Cat
?
Should I really create copy constructors in C#?
我真的应该在 C# 中创建复制构造函数吗?
I understand we are passing by value, but now my question is are we passing by pointer value or full copy of the object?
我知道我们是通过值传递,但现在我的问题是我们是通过指针值还是对象的完整副本传递?
If it's the latter, isn't that too expensive performance/memory wise? Is that when you have to use the ref keyword?
如果是后者,那在性能/内存方面是不是太昂贵了?那是你必须使用 ref 关键字的时候吗?
采纳答案by Sunny Milenov
As @rstevens answered, if it is a class, myCat is a reference. But if you pass myCat to a method call, then the reference itself is passed by value - i.e. the parameter itself will reference the same object, but it's a completely new reference, so if you assign it to null, or create a new object, the old myCat reference will still point to the original object.
正如@rstevens 回答的那样,如果它是一个类,则 myCat 是一个参考。但是如果你将 myCat 传递给一个方法调用,那么引用本身是按值传递的——即参数本身将引用同一个对象,但它是一个全新的引用,所以如果你将它分配给 null,或者创建一个新对象,旧的 myCat 引用仍将指向原始对象。
SomeMethod(myCat);
void SomeMethod(Cat cat)
{
cat.Miau(); //will make the original myCat object to miau
cat = null; //only cat is set to null, myCat still points to the original object
}
Jon Skeet has a good articleabout it.
回答by mmmmmmmm
If you delacred Cat as
如果您将 Cat 声明为
class Cat {...}
then it is.
那么它是。
If you delcared Cat as
如果您将 Cat 声明为
struct Cat {...}
then your variable "is" the structure itself.
那么你的变量“就是”结构本身。
This is the difference between reference types and value types in .Net.
这是 .Net 中引用类型和值类型之间的区别。
回答by Joel Coehoorn
Remember that a pointer is not exactly the same as a reference, but you can just about think of it that way if you want.
请记住,指针与引用并不完全相同,但如果您愿意,您可以这样想。
I swear I saw another SO question on this not 10 minutes ago, but I can't find the link now. In the other question I saw, they were talking about passing arguments by ref vs by value, and it came down to this:
我发誓我不是在 10 分钟前看到另一个 SO 问题,但我现在找不到链接。在我看到的另一个问题中,他们在谈论通过 ref 和按值传递参数,归结为:
By default in .Net, you don't pass objects by reference. You pass references to objects by value.
默认情况下,在 .Net 中,您不通过引用传递对象。您按值传递对对象的引用。
The difference is subtle but important, especially if, for example, you want to assign to your passed object in the method.
区别很微妙但很重要,特别是如果,例如,您想分配给方法中传递的对象。
回答by Jacob Proffitt
Yes, it's about pointers but not really... The thing that messed me up originally is that it isn't really about about protecting your variable from changes within the method. If you change the object within the method, those changes are visible to the external methods regardless of whether it is passed in "ref" or not.
是的,它是关于指针,但不是真的......最初让我感到困惑的是,它并不是真正关于保护您的变量免受方法内的更改。如果在方法中更改对象,则这些更改对外部方法可见,无论它是否在“ref”中传递。
The difference (as I understand it) is whether the variable you send in has its reference updated coming back out if you change the object that variable references. So given this method
区别(据我所知)是如果您更改变量引用的对象,您发送的变量是否更新了其引用。所以给出这个方法
public void DoSomething(ref CoolShades glasses)
{
glasses.Vendor = "Ray Ban";
glasses = new CoolShades();
}
the variable you passed in as a parameter now contains a reference to the new CoolShades rather than whatever object it referenced before. The original parameter object's Vendor property will be changed to "Ray Ban" regardless of whether you passed the parameter ref or not.
您作为参数传入的变量现在包含对新 CoolShades 的引用,而不是它之前引用的任何对象。无论你是否传递了参数 ref,原始参数对象的 Vendor 属性都会更改为“Ray Ban”。