C# objects by Ref

后端 未结 8 817
清歌不尽
清歌不尽 2021-01-27 12:23

If one passes an object to a method using the \'Ref\' keyword then what is the difference with passing it without the ref keyword?

Because both yield the same result, th

8条回答
  •  天涯浪人
    2021-01-27 12:39

    These examples illustrates the difference

    methodA(ref object a)
    {
          a = new object();
    }
    
     methodB(object a)
    {
         a = new object();
    }
    
    object c = new object();
    methodA(ref c); //now c points to an entire new object
    methodB(c); // does not change the value of c
    

提交回复
热议问题