c# - Method running another method -
is possible run method using run()?
public void a(int a) { // method1 } public void b(int b) { // method2 } //how run code below public void run(? b(23)) <--can change or b { b(23); } edit: if want return value methods?
public static int a(int a) { // method1 } public static int b(int b) { // method2 } //how run code below public static int run(? b(23)) <--can change or b { b(23); }
there's few ways this. 1 way define run method this:
public void run(action action) { action.invoke(); } then execute 1 method or other using:
run(() => a(3)); run(() => b(5)); if want return value, can either in 2 steps:
int r = 0; run(() => r = b(3)); or switch func<>:
public t run<t>(func<t> method) { return method.invoke(); } and call likewise:
var r = run(() => b(3));
Comments
Post a Comment