0

I'm deepening my knowledge in C# in particular MVVM. In an example from the internet, I found an implementation of the ICommand interface where I came across a syntax that I couldn't find a reference and I can't understand exactly what it does...

My question is, what exacly is the " : this (execute, null)" afther the paramiters of the Method below:

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

I don't know what is a ":" after method paramiters and why it is there

Here is the complete sample:

public class RelayCommand : ICommand
{
    readonly Action<object> _execute;
    readonly Predicate<object> _canExecute;

    public RelayCommand(Action<object> execute, Predicate<object> canExecute)
    {
        if (execute == null)
            throw new NullReferenceException("execute");

        _execute = execute;
        _canExecute = canExecute;
    }

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

    }

    public event EventHandler CanExecuteChanged
    {
        add { CommandManager.RequerySuggested += value; }
        remove { CommandManager.RequerySuggested -= value; }
    }

    public bool CanExecute(object parameter)
    {
        return _canExecute == null ? true : _canExecute(parameter);
    }

    public void Execute(object parameter)
    {
        _execute.Invoke(parameter);
    }
}
  • Constructors aren't quite like other methods. You need to construct an object before you do anything. The `:` indicates that a different constructor should be run before the rest of the method body. – Jeremy Lakeman Nov 12 '21 at 04:35

0 Answers0