delegate -= is Remove() or RemoveAll()

  • Thread starter Thread starter Ryan Liu
  • Start date Start date
R

Ryan Liu

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
 
Ryan said:
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();

?
 
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
 
Rich said:
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
 
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
 
Back
Top