问题
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