“ref"和“out"关键字有什么区别? [英] What's the difference between the 'ref' and 'out' keywords?

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

问题描述

我正在创建一个函数,我需要在其中传递一个对象,以便该函数可以对其进行修改.有什么区别:

I'm creating a function where I need to pass an object so that it can be modified by the function. What is the difference between:

public void myFunction(ref MyClass someClass)

public void myFunction(out MyClass someClass)

我应该使用哪个,为什么?

Which should I use and why?

推荐答案

ref 告诉编译器对象在进入函数前已经初始化,而 out 告诉编译器对象将在函数内部初始化.

ref tells the compiler that the object is initialized before entering the function, while out tells the compiler that the object will be initialized inside the function.

因此,虽然 ref 是双向的,但 out 是仅输出的.

So while ref is two-ways, out is out-only.

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

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