Using REF & OUT keywords with Passing by Reference & Passing by Value in C#

前端 未结 7 588
南笙
南笙 2021-01-12 06:32

Here is what I understand so far:

PASS BY VALUE

Passing by value means a copy of an argument is passed. Changes to that copy do not change the original.

7条回答
  •  醉梦人生
    2021-01-12 06:49

    Using the OUT keyword is useful if you have a method that you need to return more than one value. For example, look at methods like int.TryParse().

    Using REF is more for explicitness of objects. Keeping in mind that any non-primitive passed into a method is inherently passed by reference there's not a lot of need for it in normal managed code. By declaring the REF keyword you're stating that the parameter will likely be modified in the body of the method and thus the calling code should be aware of it (thus why you have to explicitly add the ref in the calling code as well.

提交回复
热议问题