C# reference member variable

前端 未结 5 614
情深已故
情深已故 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:54

    If you mean ref the argument passing convention, then no, you cannot store this. From the first note on MSDN:

    Do not confuse the concept of passing by reference with the concept of reference types. The two concepts are not the same...


    Edit: based on your updated question, C# has different nomenclature about pointers and references. A pointer in C# is an unsafe construct used to somewhat directly reference the memory location of an object. I say somewhat because the memory location can change based on garbage collection (unless you fix it in memory).

    References in C# are the default way reference types are passed and stored. They are akin to pointers in other languages, but not quite the same. However, the by-reference argument passing convention allows you to directly change what an object refers to.

    If your objective is to keep a mutable reference to a non-reference type local variable, you'll have to encapsulate the local variable in a reference type (like a class). If you could give some sample code, we can give some specific examples.

提交回复
热议问题