4

I want to do some performance measurement for a method that does some work with int arrays, so I wrote the following class:

public class TimeKeeper
{
    public TimeSpan Measure(Action[] actions)
    {
        var watch = new Stopwatch();
        watch.Start();
        foreach (var action in actions)
        {
            action();
        }
        return watch.Elapsed;
    }
}

But I can not call the Measure mehotd for the example below:

var elpased = new TimeKeeper();
elpased.Measure(
    () =>
    new Action[]
        {
            FillArray(ref a, "a", 10000),
            FillArray(ref a, "a", 10000),
            FillArray(ref a, "a", 10000)
        });

I get the following errors:

Cannot convert lambda expression to type 'System.Action[]' because it is not a delegate type
Cannot implicitly convert type 'void' to 'System.Action'
Cannot implicitly convert type 'void' to 'System.Action'
Cannot implicitly convert type 'void' to 'System.Action'

Here is the method that works with arrays:

private void FillArray(ref int[] array, string name, int count)
{
    array = new int[count];

    for (int i = 0; i < array.Length; i++)
    {
        array[i] = i;
    }

    Console.WriteLine("Array {0} is now filled up with {1} values", name, count);
}

What I am doing wrong?

3
  • Why are you using a Lambda expression? Can't you just call Measure(actions)? Commented Jun 9, 2012 at 22:21
  • I tried that but I still get Cannot implicitly convert type 'void' to 'System.Action' Commented Jun 9, 2012 at 22:24
  • 1
    Just a quick note, why are you using a void function with a ref parameter, rather than returning an array? In general, it's not good practice to use ref parameters if you're not returning anything. In your case, you'd have to change your code to use Func<> instead of Action<>. Commented Jun 9, 2012 at 22:49

2 Answers 2

10

Measure expects its first argument to be an Action[], not a lambda that returns an Action[]. And the actions array expects you to pass delegates, while you are in fact calling FillArray.

You probably want this:

elpased.Measure
(
    new Action[]
    {
        () => FillArray(ref a, "a", 10000),
        () => FillArray(ref a, "a", 10000),
        () => FillArray(ref a, "a", 10000)
    }
);
Sign up to request clarification or add additional context in comments.

3 Comments

I tried that but I still get Cannot implicitly convert type 'void' to 'System.Action'
No you didn't try that. Look carefully.
@cicada hmm I copy pasted exactly, I get 3 errors ; expected, I tried to place semiclones but no success!
0

Cannot implicitly convert type 'void' to 'System.Action'

This array initializer is expected to fill out the array with Actions returned by the FillArray method which is not the case.

new Action[]
        {
            FillArray(ref a, "a", 10000),
            FillArray(ref a, "a", 10000),
            FillArray(ref a, "a", 10000)
        });

Change the FillArray accordingly to return an Action instead of void

1 Comment

Yes this answers only one part of the question not all of it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.