How to pass this by ref in C#?

后端 未结 5 1904
没有蜡笔的小新
没有蜡笔的小新 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:02

    The ref keyword causes Pass by Reference semantics - that is, if the variable is re-assigned in the called function, it will re-assign the variable in the caller as well.

    Obviously, this only works if a variable2 (which can be re-assigne to) is directly passed as the argument and will not work if an arbitrary expression is passed. In this case, this is not a variable, rather a special expression which cannot be re-assigned, and so cannot be used.

    As such, this would work: (But please see other answers and keep reading as to why this is likely not required and/or just silly.)

    var me = this;
    var slave = new ClassB(ref me);
    

    However, Pass by reference should not be confused with Pass by Object [Sharing]1 semantics. Pass by Object means that if an object is passed, that object is passed: the object is not copied/cloned/duplicated. If the object is mutated then the object is mutated. All Reference Types have Pass by Object semantics in C# unless either ref or out are used. (The class keyword declares a new reference type, as in the case in the post).

    On the other hand, Value Types (e.g. struct), including int and Guid and KeyValuePair<K,V>, have Pass by Value semantics - in this case a copy is made and thus, if the value is modified, only the value struct (which is a copy) changes.

    Happy coding


    1 Underneath C#/.NET achieves Pass by Object by passing a reference to an object by Value. However, the rules above correctly describe the observable semantics and effects.

    2 Unlike C#, which only allows variables to be used with ref, VB.NET allows Properties to be used. The VB.NET compiler automatically creates a temporary variable and implicit reading/writing instructions of the property during compilation.

    0 讨论(0)
  • 2020-12-03 18:03

    You can't change the this pointer, which is something that the ref keyword allows. Why can't you declare ClassB like this?

    ClassA m_classA;
    public ClassB(ClassA classA)
    {
       m_classA = classA;
    }
    
    0 讨论(0)
  • 2020-12-03 18:11

    Do you really need the ref keyword? All the types are basically passed by reference, so if you have ClassB take ClassA as constructor argument, just pass new ClassB(this), no need to use ref.

    0 讨论(0)
  • 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);
    
    0 讨论(0)
  • 2020-12-03 18:26

    You don't need to pass by ref in this case. If you are passing ClassB(this), it will be passed by reference and not by value anyway. Any changes made to the classA instance passed into classB's constructor will be applied to class A as well.

    0 讨论(0)
提交回复
热议问题