• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 899
  • Last Modified:

Lambda expressions and the Action<Object> delegate

The Action<Object> delegate is used in RelayCommands, DelegateCommands.

Code snippets below:
  public class SimpleDelegateCommand : ICommand
    {
        Action<object> _executeDelegate;

          public SimpleDelegateCommand(Action<object> actionDelegate)
        {
            _executeDelegate = actionDelegate;
        }

etc.

 public class Methods
    {
       // A method that doesn't had any parameters
        public static void NoParameterMethod()
        {
            MessageBox.Show("The Method that doesn't take a parameter was Called");
        }

The question I have is: Why is the following acceptable?

       private ICommand _test2Command = new SimpleDelegateCommand(y => Methods.NoParameterMethod());

That is:
given that the SimpleDelegateCommand needs and Action<object> delegate to be passed as an argument, how can a method that doesn't take any parameters be allowed by the delegate?
0
quentinA
Asked:
quentinA
1 Solution
 
Bob LearnedCommented:
If I look at the description for Action<T> delegate, I see a clue:

Action<T> Delegate
http://msdn.microsoft.com/en-us/library/018hxwa8.aspx

"You can use this delegate to pass a method as a parameter without explicitly declaring a custom delegate"

Methods.NoParameterMethod is a method, and the Action<T> delegate can be used to pass a method as an argument.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

Tackle projects and never again get stuck behind a technical roadblock.
Join Now