C# reference member variable

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

    Yes if it is a reference-type instance. And then it is the only way to store it in another class:

    class Bar { }
    
    class Foo
    {
        private Bar b; // b is a reference to a Bar
    }
    

    No if it's about a value-type, or a reference to a reference.

    You would see simple object-references everywhere that C++ uses pointers, like in building Trees or Linked-Lists.

    class Element { ...; private Element _next; }
    

提交回复
热议问题