ref和out之间的区别? [英] Difference between ref and out?

查看:104
本文介绍了ref和out之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用ref关键字作为将字符串传递给方法的方式,因为

引用,所以在方法中我不必手动返回

修改后的版本传递给方法的内容,我只需要为ref''d参数赋予一个新值

。但是,我已经了解到''out''

关键字似乎做了这样的事情。


我有点困惑;对于我的意图,''ref''和

''out'之间的实际区别是什么?


谢谢,

Jon

I''ve been using the ref keyword as a way of passing strings to methods as
references so that in the method I don''t have to manually return the
modified "version" of what was passed into the method, I just have to assign
a new value to the ref''d argument. However, I''ve learned that the ''out''
keyword seems to do something just like this.

I''m kind of confused; what''s the practical difference between ''ref'' and
''out'' for my intents?

Thanks,
Jon

推荐答案

Jon Davis写道:
Jon Davis wrote:
我有点困惑;对于我的意图,'ref''
和'out'之间的实际区别是什么?
I''m kind of confused; what''s the practical difference between ''ref''
and ''out'' for my intents?




A" ref"在调用函数之前,参数需要初始化; out

不会。

以下内容无法编译:


string a;

f(ref a);

以下将编译:

string a;

g(out a);

-

问候

Jochen



A "ref" parameters need initialization BEFORE you call the function; "out"
does not.
The following will not compile:

string a;
f(ref a);
The following will compile:
string a;
g(out a);
--
Greetings
Jochen


必须初始化要作为ref参数发送的变量。

它的目的是在传递方法中进行更改。


作为out参数发送的变量不需要初始化

在传递给方法之前,因为它必须在该方法中分配。

它不是要更改,而是要分配(或重新分配)

的传递方式。


-

问候,

DennisJDMyrén

Oslo Kodebureau

" Jon Davis" < jo*@REMOVE.ME.jondavis.net>在消息中写道

news:e
A variable to be sent as ref parameter must be initialized.
It is intended to be changed in the method to which it is passed.

A variable to be sent as out parameter dont need to be initialized
before being passed to a method, because it must be assigned in that method.
It is not intended to be changed, but intended to be assigned(or reassigned)
in the method to which it is passed.

--
Regards,
Dennis JD Myrén
Oslo Kodebureau
"Jon Davis" <jo*@REMOVE.ME.jondavis.net> wrote in message
news:e


************** @ tk2msftngp13.phx.gbl ...
**************@tk2msftngp13.phx.gbl...
我一直在使用ref关键字作为一种方法将字符串传递给方法作为
引用,这样在方法中我不必手动返回
修改后的 ;版本"传递给方法的内容,我只需要
为ref''d参数分配一个新值。但是,我已经知道''out''
关键字似乎就是这样做的。

我有点困惑;对于我的意图,''ref''和
''out'之间的实际区别是什么?

谢谢,
Jon
I''ve been using the ref keyword as a way of passing strings to methods as
references so that in the method I don''t have to manually return the
modified "version" of what was passed into the method, I just have to assign a new value to the ref''d argument. However, I''ve learned that the ''out''
keyword seems to do something just like this.

I''m kind of confused; what''s the practical difference between ''ref'' and
''out'' for my intents?

Thanks,
Jon



这篇关于ref和out之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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