Passing by ref?

后端 未结 4 1016
无人共我
无人共我 2021-02-08 11:53

I am still confused about passing by ref.

If I have a Cache object which I want to be accessed/available to a number of objects, and I inject it using constructor inject

4条回答
  •  孤街浪徒
    2021-02-08 12:14

    No you do not need to use the ref keyword in this situation.

    Cache is a class, it is a reference type. When a reference is passed into a method, a copy of the reference (not the object itself) is placed into your parameter. Both references, inside and outside, of the method are pointing to the same object on the heap, and modification of the object's fields using one will be reflected in the other.

    Adding ref to your method call passes in the original reference. This is useful in a situation where you would be reassigning (ie. by calling new) the location the reference points to from within a calling method.

提交回复
热议问题