ref keyword with reference type parameter [closed]

╄→尐↘猪︶ㄣ 提交于 2019-12-11 09:55:20

问题


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;
}

, 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();
}

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?


回答1:


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

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.

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



来源:https://stackoverflow.com/questions/26057213/ref-keyword-with-reference-type-parameter

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!