C#中的Ref Propogation [英] Ref Propogation in C#

查看:94
本文介绍了C#中的Ref Propogation的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我有一个简单的问题。


假设我有3个功能如下


private main()

{

Object oTop = new Object();

}


private void A(ref Object obj)

{

B(obj)

}


private void B(Object obj)

{

obj =" 1";

}


函数B是否接受与对象oTop相同的引用

(作为A)或它是否收到参考文件的副本?


在这些

函数之间使用相同引用的最佳方法是什么?我是否需要在B函数中添加一个ref并使其像

这样:

private void B(ref Object obj)

{

obj =" 1";

}


请帮助我。我正在寻找最有效的

为什么要保持参考传播(我需要参考

,因为我更新了对象)。


谢谢。

Hi,

I have a simple question.

Let say that i have 3 functions as following

private main()
{
Object oTop = new Object();
}

private void A(ref Object obj)
{
B(obj)
}

private void B(Object obj)
{
obj = "1";
}

Does the function B receive the same reference to the object "oTop"
(as A) or it receive a copy of the reference?

What is the best way to keep using the same reference between those
functions? Do i need to add a ref to the B function and make it like
this:
private void B(ref Object obj)
{
obj = "1";
}

Please help me in this point. I''m looking to find the most efficient
why to keep with the reference propogation ( i need a reference
because i update the object).

Thanks.

推荐答案

Ori<或******* @ hotmail.com>写道:
Ori <or*******@hotmail.com> wrote:
我有一个简单的问题。

让我说我有3个功能如下

private main()
{
对象oTop = new Object();
}
私有空A(ref Object obj)
{
B(obj)
}

private void B(Object obj)
{
obj =" 1" ;;
}

函数B是否收到相同的参考对象oTop
(作为A)或它收到参考的副本?


它收到一份参考资料。这也是相同的引用,

但是B中的形式参数与A中的形式参数没有相同的内存位置。
/>
在这些
函数之间使用相同引用的最佳方法是什么?


你需要在这里弄清楚你的术语是正确的,否则很难理解你在做什么。

我是否需要在B函数中添加一个ref并使其像
这样:
private void B(ref Object obj)
{
obj =" ; 1"
}


你*可以*做到这一点,虽然在上面它会更好只是为了

让你的方法返回一个字符串。

请帮助我。我正在寻找最有效的
为什么要保持参考传播(我需要参考
因为我更新了对象)。
I have a simple question.

Let say that i have 3 functions as following

private main()
{
Object oTop = new Object();
}

private void A(ref Object obj)
{
B(obj)
}

private void B(Object obj)
{
obj = "1";
}

Does the function B receive the same reference to the object "oTop"
(as A) or it receive a copy of the reference?
It receives a copy of the reference. That is also "the same reference",
but the formal parameter in B doesn''t have the same memory location as
the formal parameter in A.
What is the best way to keep using the same reference between those
functions?
You need to get your terminology correct here, otherwise it''ll be very
difficult to understand what you''re actually doing.
Do i need to add a ref to the B function and make it like
this:
private void B(ref Object obj)
{
obj = "1";
}
You *could* do that, although in the above it would be better just to
make your method return a string.
Please help me in this point. I''m looking to find the most efficient
why to keep with the reference propogation ( i need a reference
because i update the object).




如果您要更新对象而不是创建新对象,那么您首先不需要通过引用传递。


请参阅 http://www.pobox.com/~ skeet / csharp / parameters.html 更多

信息。


-

Jon Skeet - < sk***@pobox.com>
http://www.pobox。 com / ~siget

如果回复小组,请不要给我发邮件



If you''re updating the object rather than creating a new object, you
don''t need to pass by reference in the first place.

See http://www.pobox.com/~skeet/csharp/parameters.html for more
information.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too




我放的功能只是举例而不是为了什么

真实。


我对一般概念感兴趣。


正如我写的,我想了解在函数之间传递

引用的最佳方法是什么,以及何时需要使用ref

关键字。


我想知道我们是否需要使用ref。每个函数

将接收该对象,或者仅仅执行一次(在

中传递此参数的第一个函数)。


谢谢。

***通过Developersdex发送 http://www.developersdex.com ***

不要只参加USENET ......获得奖励!


The function which i put were only for example and not for something
real.

I just was interesting for the general concept .

As I wrote, I would like to understand what is the best way to pass
reference between functions and when exactly we need to use the "ref"
keyword.

I want to know whether we need to use the "ref" with every function
which going to receive the object or it''s enough to do it only once (in
the first function which pass this parameter).

Thanks.
*** Sent via Developersdex http://www.developersdex.com ***
Don''t just participate in USENET...get rewarded for it!


Ori Anavim< an ******** @ hotmail.com>写道:
Ori Anavim <an********@hotmail.com> wrote:
我放的功能仅仅是举例而不是真实的东西。

我对一般概念感兴趣。

正如我写的那样,我想了解在函数之间传递
参考的最佳方法是什么,以及何时我们需要使用ref
关键字。
<我想知道我们是否需要使用ref。每个函数
将接收对象,或者仅仅执行一次(在第一个传递此参数的函数中)。
The function which i put were only for example and not for something
real.

I just was interesting for the general concept .

As I wrote, I would like to understand what is the best way to pass
reference between functions and when exactly we need to use the "ref"
keyword.

I want to know whether we need to use the "ref" with every function
which going to receive the object or it''s enough to do it only once (in
the first function which pass this parameter).



您是否阅读了我提供链接的页面?那应该比我现在有时间更好地解释一下。


-

Jon Skeet - < sk ***@pobox.com>
http://www.pobox.com /〜双向飞碟

如果回复小组,请不要给我发邮件



Did you read the page I gave the link for? That should explain
everything better than I have time to at the moment.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


这篇关于C#中的Ref Propogation的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆