C# reference member variable

前端 未结 5 648
情深已故
情深已故 2021-01-17 23:26

In C#, is there a way to keep a reference as a member variable in an object (like an object pointer in C++), not just as a parameter?

EDIT: How can I make a p

5条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-17 23:43

    No. Don't forget that the argument could reference a local variable which is out of scope by the time you use the object later on. A couple of options:

    • Use a mutable wrapper type
    • Use a delegate which captures the variable instead
    • Redesign your code to not require this in the first place

    It's hard to know which is most suitable without knowing more about what you're trying to achieve, but ref is a dead-end.

提交回复
热议问题