PC Review


Reply
Thread Tools Rate Thread

add and remove methods to delegate

 
 
Tony Johansson
Guest
Posts: n/a
 
      1st Dec 2007
Hello!!

I have a simple program below copied fram a book.
The program works and there is no problem with it.

The last line printed in this program writes Goodnight which come from
new Greeting(SayGoodnight);

Now to my question I see that I can remove an delegete instance by using for
example this
ourGreeting -= myGreeting; and add by using +=

How do I remove for example new Greeting(SayGoodnight);
from ourGreeting. Is that really possible without creating an delegete
instance like
Greeting nightGreeting = new Greeting(SayGoodnight); and then use this
construction
ourGreeting -= nightGreeting ;

using System;

class MulticastTester
{
delegate void Greeting();

public static void Main()
{
Greeting myGreeting = new Greeting(SayThankYou);
Console.WriteLine("My single greeting:");
myGreeting();

Greeting yourGreeting = new Greeting(SayGoodMorning);
Console.WriteLine("\nYour single greeting:");
yourGreeting();

Greeting ourGreeting = myGreeting + yourGreeting;
Console.WriteLine("\nOur multicast greeting");
ourGreeting();

ourGreeting += new Greeting(SayGoodnight);
Console.WriteLine("\nMulticast greeting which includes Goodnight:");
ourGreeting();

ourGreeting = ourGreeting - yourGreeting;
Console.WriteLine("\nMulticast greeting without you greeting:");
ourGreeting();

ourGreeting -= myGreeting;
Console.WriteLine("\nSingle greeting without your greeting and my
greeting:");
ourGreeting();
}

public static void SayThankYou()
{ Console.WriteLine("Thank you!"); }

public static void SayGoodMorning()
{ Console.WriteLine("Goodmorning!"); }

public static void SayGoodnight()
{ Console.WriteLine("Goodnight!"); }
}

//Tony


 
Reply With Quote
 
 
 
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      1st Dec 2007
Tony Johansson <(E-Mail Removed)> wrote:

<snip>

> How do I remove for example new Greeting(SayGoodnight);
> from ourGreeting. Is that really possible without creating an delegete
> instance like
> Greeting nightGreeting = new Greeting(SayGoodnight); and then use this
> construction
> ourGreeting -= nightGreeting ;


Well, you could do it in one go:

ourGreeting -= new Greeting(SayGoodnight);

That will work fine.

There's no way of doing it without constructing a delegate instance
though.

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
 
Reply With Quote
 
Ben Voigt [C++ MVP]
Guest
Posts: n/a
 
      3rd Dec 2007

"Jon Skeet [C# MVP]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Tony Johansson <(E-Mail Removed)> wrote:
>
> <snip>
>
>> How do I remove for example new Greeting(SayGoodnight);
>> from ourGreeting. Is that really possible without creating an delegete
>> instance like
>> Greeting nightGreeting = new Greeting(SayGoodnight); and then use this
>> construction
>> ourGreeting -= nightGreeting ;

>
> Well, you could do it in one go:
>
> ourGreeting -= new Greeting(SayGoodnight);
>
> That will work fine.
>
> There's no way of doing it without constructing a delegate instance
> though.


Although C# 2 and later will do that for you, so you can write simply:

ourGreeting -= SayGoodnight;

There is still an invisible delegate instance created though.

>
> --
> Jon Skeet - <(E-Mail Removed)>
> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
> World class .NET training in the UK: http://iterativetraining.co.uk



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
delegate and methods access level puzzlecracker Microsoft C# .NET 2 6th Oct 2008 12:28 AM
Serialization will not deserialize delegates to non-public methods. [HOWTO FIND DELEGATE] Martijn B Microsoft Dot NET Framework 1 3rd Aug 2007 04:46 PM
Anonymous Methods and Delegate Declaration Andrew Robinson Microsoft C# .NET 3 25th Apr 2007 02:44 AM
How to remove a given delegate from the delegate invocation list mehdi Microsoft C# .NET 2 18th Feb 2007 11:02 PM
Why not: Covariant Parameter- and Contravariant Return-Types in Delegate-Methods? Matz Microsoft C# .NET 3 18th Nov 2004 05:27 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 11:25 PM.