[Solved] C# – ref this (reference to self)


You don’t need to use ref in your parameter declarations. For reference types like your class A, a parameter declared as just A other will cause C# to pass a reference to the object.

A parameter declared as A other in C# is similar to A* other in C++.

A parameter declared as ref A other in C# is similar to A** other in C++.

class A
{
    int x = 20;

    void Interact(A other)
    {
        if (x % 2 == 0) { other.x++; x /= 2; }
    }

    public void MakeInteraction(A other)
    {
        Interact(other);
        other.Interact(this);
    }

    static void Main()
    {
        A a = new A();
        A b = new A();
        a.MakeInteraction(b);
    }
}

0

solved C# – ref this (reference to self)