0

I have been looking for a while on how I can make this work. For instance:

CheckPerformance(***Loop***);    

    private void CheckPerformance(***Function myFunctionName***)
    {
        sw.Start();
        myFunctionName(200);
        sw.Stop();
        Console.WriteLine("Elapsed in check: {0}", sw.Elapsed);
    }

    private void Loop(int n)
    {
        for (int i = 0; i < n; i++)
        {
            int number = i; 
        }
    }

How would I be able to pass Loop as the argument to dynamically put functions into CheckPerformance() like this stop watch profiler? Even if you give me a name for this type of thing I can look it up.

Thanks!

i773
  • 75
  • 1
  • 11
  • 1
    Well that's just not true... – Charleh Jul 09 '15 at 23:46
  • @wazaaaap Nope, completely wrong! – DavidG Jul 09 '15 at 23:46
  • 1
    This question has been asked a fair few times - check this one out http://stackoverflow.com/questions/3622160/c-sharp-passing-function-as-argument – Charleh Jul 09 '15 at 23:46
  • With respect to the people who try to help others this is not exactly an exact question that has been asked before, but why not try to explain it? I clearly did not understand it, and I already did look at the the answers and could not understand it. – i773 Jul 09 '15 at 23:59
  • Your question shows zero evidence of having done any research. Programmers are expected to be able to generalize; just because no one else has ever asked a question in which they want to pass a method named `Loop()` to another method named `CheckPerformance()`, that doesn't mean that all those other questions with the phrase "function as an argument" in the title aren't actually the exact same question. If you think they are different, it's up to _you_ to clearly explain what research you've done and what it is about the related questions you don't understand and still need help with. – Peter Duniho Jul 10 '15 at 00:12

1 Answers1

2

You are passing a function of type void (int) which in C# is:

Action<int>

So, replace your ***Function myFunctionName*** with Action<int> myFunctionName and you should be good. The Action type takes from 0 to several generic parameters, corresponding to the types of the arguments.

If your function returned something other than void, you would use the Func<...> generic type. The last generic argument would be the return type.

Los Frijoles
  • 4,771
  • 5
  • 30
  • 49
  • Have I told you how amazing you are!! I was Was looking at Action and func but had no real idea what the difference was! – i773 Jul 09 '15 at 23:50
  • **I dont know which function I'm going to pass! I want to pass one outof two.** How would I go about passing function as parameter when I don't know which one I'll be passing from a list of 2. Say two functions newline() and sameline(). How would I pass it from thisprimaryfunction(somevar,newline()) or thisprimaryfunction(somevar,sameline()) . How could I conveniently pass functions like this? Both are void types. I don't need return values. – Siddhant Rimal May 01 '16 at 03:30
  • what about **int function(void)** ?? – Siddhant Rimal May 01 '16 at 04:56
  • `void fn(void)` is simply `Action`. `int fn(void)` would be `Func`. – Los Frijoles May 01 '16 at 04:57