1

I thought I'd got my head around delegates, in that it creates a reference to a method and then can be referenced by 'external' code (another dll/project).

Now I'm using MVVM and I see this thing called RelayCommand which appears as if it is passing a method as a parameter in the same way a delegate is passed. How is this possible?

The code I'm looking at is

        public RelayCommand AddUserCommand { get; set; }

        public ViewModelMain()
        {
            this.AddUserCommand = new RelayCommand(AddUser, null);
        }

        void AddUser(object parameter)
        {
            //logic
        }

and the RelayCommand object is

public class RelayCommand : ICommand
{
    public RelayCommand(Action<object> execute, Predicate<object> canExecute)
    {
         //logic
    }
}

So since there is no delegate can I assume some trickery is occurring with the Action<> object?

Dave
  • 8,163
  • 11
  • 67
  • 103
  • 2
    [`Action`](http://msdn.microsoft.com/en-us/library/018hxwa8.aspx) **is** a delegate. – Jon Feb 26 '13 at 10:19
  • Blimey - I've been looking up what RelayCommand inherited from and was thinking it was to do with the interfaces it used! Right! Thank you (please move your comment to an answer, this is all I needed as I'm happy to research further) – Dave Feb 26 '13 at 10:21
  • No need really. You can just delete the question. – Jon Feb 26 '13 at 10:21
  • Before you delete, it might be worth looking at the Enterprise Library Prism/Composite Application Block. It has a DelegateCommand which provides some more functionality such as RaiseCanExecuteChanged. I'm not suggesting you use Enterprise Library just for this command, but it's worth a look. – BenCr Feb 26 '13 at 10:25
  • I now have other answers (and since they made the effort it would be rude of me to delete, otherwise I would have :) ). Thank you BenCr and Jon – Dave Feb 26 '13 at 10:26

1 Answers1

1

Action<object> is a delegate. When you write:

new RelayCommand(AddUser, null)

the compiler rewites it as:

new RelayCommand(new Action<object>(AddUser), null)

Which creates the reference to a method, as you're expecting.

Sean
  • 60,939
  • 11
  • 97
  • 136
  • Sean, this is great, but you've hit another point; How do you know this is what the compiler does? I can't see this detail on the [MSDN site](http://msdn.microsoft.com/en-us/library/018hxwa8.aspx) – Dave Feb 26 '13 at 10:23
  • Thank you Sean this is great. I appreciate your time. – Dave Feb 26 '13 at 10:32
  • @DaveRook: It's on the C# language spec itself. In this context, `AddUser` is called a *method group* (because in the general case there may be many overloads with the same name). The C# lang spec, 13.6 "Method group conversions" details how there is an implicit conversion from a method group to any compatible delegate type. – Jon Feb 26 '13 at 10:32
  • @Jon, is the spec something you refer to regularly (or regularly enough) that you (personally) would recommend keeping a copy to look things up? So far, I've only used the MSDN site, but it appears I should use both (MSDN and this documentation). Would you agree? – Dave Feb 26 '13 at 10:35
  • 1
    @DaveRook: Yes. I 've even got the spec in my dropbox so it's always available on all of my machines. – Jon Feb 26 '13 at 10:35