通过引用两次传递 [英] Passing by reference twice
问题描述
如果我通过引用引用另一个例程传递一个变量,请执行
我需要先取消引用吗?
string testString;
....
FirstSub(ref firstString)
{
HandleString(ref firstString); //我需要在这里引用
引用,或者只是在没有ref的情况下传递theString
}
void HandleString(ref theString)
{
Console.WriteLine(thestring);
}
" tshad" < TS ********** @ ftsolutions.com>在消息中写道
新闻:Oc ************** @ TK2MSFTNGP05.phx.gbl ...如果我通过了通过引用引用另一个例程变量,我需要先取消引用吗?
字符串testString;
......
FirstSub(ref firstString )
{
HandleString(ref firstString); //我需要在这里引用或者只是传递theString而不使用ref
}
void HandleString(ref theString)
{
Console.WriteLine (thestring);
}
不,但为什么要传递参考?
因为我在另一个函数中构建字符串,这个函数可以相当大,而且我不想传递实际的字符串,这会变得非常大。
它现在似乎确实有效,我不确定我在做什么导致
问题。这个例子实际上是有用的。
我很好奇为什么它可以正常工作。
如果我有testString并且我把它传递给了引用FirstSub,然后它有对引用testString(而不是字符串本身)的
。当我把它传递给
HandleString时,我不是要传递对testString引用的引用吗?
如果是这样的话,HandleString怎么样?正确处理它?<br />
谢谢,
汤姆
" Michael C" <无**** @ nospam.com>在消息中写道
news:u4 ************** @ TK2MSFTNGP03.phx.gbl ..." tshad" < TS ********** @ ftsolutions.com>在消息中写道
新闻:Oc ************** @ TK2MSFTNGP05.phx.gbl ...如果我通过引用传递变量通过引用另一个例程,我需要先取消引用吗?
字符串testString;
...
FirstSub(ref testString);
void FirstSub(ref firstString){
HandleString(ref firstString); //我需要在这里引用或者只是传递theString而不使用ref
}
void HandleString(ref theString)
{
Console.WriteLine (thestring);
}
不,但为什么要传递参考?
" tshad" < TS ********** @ ftsolutions.com>在消息中写道
新闻:Ol ************** @ TK2MSFTNGP02.phx.gbl ...因为我正在建设字符串在另一个函数中可以得到相当大的并且我不想传递实际的字符串,这将变得非常大。
C#在按值传递时不会复制整个字符串。这是一个参考
类型,所以只有指向它的指针才会被传递。如果你想修改字符串,你只需要传递一个
引用。
如果是这种情况,HandleString如何正确处理它?</ blockquote>
它可能只是传递指针。
Michael
If I am passing a variable by reference to another routine by reference, do
I need to dereference first?
string testString;
....
FirstSub(ref firstString)
{
HandleString(ref firstString); //do I need to de
reference here or just pass theString without the "ref"
}
void HandleString(ref theString)
{
Console.WriteLine(thestring);
}解决方案"tshad" <ts**********@ftsolutions.com> wrote in message
news:Oc**************@TK2MSFTNGP05.phx.gbl...If I am passing a variable by reference to another routine by reference,
do I need to dereference first?
string testString;
...
FirstSub(ref firstString)
{
HandleString(ref firstString); //do I need to de
reference here or just pass theString without the "ref"
}
void HandleString(ref theString)
{
Console.WriteLine(thestring);
}
No, but why pass it a ref?
Because I am building the string in another function which can get fairly
large and I don''t want to pass the actual string which will get very large.
It does seem to work now and I am not sure what I was doing that was causing
the problem. The example actually work.
I am curious as to why it work, however.
If I have testString and I pass it as a reference to FirstSub, then it has
the reference to testString (not the string itself). When I pass it to
HandleString, aren''t I passing a reference to a reference of testString?
If that is the case, how does HandleString handle it correctly?
Thanks,
Tom
"Michael C" <no****@nospam.com> wrote in message
news:u4**************@TK2MSFTNGP03.phx.gbl..."tshad" <ts**********@ftsolutions.com> wrote in message
news:Oc**************@TK2MSFTNGP05.phx.gbl...If I am passing a variable by reference to another routine by reference,
do I need to dereference first?
string testString;
...
FirstSub(ref testString);
void FirstSub(ref firstString) {
HandleString(ref firstString); //do I need to de
reference here or just pass theString without the "ref"
}
void HandleString(ref theString)
{
Console.WriteLine(thestring);
}
No, but why pass it a ref?
"tshad" <ts**********@ftsolutions.com> wrote in message
news:Ol**************@TK2MSFTNGP02.phx.gbl...Because I am building the string in another function which can get fairly
large and I don''t want to pass the actual string which will get very
large.
C# doesn''t copy the entire string when passing it by value. It''s a reference
type so only a pointer to it will be passed anyway. You only need to pass a
reference if you want to modify the string.
If that is the case, how does HandleString handle it correctly?
It probably just passes on the pointer it has.
Michael
这篇关于通过引用两次传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!