0

when I doing a coding I come across a function like this =>

public RelayCommand(Action<object> execute): this(execute, null)

I really dont know about the "this" key word usage here

Adriano Carneiro
  • 57,693
  • 12
  • 90
  • 123
Prageeth godage
  • 4,054
  • 3
  • 29
  • 45
  • `this` in that context usually means call another constructor of *this* class with these arguments. It is a way of reducing code duplication – Hunter McMillen May 09 '13 at 03:29

3 Answers3

5

It's constructor chaining. this(execute, null) calls another constructor defined in that class which takes an Action<object> and some other value. For example:

class Whatever
{
    public Whatever() : this("string arg") {}  // calls Whatever(string)

    public Whatever(string something) {}
}
Ed S.
  • 122,712
  • 22
  • 185
  • 265
1

This particular use of this keyword lets you call one constructor from the other, presumably to supply a default argument. You can "fold" both constructors into one by applying default parameter values:

public RelayCommand(Action<object> execute, string name = null) {
    ...
}
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
0

This is referring to an overloaded version of the current constructor. Basically the two constructors are chained together which can be good for avoiding duplicate code in constructors

TGH
  • 38,769
  • 12
  • 102
  • 135