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

前端 未结 7 591
南笙
南笙 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:47

    You pass by ref something you want to be read and written by another function, so you should pass the variable initialized in order to be correctly read.

    EDIT: example:

    void mymethod(ref int a) {
      a++;
    }
    

    You pass as OUT something you want to be written by another function, but you don't need to initialize it as it won't be read by the function, only written.

    EDIT: example:

    void mymethod2(out string a) {
      a="hello";
    }
    

提交回复
热议问题