It is possible to pass parameters by reference in C#. For example:
private void Add(ref Node node)
{
if (node == null)
{
node = new Node();
}
}
Add(ref this.Root);
After executing Add(ref this.Root)
, this.Root
will not be null.
However, in TypeScript, passing parameters by reference is not supported. Consider this code:
private addAux(node: Node): void {
if (node === undefined) {
node = new Node();
}
}
addAux(this._root);
Even after calling addAux(this._root)
, this._root
will remain undefined because a copy of it is passed into addAux
.
Is there a way to achieve the same functionality as the ref
keyword in C# in TypeScript?