函数参数中的ref关键字 [英] ref keyword in a function parameter

查看:217
本文介绍了函数参数中的ref关键字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我已经考虑了一段时间了...
建议何时在函数参数中使用ref关键字?
我知道它的作用...对我来说,它与c样式指针相同.
它将地址(引用)传递给变量,而不是变量的副本.

我知道使用ref会减少对资源的依赖.
但是实际上什么时候让函数返回到ref函数参数之上或者反之亦然呢?

谢谢,

Hi
I''ve been pondering on this matter for some time now...
When is it suggested to use the ref keyword in a function parameter?
I know what it does... To me it is the same as a c style pointer.
It passes the address(reference) to the variable rather than a copy of the variable.

I know that using ref is probebly less resource dependent.
But when is it actually better to rather have a function return above a ref function parameter or visa versa?

Thanks,

推荐答案

不要将按引用传递的概念与引用类型的概念混淆.这两个概念不相同.可以修改方法参数引用,无论它是值类型还是引用类型.通过引用传递值类型时,都不会装箱."

[
"Do not confuse the concept of passing by reference with the concept of reference types. The two concepts are not the same. A method parameter can be modified by ref regardless of whether it is a value type or a reference type. There is no boxing of a value type when it is passed by reference."

This [^] should answer your queries.


这要视情况而定.

大多数现有的API始终使用基于"ref"的函数,并且还会返回值.
It depends.

Most of the existings APIs always uses "ref" based functions and also returns value.


方法参数可以由ref修改,无论它是值类型还是引用类型.

通过引用传递时,没有值类型的装箱.
A method parameter can be modified by ref regardless of whether it is a value type or a reference type.

There is no boxing of a value type when it is passed by reference.


这篇关于函数参数中的ref关键字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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