From 090fb95a0ed685cc849181cc60c98a48cbf88153 Mon Sep 17 00:00:00 2001 From: Marcello Lamonaca Date: Tue, 10 Aug 2021 09:55:00 +0200 Subject: [PATCH] Improve notes on Events & Delegates --- DotNet/C#/C#.md | 53 ++++++++++++++++++++----------------------------- 1 file changed, 21 insertions(+), 32 deletions(-) diff --git a/DotNet/C#/C#.md b/DotNet/C#/C#.md index 8ea77b1..2024b4a 100644 --- a/DotNet/C#/C#.md +++ b/DotNet/C#/C#.md @@ -33,7 +33,7 @@ to native code later. ```cs // comment /* multi line comment */ -// / single line xml comment (docstring) +/// single line xml comment (docstring) /** multi line xml string (docsting) */ ``` @@ -2091,13 +2091,13 @@ Any method from any accessible class or struct that matches the delegate type ca This makes it possible to programmatically change method calls, and also plug new code into existing classes. ```cs -// dedlegate definition -public delegate Type DelegateName(Type param, ...); // can take any method with specidied type params and return type -public delegate bool Predicate(T obj); +// delegate definition +public delegate Type Delegate(Type param, ...); // can take any method with specidied type params and return type +public delegate Type Delegate(T param); // generic delegate // delegate creation -var delegate = new Delegate(method); // explicit creation, useful when compiler cannot infer delegate type -Delegate delegate = method; // implicit creation +var delegate = new Delegate(Method); // explicit creation, useful when compiler cannot infer delegate type +Delegate delegate = Method; // implicit creation ``` ### [Multicast Delegates](https://docs.microsoft.com/en-us/dotnet/api/system.multicastdelegate) @@ -2105,9 +2105,9 @@ Delegate delegate = method; // implicit creation **Multicast Delegares** are delegates that can have more than one element in their invocation list. ```cs -Delegate multicastDelegate = method1 + method2 + ...; // multicast delegate creation -multicastDelegate += method; // add method to delegate -multicastDelegate -= method; // remove method from delegate +Delegate multicastDelegate = Method1 + Method2 + ...; // multicast delegate creation +multicastDelegate += Method; // add method to delegate +multicastDelegate -= Method; // remove method from delegate ``` **NOTE**: Delegate removal behaves in a potentially surprising way if the delegate removed refers to multiple methods. @@ -2119,7 +2119,7 @@ Invoking a delegate with a single target method works as though the code had cal Invoking a multicast delegate is just like calling each of its target methods in turn. ```cs -Delegate delegate = method; +Delegate delegate = Method; delegate(args); // use delegat as standard method delegate.DynamicInvoke(argsArray); // Dynamically invokes the method represented by the current delegate. @@ -2175,10 +2175,11 @@ The class who raises events is called _Publisher_, and the class who receives th Typically, a publisher raises an event when some action occurred. The subscribers, who are interested in getting a notification when an action occurred, should register with an event and handle it. ```cs +public delegate void EventDelegate(object sender, CustomEventArgs args); // called on event trigger + public class Publisher { - - public event Delegate Event; + public event EventDelegate Event; // A derived class should always call the On method of the base class to ensure that registered delegates receive the event. public virtual void OnEvent(Type param) @@ -2193,19 +2194,15 @@ public class Publisher ```cs public class Subscriber { - Publisher publisher = new Publisher(); public Subscriber() { - publisher.Event += eh_Event; // register handler (+= syntax) + publisher.OnEvent += Handler; // register handler (+= syntax) } - // event handler, handles the event because it matches the signature of the Event delegate. - public Delegate eh_Event() - { - // act - } + // event handler, matches the signature of the Event delegate. + public Type Handler() { /* act */ } } ``` @@ -2232,22 +2229,17 @@ public class Subsciber public Subscriber() { - publisher.Event += eh_Event; // register handler (+= syntax) + publisher.OnEvent += Handler; // register handler (+= syntax) } - public void eh_Event(object sender, EventArgs e) - { - // act - } + public Type Handler(object sender, EventArgs e) { /* act */ } } ``` ### Custom Event Args ```cs -public class CustomEventArgs : EventArgs { - // custom attributes -} +public class CustomEventArgs : EventArgs { } public class Publisher { @@ -2265,13 +2257,10 @@ public class Subsciber public Subscriber() { - publisher.Event += eh_Event; // register handler (+= syntax) + publisher.OnEvent += Handler; // register handler (+= syntax) } - public void eh_Event(object sender, CustomEventArgs e) - { - // act - } + public Type Handler(object sender, CustomEventArgs e) { /* act */ } } ```