How to pass this by ref in C#?

后端 未结 5 1911
没有蜡笔的小新
没有蜡笔的小新 2020-12-03 17:27

In a class (ClassA) of mine I want to create a related instance of another class (ClassB) providing it with a reference to the object who has initiated it\'s creation. So I\

5条回答
  •  没有蜡笔的小新
    2020-12-03 18:14

    ref refers to variable references, not object references.

    If you just want to pass a reference to an object, the ref keyword isn't necessary. Objects are already reference types, so it's their references being passed by value. The objects themselves aren't copied.

    So, you neither need the ref keyword in the constructor nor in the instantiation:

    public ClassB(ClassA master)
    {
    }
    
    var slave = new ClassB(this);
    

提交回复
热议问题