Prev: ChangeWindowMessageFilter und LateBinding
Next: After EndAccept new Socket has no LocalEndpoit-Compact Framewo
From: Geri Broser on 25 Oct 2009 05:55 ....when the following program works the same with and without it: ===================================================================== namespace DelegateSample_Concert { class Program { static void Main( string[] args ) { Concert concert = new Concert(); Visitor evan = new Visitor() { Name = "Evan" }; Visitor sharon = new Visitor() { Name = "Sharon" }; Visitor pradesh = new Visitor() { Name = "Pradesh" }; concert.CancellationHandler += evan.ReceiveE_Mail; concert.CancellationHandler += sharon.ReceiveSnailMail; concert.CancellationHandler += pradesh.ReceivePhoneCall; //--------------------------------------------------------------------- System.Diagnostics.Trace.WriteLine( new string( '-', 69 ) ); concert.Cancel(); // Output: // --------------------------------------------------------------------- // Evan received an E-Mail concerning cancellation due to bad weather! // Sharon got a letter concerning cancellation due to bad weather! // Pradesh was called concerning cancellation due to bad weather! } } // Program class Concert { public delegate void Cancellation_Handler( string cancellationReason ); // The program works the same with each of the following two lines: //public event Cancellation_Handler CancellationHandler; public Cancellation_Handler CancellationHandler { get; set; } public void Cancel() { if ( CancellationHandler != null ) CancellationHandler( "bad weather" ); } } // Concert class Visitor { public string Name { get; set; } public void ReceiveE_Mail( string cancellationReason ) { System.Diagnostics.Trace.WriteLine( Name + " received an E-Mail concerning cancellation due to " + cancellationReason + "!" ); } public void ReceivePhoneCall( string cancellationReason ) { System.Diagnostics.Trace.WriteLine( Name + " was called concerning cancellation due to " + cancellationReason + "!" ); } public void ReceiveSnailMail( string cancellationReason ) { System.Diagnostics.Trace.WriteLine( Name + " got a letter concerning cancellation due to " + cancellationReason + "!" ); } } // Visitor } // ns:DelegateSample_Concert ===================================================================== Geri
From: Jeroen Mostert on 25 Oct 2009 06:22 Geri Broser wrote: > // The program works the same with each of the following two lines: > //public event Cancellation_Handler CancellationHandler; > public Cancellation_Handler CancellationHandler { get; set; } > The latter allows any client to *replace* the delegate entirely, overwriting all earlier assigned handlers. You don't want that. It also gives them access to all the delegate's fields and methods from outside the object, which is again not what you want. Public delegate fields are also not recognized as events by designers and Intellisense, but that's minor in comparison. In short, the event keyword isn't there to enable things you otherwise couldn't do, but to prevent clients form doing things they otherwise could. When "event" is used, the only operations available to clients are += and -=. -- J.
From: Geri Broser on 25 Oct 2009 07:12 Jeroen Mostert schrieb: > Geri Broser wrote: >> // The program works the same with each of the following two lines: >> //public event Cancellation_Handler CancellationHandler; >> public Cancellation_Handler CancellationHandler { get; set; } >> > The latter allows any client to *replace* the delegate entirely, > overwriting all earlier assigned handlers. You don't want that. It also > gives them access to all the delegate's fields and methods from outside > the object, which is again not what you want. Public delegate fields are > also not recognized as events by designers and Intellisense, but that's > minor in comparison. > > In short, the event keyword isn't there to enable things you otherwise > couldn't do, but to prevent clients form doing things they otherwise > could. When "event" is used, the only operations available to clients > are += and -=. > Thank you for the quick reply. Prevent replacing to make it foolproof is clear. I found that already when I tried... concert.CancellationHandler *=* evan.ReceiveE_Mail; ....with the event field version. (Which leads to "Compiler Error CS0070", for those who are interested.) However, I didn't consider this as remarkable, since fools should not program anyway. (OK, that's the theory... ;-) Recognizing in designers and Intellisense is comprehensible, too. But, what do you mean by "gives them access to all the delegate's fields and methods"? A delegate is a function pointer, i.e. it represents a method. I do not see any "delegate's [=method's] fields and methods" there. Geri
From: Jeroen Mostert on 25 Oct 2009 07:28 Geri Broser wrote: <snip> > But, what do you mean by "gives them access to all the delegate's fields > and methods"? A delegate is a function pointer, i.e. it represents a > method. I do not see any "delegate's [=method's] fields and methods" there. > Then your Intellisense is broken. :-) A delegate is an instance of a class derived from System.Delegate, with plenty of magic in the CLR and the language to back it up (so you hardly ever notice it is). -- J.
From: Geri Broser on 25 Oct 2009 07:39 Jeroen Mostert schrieb: > Geri Broser wrote: >> // The program works the same with each of the following two lines: >> //public event Cancellation_Handler CancellationHandler; >> public Cancellation_Handler CancellationHandler { get; set; } >> > The latter allows any client to *replace* the delegate entirely, > overwriting all earlier assigned handlers. You don't want that. It also > gives them access to all the delegate's fields and methods from outside > the object, which is again not what you want. Public delegate fields are > also not recognized as events by designers and Intellisense, but that's > minor in comparison. > > In short, the event keyword isn't there to enable things you otherwise > couldn't do, but to prevent clients form doing things they otherwise > could. When "event" is used, the only operations available to clients > are += and -=. > [Please ignore my previous post. I wanted to withdraw it, but it didn't appear in the group 20 min after I sent it. Is this normal for this server?] Thank you for the quick reply. Prevent replacing to make it foolproof is clear. I found that already when I tried... concert.CancellationHandler *=* evan.ReceiveE_Mail; ....with the event field version. (Which leads to "Compiler Error CS0070", for those who are interested.) However, I didn't consider this as remarkable, since fools should not program anyway. (OK, that's the theory... ;-) Recognizing in designers and Intellisense is comprehensible, too. I had to think about, and try in code, what you meant by "gives them access to all the delegate's fields and methods". I thought a delegate is a function pointer, i.e. it represents a method, so there are no "delegate's [=method's] fields and methods". But then it became obvious that a delegate is the content of a variable of a delegate type - which is a reference type. So, it inherits Equals(), ToString(), ... from "Object" and more from others. Thank you Geri
|
Next
|
Last
Pages: 1 2 3 Prev: ChangeWindowMessageFilter und LateBinding Next: After EndAccept new Socket has no LocalEndpoit-Compact Framewo |