Main Page | Report this Page
.NET DotNet Forum Index  »  Visual C# Forum  »  delegate -= is Remove() or RemoveAll()...
Page 1 of 1    

delegate -= is Remove() or RemoveAll()...

Author Message
Ryan Liu...
Posted: Wed Nov 04, 2009 9:17 pm
Guest
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...
Posted: Wed Nov 04, 2009 11:01 pm
Guest
Ryan Liu wrote:
Quote:
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()?

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();

?
 
Rich P...
Posted: Thu Nov 05, 2009 5:20 pm
Guest
Quote:
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...
Posted: Thu Nov 05, 2009 5:42 pm
Guest
Rich P wrote:
Quote:
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.

Quote:
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...
Posted: Thu Nov 05, 2009 6:25 pm
Guest
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.

Rich

*** Sent via Developersdex http://www.developersdex.com ***
 
 
Page 1 of 1    
All times are GMT - 5 Hours
The time now is Thu Nov 26, 2009 7:55 am