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

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

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

java - Cannot secure connection using TLS -