Using the Func as mentioned above works but there are also delegates that do the same task and also define intent within the naming:
public delegate double MyFunction(double x);
public double Diff(double x, MyFunction f)
{
double h = 0.0000001;
return (f(x + h) - f(x)) / h;
}
public double MyFunctionMethod(double x)
{
// Can add more complicated logic here
return x + 10;
}
public void Client()
{
double result = Diff(1.234, x => x * 456.1234);
double secondResult = Diff(2.345, MyFunctionMethod);
}
You can use the delegate in .NET 3.5 as the parameter in your Func method. The RunTheMethod delegate allows you to specify a method that takes a number of parameters of a specific type and returns a single argument of a specific type. Here is an example that should work:Func
public class Class1
{
public int Method1(string input)
{
//... do something
return 0;
}
public int Method2(string input)
{
//... do something different
return 1;
}
public bool RunTheMethod(Func<string, int> myMethodName)
{
//... do stuff
int i = myMethodName("My String");
//... do more stuff
return true;
}
public bool Test()
{
return RunTheMethod(Method1);
}
}
It sounds like you want a :Func<T>
T GetCachedValue<T>(string key, Func<T> method) {
T value;
if(!cache.TryGetValue(key, out value)) {
value = method();
cache[key] = value;
}
return value;
}
The caller can then wrap this in many ways; for simple functions:
int i = GetCachedValue("Foo", GetNextValue);
...
int GetNextValue() {...}
or where arguments are involved, a closure:
var bar = ...
int i = GetCachedValue("Foo", () => GetNextValue(bar));
You cannot pass a value-returning method as an , because Action must take a parameter Action<T> and return nothing (i.e. T).void
You can work around this by passing a lambda that calls your method and ignores its output:
Action empBasicAction = () => GetEmployeeBasicDetails(0);
I am not sure what you are trying to achieve here. But to answer your question, you can pass methods as parameters using delegates. Here is an example:
public class ScriptA
{
public delegate void MethodOneDelegate(int a, int b);
public void MethodOne(int a, int b)
{
Console.WriteLine(a + b);
}
}
public static class ScriptB
{
public static void StaticMethod(ScriptA.MethodOneDelegate function, int a, int b)
{
function(a, b);
}
}
public static void Main()
{
ScriptA scriptA = new ScriptA();
ScriptB.StaticMethod(scriptA.MethodOne, 1, 2);
}
There is alternative solutions, you can take a look at System.Func and System.Action.
Func<> Delegates are what you are looking for to pass methods in another method. I wrote a small example below, you can apply it according to your needs. Func<> is a generic delegate. It can be Func<int,string> Func<int,int,bool> and so on. The last one represents the return type and the others represent the input parameter types of method.
static void Main(string[] args)
{
MethodB(MethodA);
Console.ReadLine();
}
static string MethodA(string message) //Func<string,string>
{
return message;
}
static void MethodB(Func<string, string> method)
{
Console.WriteLine(method("I am MethodA"));
}
For more detailed information, you can check this link => https://www.tutorialsteacher.com/csharp/csharp-func-delegate
Your original code can still work. How people will call it is what changes when you have parameters:
With_Stopwatch(MethodWithoutParameter);
With_Stopwatch(() => MethodWithParameters(param1, param2));
you can also call the method with parameters with the second syntax:
With_Stopwatch(() => MethodWithoutParameter());
With_Stopwatch(() => MethodWithParameters(param1, param2));
Update: if you want the return value, you can change your function to take a measureThis instead of an Action:Func<T>
public T measureThis<T>(Stopwatch sw, Func<T> funcToMeasure)
{
sw.Start();
T returnVal = funcToMeasure();
sw.Stop();
return returnVal;
}
Stopwatch sw = new Stopwatch();
int result = measureThis(sw, () => FunctionWithoutParameters());
Console.WriteLine("Elapsed: {0}, result: {1}", sw.Elapsed, result);
double result2 = meashreThis(sw, () => FuncWithParams(11, 22));
Console.WriteLine("Elapsed: {0}, result: {1}", sw.Elapsed, result);