请在C#中关键字ref和out之间有什么区别 [英] Please what is the difference between keyword ref and out in c#

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

问题描述

我是初学者,我需要有关:
的帮助 请在c#

i''m beginner,i need help about :
Please what is the difference between keyword ref and out in c#

推荐答案

中,关键字ref和out之间有什么区别?请参阅以下最新答案:
out参数的重要性 [ out参数的重要性 [
Please see these recent answers:
significance of out parameter [Solution 4],
significance of out parameter [Solution 3],
see other answers and comments on this page.

It will answer your question. In a nutshell: both passes data by reference (recommended only for value types, not reference type), make passing faster (reference only, no copy of all data) and allow to return data by modifying the referenced object in method implementation, but out also requires modification (will not compile if the parameter is not assigned a new value), unlike ref, therefore, a calling code is not required to initialize a variable used as out parameter (as it will be initialized as a result of the call anyway).

—SA


默认情况下,始终将默认参数按值传递给方法和函数.如果要通过引用传递数据,则可以使用out或ref关键字.

差异:
当您使用OUT数据时,数据只能以一种方式传递,即从方法到调用者代码.
当您使用REF时,数据可以从被调用方法传递给方法,反之亦然.

在OUT中,变量值必须在方法中初始化.
在REF中,值由调用方在方法外部初始化.
表示
在ref中,我们必须在使用之前初始化变量,但
由内而外,我们不需要在使用变量之前对其进行初始化.
您可以在
By default parameters are always passed by value to methods and functions.If you want to pass data by refrence then you can use either out or ref keyword.

Diff:
When you use OUT data is passed only one way i.e from the method to the caller code.
When you use REF , Data can be passed from the called to the method and also vice versa.

In OUT the variables value has to be intialized in the method.
In REF the value is initialized outside the method by the caller.
means
in ref we have to initialize variable before using but
in out we do not need to initilize the variable before using it.
You can see lot of examples on Google easily.......


^ ]


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

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