Passing a class as a ref parameter in C# does not always work as expected. Can anyone explain?

前端 未结 4 431
慢半拍i
慢半拍i 2020-12-08 14:37

I always thought that a method parameter with a class type is passed as a reference parameter by default. Apparently that is not always the case. Consider these unit tests

4条回答
  •  -上瘾入骨i
    2020-12-08 15:04

    My 2 cents

    When class is passed to a method a copy of it's memory space address is being sent (a direction to you house is being sent). So any operation on that address with effect the house but will not change the address it self. (this is default)

    Passing class(object) by reference has an effect of passing it's actual address instead of copy of an address. That means if you assign a new object to argument passed by reference it will change the actual address (similar to relocation). :D

    This is how I see it.

提交回复
热议问题