C# ref keyword usage

后端 未结 7 1362
耶瑟儿~
耶瑟儿~ 2020-12-10 03:39

I understand (or at least I believe I do) what it means to pass an instance of a class to a method by ref versus not passing by ref. When or under

7条回答
  •  借酒劲吻你
    2020-12-10 04:36

    Most use cases for passing a reference variable by reference involve initialization and out is more appropriate than ref. And they compile to the same thing (the compiler enforces different constraints - that ref variables be initialized before being passed in and that out variables are initialized in the method). So the only case I can think of where this would be useful is where you need to do some checking of an instantiated ref variable and may need to reinitialize under certain circumstances.

    This might also be necessary to modify an immutable class (like string) as pointed out by Asaf R.

提交回复
热议问题