C# Passing arguments by default is ByRef instead of ByVal

后端 未结 5 1865
闹比i
闹比i 2020-12-05 07:40

I know the default is ByVal in C#. I used same variable names in many places and then I noticed passed values change and come back. I think I knew scope mechanism of C# wron

5条回答
  •  佛祖请我去吃肉
    2020-12-05 08:26

    You are passing the licence argument by value; essentially this means you can modify any public properties of the object. However, if you reassign the reference of the licence object to a new object, i.e. if you did this:

    public static void InsertLicense(License license)      
    {         
       license = new Licence();         
       UpdateLicense(license);      
    }
    

    the caller won't reference the new license object defined in the static method unless you passed it by ref.

    Remember that all parameters are passed into methods by value unless you use the ref or out keywords.

提交回复
热议问题