C# method pointer like in C++ -


this question has answer here:

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

Popular posts from this blog

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - UML - How would you draw a try catch in a sequence diagram? -

c++ - No viable overloaded operator for references a map -