0

I know about "This" keyword and what is it working. but what is this using for?

public ReactiveProperty() : this(default(T))
{

}

I have seen this in UniRx Project. I just don't know the "This" keyword front of constructor. I googled it but there is nothing to catch. does anyone know?

DeadDreams
  • 53
  • 5

1 Answers1

1

This syntax is used to call another constructor defined in the class. Example from the docs:

class Coords
{
    public Coords() : this(0, 0) // calls Coords(int x, int y) with x = 0 and y = 0
    {  }

    public Coords(int x, int y)
    {
        X = x;
        Y = y;
    }

    public int X { get; set; }
    public int Y { get; set; }

    public override string ToString() => $"({X},{Y})";
}
var p1 = new Coords();
Console.WriteLine($"Coords #1 at {p1}");
// Output: Coords #1 at (0,0)

var p2 = new Coords(5, 3);
Console.WriteLine($"Coords #2 at {p2}");
// Output: Coords #2 at (5,3)
Guru Stron
  • 102,774
  • 10
  • 95
  • 132
  • thanx man. I was involved in this for several days. and i didn't know what it is that to right search about it. – DeadDreams Jan 13 '23 at 19:44
  • @DeadDreams Was glad to help! As for closure - you should not worry, it is not directed at you in anyway and does not mean that you should have known what to search, it just means that your question has answers in another one (as title says). – Guru Stron Jan 14 '23 at 07:28