c# - Eliminate duplicate code introduced by Func and Action -


there generic function return type.

tresult invoke<tresult>(func<string, tresult> callback) {     string message = generate_some_string();     return callback(message); } 

and there similar 1 without return type since there no func.

void invoke(action<string> callback) {     string message = generate_some_string();     callback(message); } 

but these duplicate code. once invoke changes, invoke has changed correspondingly. there way eliminate duplicate code?

thanks, jim

you try this:

void invoke(action<string> callback) {     invoke<int>(s=>{callback(s);return 0;}); } 

that way logic stays in func version , action version should never have change.

another option create tofunc conversion routine, , place onus on callers change action func:

public static func<tin, tresult> tofunc<tin, tresult>(this action<tin> a) {     return input =>     {         a(input);         return default(tresult);     }; } 

Comments

Popular posts from this blog

javascript - DIV "hiding" when changing dropdown value -

Does Firefox offer AppleScript support to get URL of windows? -

android - How to install packaged app on Firefox for mobile? -