I'm trying to make my own understandable example of what the method RelayCommand is doing in the following code:
return new RelayCommand(p => MessageBox.Show("It worked."));
the constructor is this:
public RelayCommand(Action<object> execute, Predicate<object> canExecute)
{
if (execute == null)
throw new ArgumentNullException("execute");
_execute = execute;
_canExecute = canExecute;
}
In my last question Jon Skeet pointed me in the right direction so I could get an example (below) that does what I wanted (pass some method name as in MessageBox.Show above). But the problem is, to get it to work, I had to take out all the lambda syntax (Action, Predicate, etc.), which is what I am trying to understand.
Is there a way to change the working example so it performs the same function but uses the lambda-syntax as a parameter as in the commented out lines below?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TestLambda24
{
class Program
{
static void Main(string[] args)
{
int[] numbers = { 6, 3, 7, 4, 8 };
//Console.WriteLine("The addition result is {0}.", Tools.ProcessNumbers(p => Tools.AddNumbers, numbers));
Console.WriteLine("The addition result is {0}.", Tools.ProcessNumbers(Tools.AddNumbers, numbers));
//Console.WriteLine("The multiplication result is {0}.", Tools.ProcessNumbers(p => Tools.MultiplyNumbers, numbers));
Console.WriteLine("The multiplication result is {0}.", Tools.ProcessNumbers(Tools.MultiplyNumbers, numbers));
Console.ReadLine();
}
}
class Tools
{
public static int ProcessNumbers(Func<int[], int> theMethod, int[] integers)
{
return theMethod(integers);
}
public static int AddNumbers(int[] numbers)
{
int result = 0;
foreach (int i in numbers)
{
result += i;
}
return result;
}
public static int MultiplyNumbers(int[] numbers)
{
int result = 1;
foreach (int i in numbers)
{
result *= i;
}
return result;
}
}
}