Anonymous Method Sample

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Nick

    Anonymous Method Sample

    Hi,
    I was just playing around and found that the following two ways of
    calling the same method using the WaitCallback method that takes an
    object parameter. Can any one explain why there is no problem with the
    first call even though the delegate doesn't take in the Object
    Parameter??? What is the complier doing "under the covers".

    Thanks for any help
    Nick

    Object state = new Object();
    // Using WaitCallback
    // Note no parameter passed in
    WaitCallback oInvokeNoParame ter = delegate {
    MyMethodCall(); };
    oInvokeNoParame ter.Invoke(stat e);


    WaitCallback oInvokeAParamet er = delegate(Object oObject) {
    MyMethodCall(); };
    oInvokeAParamet er.Invoke(state );

    .......
    static void MyMethodCall()
    {
    Console.WriteLi ne("MyMethodCal l has been invoked");
    }

  • Dustin Campbell

    #2
    Re: Anonymous Method Sample

    Hi,
    I was just playing around and found that the following two ways of
    calling the same method using the WaitCallback method that takes an
    object parameter. Can any one explain why there is no problem with
    the
    first call even though the delegate doesn't take in the Object
    Parameter??? What is the complier doing "under the covers".
    Thanks for any help
    Nick
    Object state = new Object();
    // Using WaitCallback
    // Note no parameter passed in
    WaitCallback oInvokeNoParame ter = delegate {
    MyMethodCall(); };
    oInvokeNoParame ter.Invoke(stat e);
    WaitCallback oInvokeAParamet er = delegate(Object oObject)
    {
    MyMethodCall(); };
    oInvokeAParamet er.Invoke(state );
    ......
    static void MyMethodCall()
    {
    Console.WriteLi ne("MyMethodCal l has been invoked");
    }
    The compiler is still generating the same WaitCallback delegate with a state
    parameter under the covers. However, with an anonymous method, you aren't
    required to actually declare the parameters unless you use them in the anonymous
    method's body.

    Best Regards,
    Dustin Campbell
    Developer Express Inc.


    Comment

    • Brian Gideon

      #3
      Re: Anonymous Method Sample

      Nick,

      This is called delegate inference. See the following article for more
      information.



      Brian

      Nick wrote:
      Hi,
      I was just playing around and found that the following two ways of
      calling the same method using the WaitCallback method that takes an
      object parameter. Can any one explain why there is no problem with the
      first call even though the delegate doesn't take in the Object
      Parameter??? What is the complier doing "under the covers".
      >
      Thanks for any help
      Nick
      >
      Object state = new Object();
      // Using WaitCallback
      // Note no parameter passed in
      WaitCallback oInvokeNoParame ter = delegate {
      MyMethodCall(); };
      oInvokeNoParame ter.Invoke(stat e);
      >
      >
      WaitCallback oInvokeAParamet er = delegate(Object oObject) {
      MyMethodCall(); };
      oInvokeAParamet er.Invoke(state );
      >
      ......
      static void MyMethodCall()
      {
      Console.WriteLi ne("MyMethodCal l has been invoked");
      }

      Comment

      • William Stacey [C# MVP]

        #4
        Re: Anonymous Method Sample

        Also, if method was "static void MyMethodCall(ob ject value){}",
        you could also do:

        WaitCallback cb = MyMethodCall;
        cb(null);

        --
        William Stacey [C# MVP]

        "Nick" <nick_tucker@ho tmail.comwrote in message
        news:1161866896 .722159.151250@ k70g2000cwa.goo glegroups.com.. .
        | Hi,
        | I was just playing around and found that the following two ways of
        | calling the same method using the WaitCallback method that takes an
        | object parameter. Can any one explain why there is no problem with the
        | first call even though the delegate doesn't take in the Object
        | Parameter??? What is the complier doing "under the covers".
        |
        | Thanks for any help
        | Nick
        |
        | Object state = new Object();
        | // Using WaitCallback
        | // Note no parameter passed in
        | WaitCallback oInvokeNoParame ter = delegate {
        | MyMethodCall(); };
        | oInvokeNoParame ter.Invoke(stat e);
        |
        |
        | WaitCallback oInvokeAParamet er = delegate(Object oObject) {
        | MyMethodCall(); };
        | oInvokeAParamet er.Invoke(state );
        |
        | ......
        | static void MyMethodCall()
        | {
        | Console.WriteLi ne("MyMethodCal l has been invoked");
        | }
        |


        Comment

        Working...