ref vs. out:运行时相同,编译时相同/不同? [英] ref vs. out: same at runtime, same/different at compiler time?

查看:75
本文介绍了ref vs. out:运行时相同,编译时相同/不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

MSDN说:ref-out关键字在run-

时间处理不同,但在编译时它们的处理方式相同。因此,如果一个方法接受一个ref参数而另一个方法需要一个out参数,那么方法

不能重载。


但是,我认为这意味着它们在运行时被视为相同,并且

在编译时处理不同且相同:1。你不能拥有

两种方法仅在ref / out中有所不同(可能是因为它们在运行时被视为
处理相同),因此它们在编译时的编码时间似乎是相同的,并且2,ref / out特别是在

条款中以不同的方式处理它们需要被初始化的时间,这是在

编译时捕获的。


这是正确的吗?


Zytan

MSDN says: "The ref and out keywords are treated differently at run-
time, but they are treated the same at compile time. Therefore methods
cannot be overloaded if one method takes a ref argument and the other
takes an out argument."

But, I think it means they are treated the same at run time, and
treated BOTH different AND the same at compile time: 1. you can''t have
two methods that differ only in ref/out (presumably because they are
treated the same at run time), so they appear to be the same at
compile time, and 2, ref/out specifically are handled differently in
terms of when they need to be ininitialized, which is caught at
compile time.

Is that correct?

Zytan

推荐答案

1。你不能拥有
1. you can''t have

两种方法仅在ref / out中有所不同(大概是因为它们在运行时被视为
处理相同),
two methods that differ only in ref/out (presumably because they are
treated the same at run time),



否,在运行时它们由CLR根据不同的规则处理。


2,参考/特别是在

No, at run time they are processed by the CLR according to different rules.

2, ref/out specifically are handled differently in


处理它们需要初始化时的条款,这是在

编译时捕获的。
terms of when they need to be ininitialized, which is caught at
compile time.



不完全。他们在编译时被视为相同的原因是

他们的方法签名是相同的,这就是为什么你不能超载

他们的原因。
" Zytan" < zy ********** @ yahoo.comwrote in message

news:11 ******************** **@64g2000cwx.googlegro ups.com ...

Not quite. The reason they are treated the same at compile time is that
thier method signatures are the same and this is why you can''t overload
them.
"Zytan" <zy**********@yahoo.comwrote in message
news:11**********************@64g2000cwx.googlegro ups.com...


MSDN说:ref-out关键字在运行时的处理方式不同 -

时间,但它们在编译时被视为相同。因此,如果一个方法接受一个ref参数而另一个方法需要一个out参数,那么方法

不能重载。


但是,我认为这意味着它们在运行时被视为相同,并且

在编译时处理不同且相同:1。你不能拥有

两种方法仅在ref / out中有所不同(可能是因为它们在运行时被视为
处理相同),因此它们在编译时的编码时间似乎是相同的,并且2,ref / out特别是在

条款中以不同的方式处理它们需要被初始化的时间,这是在

编译时捕获的。


这是正确的吗?


Zytan
MSDN says: "The ref and out keywords are treated differently at run-
time, but they are treated the same at compile time. Therefore methods
cannot be overloaded if one method takes a ref argument and the other
takes an out argument."

But, I think it means they are treated the same at run time, and
treated BOTH different AND the same at compile time: 1. you can''t have
two methods that differ only in ref/out (presumably because they are
treated the same at run time), so they appear to be the same at
compile time, and 2, ref/out specifically are handled differently in
terms of when they need to be ininitialized, which is caught at
compile time.

Is that correct?

Zytan



Zytan< zy ** ********@yahoo.comwrote:
Zytan <zy**********@yahoo.comwrote:

MSDN说:ref-out关键字在运行时被区别对待 -

时间,但在编译时它们的处理方式相同。因此,如果一个方法接受ref参数而另一个方法接受一个out参数,则方法

不能重载。
MSDN says: "The ref and out keywords are treated differently at run-
time, but they are treated the same at compile time. Therefore methods
cannot be overloaded if one method takes a ref argument and the other
takes an out argument."



我个人会说它反过来了。他们肯定会在编译时以不同的方式对待他们 - 使用out你可以传入一个

未初始化的变量,而ref参数必须初始化。


在方法本身中,必须在代码中初始化out参数

方法正常终止之前(即没有例外)

而ref没有这样的限制。


然而,就运行时而言,我*相信*out

只是为了编译器而在方法中添加了一个属性。我认为

序列化代码也注意到它,但不是CLR

本身。


-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet 博客: http://www.msmvps.com/jon.skeet

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

I would personally say it''s the other way round. They''re definitely
treated differently at compile-time - with "out" you can pass in an
uninitialized variable, whereas a ref argument must be initialised.

In the method itself, an out parameter must be initialised in the code
before the method terminates normally (i.e. without an exception)
whereas there''s no such restriction for ref.

As far as the runtime is concerned, however, I *believe* that "out"
just adds an attribute to the method for the sake of compilers. I think
that serialization code takes note of it as well, but not the CLR
itself.

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


Scott M.< s - *** @nospam.nospamwrote:
Scott M. <s-***@nospam.nospamwrote:

1.你不能拥有
1. you can''t have

两种方法仅在ref / out中有所不同(大概是因为它们在运行时处理相同的
),
two methods that differ only in ref/out (presumably because they are
treated the same at run time),



否,在运行时它们由CLR根据不同的规则处理。


No, at run time they are processed by the CLR according to different rules.



您能否在CLI规范中指出这些不同的规则?我不认为
相信存在任何差异。

Could you point out those different rules in the CLI spec? I don''t
believe there''s any difference.


2,ref / out具体在$ b $中处理不同b
2, ref/out specifically are handled differently in

它们何时需要初始化的条款,这是在编译时获得的

terms of when they need to be ininitialized, which is caught at
compile time.



不太好。他们在编译时被视为相同的原因是

他们的方法签名是相同的,这就是为什么你不能超载

他们。


Not quite. The reason they are treated the same at compile time is that
thier method signatures are the same and this is why you can''t overload
them.



他们的待遇不一样。改变退出对于ref反之亦然

改变了你能做什么和不能做什么的规则。


-

Jon Skeet - < sk *** @ pobox.com>
http:// www .pobox.com / ~silet 博客: http://www.msmvps .com / jon.skeet

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

They''re not treated the same. Changing "out" for "ref" or vice versa
changes the rules as to what you can and can''t do.

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


这篇关于ref vs. out:运行时相同,编译时相同/不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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