已经引用变量时使用'ref' [英] Using 'ref' when already reference variable
本文介绍了已经引用变量时使用'ref'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果对象已经是引用变量,将对象传递给带有'ref''
关键字的方法会有什么危害吗?如果没有,是否有任何好处?
Is there any harm in passing an object into a method with the ''ref''
keyword if the object is already a reference variable? If not, is
there any benefit?
推荐答案
Doug写道:
如果对象已经是引用变量,将对象传递给带有'ref''
关键字的方法有什么危害吗?如果没有,是否有任何好处?
Is there any harm in passing an object into a method with the ''ref''
keyword if the object is already a reference variable? If not, is
there any benefit?
这是对象的默认值。
That is the default for objects.
对对象使用''ref''意味着该方法可以改变指向哪个对象
。所以,是的 - 使用''ref''会有一些伤害,当你没有
想让方法改变指向哪个对象时。
>
但你可能没有选择 - 如果方法有''ref''参数,你必须
在参数上使用''ref''关键字(在C#中) 。
-
David Anton
www.tangiblesoftwaresolutions.com
即时C#:VB.NET转换为C#转换器
即时VB:C#转VB.NET转换器
即时C ++:C#到C ++转换器
即时J#:VB.NET到J#转换器
" Doug"写道:
Using ''ref'' for an object means that the method may change which object is
being pointed to. So, yes - there is some harm in using ''ref'' when you don''t
want to allow the method to change which object is being pointed to.
But you may not have a choice - if the method has a ''ref'' param, you must
use the ''ref'' keyword on the argument (in C#).
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB.NET to C# Converter
Instant VB: C# to VB.NET Converter
Instant C++: C# to C++ Converter
Instant J#: VB.NET to J# Converter
"Doug" wrote:
如果对象已经是引用变量,将对象传递给带有'ref''
关键字的方法会有什么危害吗?如果没有,是否有任何好处?
Is there any harm in passing an object into a method with the ''ref''
keyword if the object is already a reference variable? If not, is
there any benefit?
" John A. Bailo" < JA ***** @ texeme.com>写道:
"John A. Bailo" <ja*****@texeme.com> wrote:
如果对象已经使用''ref''
关键字将对象传递给方法有什么危害吗参考变量?如果没有,是否有任何好处?
Is there any harm in passing an object into a method with the ''ref''
keyword if the object is already a reference variable? If not, is
there any benefit?
这是对象的默认设置。
That is the default for objects.
其实是默认的(对于一切)是* by-value *传递。见
< http://www.yoda.arachsys.com/csharp/parameters.html> ;.
Actually the default (for everything) is *by-value* passing. See
<http://www.yoda.arachsys.com/csharp/parameters.html>.
这篇关于已经引用变量时使用'ref'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文