如何保存 ref 变量以供以后使用?
所以这有效..
public MyClass(ref Apple apple)
{
apple = new Apple("Macintosh"); // Works fine
}
但是有可能做这样的事情吗?
private Apple myApple;
public MyClass(ref Apple apple)
{
myApple = apple;
}
public void ModifyApple()
{
myApple = new Apple("Macintosh"); // does not change the input variable like the first example did
}
当 ref 变量被复制到成员变量 myApple
时,它似乎失去了它的“引用性”,并且重新分配它不再更改输入变量。有办法解决这个问题吗?
So this works..
public MyClass(ref Apple apple)
{
apple = new Apple("Macintosh"); // Works fine
}
But is it possible to do something like this?
private Apple myApple;
public MyClass(ref Apple apple)
{
myApple = apple;
}
public void ModifyApple()
{
myApple = new Apple("Macintosh"); // does not change the input variable like the first example did
}
When the ref variable is copied to the member variable myApple
it appears to lose it's 'ref-ness' and re-assigning it no longer changes the input variable. Is there a way around this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
ref
-ness 是函数参数和传递给函数的参数的属性。一般来说,它不是变量(甚至字段)的属性。所以事实上,你在那里尝试的事情从一开始就注定是失败的。
ref
-ness is a property of parameters of functions and arguments you are passing to functions. It's not a property of variables (or even fields) in general.So indeed, what you are trying there is doomed from the start.
不完全是,不。到下次调用代码时,用作方法参数的原始变量甚至可能不再存在:
这里,
apple
是一个本地变量,位于堆栈帧中当我们调用ModifyApple
时,它就会被弹出。您确定需要修改原始调用者的变量,而不仅仅是更改对象本身吗?
对此进行排序的一种方法是使用包装类型开始:
然后传入
MutableWrapper
,并将其存储在您的类中。然后在ModifyApple
中,您可以编写:这不会更改调用者的变量,但下次调用者查看
Value
属性时,它们会'会看到你的新苹果。老实说,这种事情往往会导致代码难以维护,甚至
ref
的可读性也不是很好。如果您能解释一下您想要实现的目标,我们也许可以建议更好的整体方法。Not really, no. By the time your code is next invoked, the original variable used as the method argument may no longer even exist:
Here,
apple
is a local variable, in a stack frame which will have been popped by the time we callModifyApple
.Are you sure you need to modify the original caller's variable rather than just changing the object itself?
One way to sort of fake this would be to use a wrapper type to start with:
Then pass in a
MutableWrapper<Apple>
, and store that in your class. Then inModifyApple
you can write:This won't change the caller's variable, but next time the caller looks at the
Value
property, they'll see your new apple.To be honest, this sort of thing tends to make for hard-to-maintain code, and even
ref
isn't great for readability. If you can explain the bigger picture of what you're trying to achieve, we may be able to suggest a better overall approach.不,没有。
myApple
是一个保存对Apple
引用的字段;然而,参数apple
实际上是对Apple的引用。当您将apple
分配给myApple
时,您取消引用参数的值。除此之外,它们是独立且独特的。所以不:这是不可能的。
可能是这样的:
现在;如果您存储
AppleWrapper
,任意数量的调用者都可以访问并更改.Value
No, there isn't.
myApple
is a field that holds a reference to anApple
; the parameterapple
, however, is actually a reference-to-a-reference-to-an-Apple. When you assignapple
tomyApple
you dereference the value of the parameter. Beyond that, they are separate and distinct.So no: it is not possible.
What would be possible is to have something like:
Now; if you store an
AppleWrapper
, any number of callers can access and change the.Value
为什么不直接让
ModifyApple
返回修改后的 Apple 实例呢?Why not just have
ModifyApple
return the modified Apple instance?