locked
C# 2.0 delegate method group conversion

    Question

  • Folks,

    As you know C# 2.0 includes a feature called method group conversion that simplifies the syntax used to assign a method to a delegate. Method group conversion allows you to assign the name of a method to a delegate, without the use new or explicitly invoking the delegate's constructor. For example, assume a delegate called StrMod that is declared like this:

    delegate string StrMod(string str);

    In the past, to assign a method called removeSpaces( ) to that delegate, you would use a statement like this:

    strOp = new StrMod(removeSpaces);

    With the addition of method group conversion, this statement can now be written more compactly:

    strOp = removeSpaces;

    This syntax is both shorter and more to the point, fine! However, I'd like to know the performance-wise benefits/tradeoffs of this syntax.

     

    Thanks

     

    Tuesday, September 28, 2010 6:59 AM

Answers

  • I have checked the same using ldasm.exe and find, the new approach is internally using new. So no need to worry about the performance trade off between the two syntaxes.

     


    Thanks,
    A.m.a.L
    [MVP Visual C#]
    Dot Net Goodies
    Don't hate the hacker, hate the code
    • Proposed as answer by Louis.fr Tuesday, September 28, 2010 9:02 AM
    • Marked as answer by Larcolais Gong Tuesday, October 05, 2010 3:45 AM
    Tuesday, September 28, 2010 7:14 AM
  • Hi recherché,

     

    Thank you for your post.

     

    Based on your description, please check the following code snippets.

     

    // Creates one delegate for each method. For the instance method, an

    // instance (mySC) must be supplied. For the static method, use the

    // class name.

    mySampleClass mySC = new mySampleClass();

    myMethodDelegate myD1 = new myMethodDelegate( mySC.myStringMethod );

    myMethodDelegate myD2 = new myMethodDelegate( mySampleClass.mySignMethod );

     

    See: http://msdn.microsoft.com/en-us/library/system.delegate.aspx for more detail information.

     

    If you have any issues, please feel free to let me know.

    CHEERS,

    Larcolais


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Larcolais Gong Tuesday, October 05, 2010 3:45 AM
    Thursday, September 30, 2010 5:25 AM

All replies

  • I have checked the same using ldasm.exe and find, the new approach is internally using new. So no need to worry about the performance trade off between the two syntaxes.

     


    Thanks,
    A.m.a.L
    [MVP Visual C#]
    Dot Net Goodies
    Don't hate the hacker, hate the code
    • Proposed as answer by Louis.fr Tuesday, September 28, 2010 9:02 AM
    • Marked as answer by Larcolais Gong Tuesday, October 05, 2010 3:45 AM
    Tuesday, September 28, 2010 7:14 AM
  • Hi recherché,

     

    Thank you for your post.

     

    Based on your description, please check the following code snippets.

     

    // Creates one delegate for each method. For the instance method, an

    // instance (mySC) must be supplied. For the static method, use the

    // class name.

    mySampleClass mySC = new mySampleClass();

    myMethodDelegate myD1 = new myMethodDelegate( mySC.myStringMethod );

    myMethodDelegate myD2 = new myMethodDelegate( mySampleClass.mySignMethod );

     

    See: http://msdn.microsoft.com/en-us/library/system.delegate.aspx for more detail information.

     

    If you have any issues, please feel free to let me know.

    CHEERS,

    Larcolais


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Larcolais Gong Tuesday, October 05, 2010 3:45 AM
    Thursday, September 30, 2010 5:25 AM
  • Thanks for the reply.
    Tuesday, October 05, 2010 5:36 AM