ref anonymous method inside the method

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • William Stacey [C# MVP]

    ref anonymous method inside the method

    It would be handy to be able to ref "this" from inside an AM such as:

    (string s)
    {
    Console.Writeli ne(s);
    DoSomething(thi s);
    }

    So treating am like a method of a class (which it is). Currently we have no
    context to know so you have to pass that as state which seems kinda
    redundant. Naturally, "this" would need to be named something else. Maybe
    "that"? ;/ Double-double this-this, double-double that-that, double-this
    double-that, double-double this-that.

    --
    William Stacey [C# MVP]



  • Jon Skeet [C# MVP]

    #2
    Re: ref anonymous method inside the method

    William Stacey [C# MVP] <william.stacey @gmail.comwrote :
    It would be handy to be able to ref "this" from inside an AM such as:
    >
    (string s)
    {
    Console.Writeli ne(s);
    DoSomething(thi s);
    }
    >
    So treating am like a method of a class (which it is). Currently we have no
    context to know so you have to pass that as state which seems kinda
    redundant. Naturally, "this" would need to be named something else. Maybe
    "that"? ;/ Double-double this-this, double-double that-that, double-this
    double-that, double-double this-that.
    I'm not sure I understand what you mean - you already *do* have access
    to "this":

    using System;

    delegate void SimpleDelegate( );

    class Test
    {
    string name;

    Test(string name)
    {
    this.name = name;
    }

    public static void Main()
    {
    Test t = new Test("Jon");
    SimpleDelegate am = t.Foo();
    am();
    }

    SimpleDelegate Foo()
    {
    string s = "Hello";
    return delegate
    {
    Console.WriteLi ne (s);
    DoSomething(thi s);
    };
    }

    static void DoSomething(Tes t t)
    {
    Console.WriteLi ne (t.name);
    }
    }

    (I'm sure there's a general purpose parameterless void delegate
    somewhere, but I can't remember what it is. I originally used
    ThreadStarty, but thought it might confuse things.)


    My guess is that you're aware of the above and you're actually after
    something else, but it's not clear to me at the moment what that is...

    --
    Jon Skeet - <skeet@pobox.co m>
    http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
    If replying to the group, please do not mail me too

    Comment

    • William Stacey [C# MVP]

      #3
      Re: ref anonymous method inside the method

      A way for "this" (or some other name) to refer to the anonymous delegate
      itself, not the containing class. So for example, inside my anonymous
      method I want to call an Unregister() handler to unregister the delegate
      from some dispatcher. I need to have a ref to the delegate. And there is
      the challenge. I have no way to refer to myself inside the method unless I
      also pass that information to the delegate. Like:

      // Some anonoumous method.
      (string s, delegate self)
      {
      // use s.
      if ( isTrue )
      UnRegister(self );
      }

      This would work, but junks up the delegate signatures.

      --
      William Stacey [C# MVP]

      "Jon Skeet [C# MVP]" <skeet@pobox.co mwrote in message
      news:MPG.200a2b 89136c376398d74 4@msnews.micros oft.com...
      | William Stacey [C# MVP] <william.stacey @gmail.comwrote :
      | It would be handy to be able to ref "this" from inside an AM such as:
      | >
      | (string s)
      | {
      | Console.Writeli ne(s);
      | DoSomething(thi s);
      | }
      | >
      | So treating am like a method of a class (which it is). Currently we have
      no
      | context to know so you have to pass that as state which seems kinda
      | redundant. Naturally, "this" would need to be named something else.
      Maybe
      | "that"? ;/ Double-double this-this, double-double that-that,
      double-this
      | double-that, double-double this-that.
      |
      | I'm not sure I understand what you mean - you already *do* have access
      | to "this":
      |
      | using System;
      |
      | delegate void SimpleDelegate( );
      |
      | class Test
      | {
      | string name;
      |
      | Test(string name)
      | {
      | this.name = name;
      | }
      |
      | public static void Main()
      | {
      | Test t = new Test("Jon");
      | SimpleDelegate am = t.Foo();
      | am();
      | }
      |
      | SimpleDelegate Foo()
      | {
      | string s = "Hello";
      | return delegate
      | {
      | Console.WriteLi ne (s);
      | DoSomething(thi s);
      | };
      | }
      |
      | static void DoSomething(Tes t t)
      | {
      | Console.WriteLi ne (t.name);
      | }
      | }
      |
      | (I'm sure there's a general purpose parameterless void delegate
      | somewhere, but I can't remember what it is. I originally used
      | ThreadStarty, but thought it might confuse things.)
      |
      |
      | My guess is that you're aware of the above and you're actually after
      | something else, but it's not clear to me at the moment what that is...
      |
      | --
      | Jon Skeet - <skeet@pobox.co m>
      | http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
      | If replying to the group, please do not mail me too


      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: ref anonymous method inside the method

        William Stacey [C# MVP] <william.stacey @gmail.comwrote :
        A way for "this" (or some other name) to refer to the anonymous delegate
        itself, not the containing class.
        Ah, right. I'm with you now - although I can't recall ever wanting this
        behaviour myself (in .NET 2.0 or other languages that have the concept
        of closures).

        I'll have a think about any interesting ways of doing it...

        --
        Jon Skeet - <skeet@pobox.co m>
        http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
        If replying to the group, please do not mail me too

        Comment

        Working...