C# method pointer like in C++ -
this question has answer here:
- passing around member functions in c# 7 answers
in c++ able create method pointer without knowing on instance called, in c# can't - need instance on delegate creation.
this i'm looking :
here code msdn
using system; using system.windows.forms; public class name { private string instancename; public name(string name) { this.instancename = name; } public void displaytoconsole() { console.writeline(this.instancename); } public void displaytowindow() { messagebox.show(this.instancename); } } public class testtestdelegate { public static void main() { name testname = new name("koani"); action showmethod = testname.displaytowindow; showmethod(); } }
but want :
public class testtestdelegate { public static void main() { name testname = new name("koani"); action showmethod = name.displaytowindow; testname.showmethod(); } }
you can create delegate takes instance parameter:
name testname = new name("koani"); action<name> showmethod = name => name.displaytowindow(); showmethod(testname);
Comments
Post a Comment