Is there a way to force the this keyword to act as a ref argument? I would like to pass in a visitor that modifies multiple properties on the object, but this only wants to act like a value parameter.
Code in Object:
public void Accept(Visitor<MyObject> visitor)
{
visitor.Visit(this);
}
Code in Visitor:
public void Visit(ref Visitor<MyObject> receiver)
{
receiver.Property = new PropertyValue();
receiver.Property2 = new PropertyValue();
}
Since you are not actually changing what
receiverrefers to there is no need for therefkeyword. However, if that would have been the case, you would not be able to makethisrefer to another instance.In order to pass
thisas arefparameter, you would need to write it like this:That code will not compile: “Cannot pass ‘<this>’ as a ref or out argument because it is read-only”