“参考” 关键字和应用程序域
当我开始使用 C# 时,我不确定如何准确处理引用(它们是否按值传递等)。 我错误地认为在传递将由被调用方法修改的对象时需要“ref”关键字。
然后在阅读了像 this 这样的线程之后,我意识到“ref”仅当您需要更改实际引用/指针本身时才需要。
但今天我在通过远程调用传递参数时遇到了一个问题,实际上需要 ref 来修改对象的内容。 当没有引用的情况下传递时,对象返回时没有改变。 有人告诉我添加 ref 关键字,但我争论了一段时间,只有当您更改指针本身而不是所指向的内容时才需要添加 ref 关键字。
我在网上搜索过,只能找到
When I started using C# I was unsure of how references were treated exactly (whether they were being passed by value etc.). I wrongly thought the 'ref' keyword was needed when passing objects that would be modified by the called method.
Then after reading threads like this, I realized 'ref' was only needed when you need to change the actual reference / pointer itself.
But today I have come across an issue when passing a parameter via a remoting call, where ref was actually needed to modify the content of the object. When passed without ref, the object came back unchanged. I was told to add the ref keyword but I argued for a while that it was only necessary when you change the pointer itself, not the content that is being pointed to.
I have searched the net and could only find a single page that discusses it briefly. Is this a known issue and is anyone able to point to some documentation about it? It seems to me that I will have to use ref now for any parameter that is being modified via a remoting call.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
添加“ref”可能有帮助,也可能没有帮助。 这一切都取决于特定编组器实现的智能程度。 例如,如果您调用 Web 服务,那么再多的“ref”也无济于事——函数的参数根本不会通过线路发送回。 从服务返回的唯一内容是函数的返回值。 在处理远程处理时,您必须(至少在某种程度上)了解事物实际工作的方式 - 事实上参数需要序列化并通过某种“线路”发送到被调用者,在另一端反序列化,工作由服务器执行,结果序列化并发回给您。 这些结果是否包括对您首先传递的参数的更改更多地取决于特定的远程处理实现,然后取决于您添加来装饰参数的“ref”...
Adding "ref" might, or might not help. It all depends on the smartness of the particular marshaller implementation. If you call, for example, a web service, no amount of "ref"s is going to help you -- the parameters of the function are simply not sent back over the wire. The only thing that comes back from the service is the function's return value. When dealing with remoting you have to understand (at least to some degree) the way things actually work -- the fact that parameters need to be serialized and sent to the callee over some sort of "wire", deserialized on the other end, work is performed by the server, and the results serialized and sent back to you. Whether these results include changes to the parameters you passed in the first place depends more on the specific remoting implementation, then on the "ref"s that you add to decorate your parameters...
我想知道你为什么说“引用/指针”? 这些术语之间存在很大差异。 看,指针只是一个地址,比如一个 int。
另一方面,引用只不过是某物的别名。 就 C++ 而言:
从现在开始,无论 x 发生什么,y 也发生什么,反之亦然,它们将“永远”绑定。
同样,在 C++ 中,传递引用:
这意味着 y 只是 foo 范围内 x 的另一个名称(别名)。 这就是 ref 在 C# 中的含义。
I wonder why did you say "reference/pointer"?. There's a big difference between those terms. See, a pointer is just an address, say an int.
On the other hand, a reference is nothing but an alias to something. In terms of C++:
From here on, whatever happens to x, happens to y and viceversa, they are bound "forever".
Again, in C++, a pass-by-ref:
It means that y, is just another name (alias) for x within foo's scope. This is what ref means in C#.