ref关键字与参考类型参数 [英] ref keyword with reference type parameter

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

问题描述

我认为更清楚的是,如果我传递引用类型参数到方法,将在方法内添加
ref关键字像这样

I think it is more clear that if I pass reference type parameter to the method, that will be changed inside method to add ref keyword like this

void Foo(ref Boo boo)
{
    boo.Value = 6;
}

,即使这不会影响程序执行,通过引用,我不想改变
引用内部void像这样:

, even this doesn't affect program execution any way and by default objects are passed by reference and I don't want to change reference inside void like this:

void Foo(ref Boo boo)
{
    boo = new Boo();
}

因为我认为使用ref从方法签名中清楚, Boo里面,而不是只是读它。
你同意吗?

because I think that with ref it is clear from method signature that I will change Boo inside instead of just reading it. Do you agree? What do you think about this?

推荐答案


我想更清楚的是,如果我通过引用类型参数到方法,将在方法内改变添加ref关键字像这样

I think it is more clear that if I pass reference type parameter to the method, that will be changed inside method to add ref keyword like this

不,这只是表明你'熟悉 ref 的预期含义。这是完全正常和惯用的方法来操纵对象提供给他们。如果您不想要: 写不可变对象

No, that just demonstrates that you aren't familiar with the intended meaning of ref. It is entirely normal and idiomatic for methods to manipulate objects that are made available to them. If you don't want that: write immutable objects.

不要这样做。缺少 ref 确实意味着任何种类的 const

Do not do this. The lack of ref does not imply any kind of const.

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

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