Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

delegate -= is Remove() or RemoveAll()

1 view
Skip to first unread message

Ryan Liu

unread,
Nov 4, 2009, 9:17:21 PM11/4/09
to
Hi,

I look at msdn class lib, it mentions == and != op for Delegate, but does
not mention operators += and -=

My question is: for -=, is that calling Delegate.Remove() or RemoveAll()?

Thanks,
Ryan

Peter Duniho

unread,
Nov 4, 2009, 11:01:38 PM11/4/09
to

Hint: what does the following code do when you execute it...

Action action1 = () => Console.WriteLine("called"),
action2 = null;

action2 += action1;
action2 += action1;
action2 -= action1;

action2();

?

Ryan Liu

unread,
Nov 5, 2009, 3:14:50 PM11/5/09
to
Thanks, it will out put single "called", so "-=" is Remove() , not
RemoveAll()


"Peter Duniho" <no.pet...@no.nwlink.spam.com> wrote in message
news:u3%23ltycX...@TK2MSFTNGP06.phx.gbl...

Rich P

unread,
Nov 5, 2009, 5:20:16 PM11/5/09
to

> Action action1 = () => Console.WriteLine("called"),
> action2 = null;
>
> action2 += action1;
> action2 += action1;
> action2 -= action1;
>
> action2();

>?

Just asking about the lambda notation above. I understand that => is
short hand for an anonymous method. Is this correct? If yes, then what
would the long hand version of the above example look like ? (without
the lambda notation)

Thankds,

Rich

*** Sent via Developersdex http://www.developersdex.com ***

Peter Duniho

unread,
Nov 5, 2009, 5:42:13 PM11/5/09
to
Rich P wrote:
>> Action action1 = () => Console.WriteLine("called"),
>> action2 = null;
>>
>> action2 += action1;
>> action2 += action1;
>> action2 -= action1;
>>
>> action2();
>
>> ?
>
> Just asking about the lambda notation above. I understand that => is
> short hand for an anonymous method. Is this correct?

Sort of. The => is specifically for writing a lambda expression.
Depending on context, this can either be compiled into an anonymous
method or an actual expression.

So, technically it's not really "short hand" for anything. It simply
"is" a lambda expression. But yes, in a way you can think of it as
short-hand for an anonymous method, because in that context, that's what
it winds up creating.

> If yes, then what
> would the long hand version of the above example look like ? (without
> the lambda notation)

Action action1 = delegate() { Console.WriteLine("called"); };

As you can see, for this specific usage, there's very little difference.

Pete

Rich P

unread,
Nov 5, 2009, 6:25:21 PM11/5/09
to
so - it looks like => is short hand for a delegate expression /
anonymous method. I haven't quite got the lambda concept down yet. I
will have to keep following posts and examples.
0 new messages