<p>What are the differences between delegates and an events? Don't both hold references to functions that can be executed?</p>

[{'answer_id': 29170, 'author': 'mmcdole', 'author_id': 2635, 'author_profile': 'https://Stackoverflow.com/users/2635', 'pm_score': 9, 'selected': True, 'text': '<p>An <strong>Event</strong> declaration adds a layer of abstraction and protection on the <strong>delegate</strong> instance.  This protection prevents clients of the delegate from resetting the delegate and its invocation list and only allows adding or removing targets from the invocation list.</p>\n'}, {'answer_id': 29206, 'author': 'Jorge Córdoba', 'author_id': 2695, 'author_profile': 'https://Stackoverflow.com/users/2695', 'pm_score': 7, 'selected': False, 'text': '<p>In addition to the syntactic and operational properties, there\'s also a semantical difference. </p>\n\n<p>Delegates are, conceptually, function templates; that is, they express a contract a function must adhere to in order to be considered of the "type" of the delegate.</p>\n\n<p>Events represent ... well, events. They are intended to alert someone when something happens and yes, they adhere to a delegate definition but they\'re not the same thing.</p>\n\n<p>Even if they were exactly the same thing (syntactically and in the IL code) there will still remain the semantical difference. In general I prefer to have two different names for two different concepts, even if they are implemented in the same way (which doesn\'t mean I like to have the same code twice).</p>\n'}, {'answer_id': 3567000, 'author': 'Paul Hill', 'author_id': 215321, 'author_profile': 'https://Stackoverflow.com/users/215321', 'pm_score': 3, 'selected': False, 'text': '<p>You can also use events in interface declarations, not so for delegates.</p>\n'}, {'answer_id': 7216043, 'author': 'supercat', 'author_id': 363751, 'author_profile': 'https://Stackoverflow.com/users/363751', 'pm_score': 2, 'selected': False, 'text': "<p>An event in .net is a designated combination of an Add method and a Remove method, both of which expect some particular type of delegate.  Both C# and vb.net can auto-generate code for the add and remove methods which will define a delegate to hold the event subscriptions, and add/remove the passed in delegagte to/from that subscription delegate.  VB.net will also auto-generate code (with the RaiseEvent statement) to invoke the subscription list if and only if it is non-empty; for some reason, C# doesn't generate the latter.</p>\n\n<p>Note that while it is common to manage event subscriptions using a multicast delegate, that is not the only means of doing so.  From a public perspective, a would-be event subscriber needs to know how to let an object know it wants to receive events, but it does not need to know what mechanism the publisher will use to raise the events.  Note also that while whoever defined the event data structure in .net apparently thought there should be a public means of raising them, neither C# nor vb.net makes use of that feature.</p>\n"}, {'answer_id': 15968041, 'author': 'vibhu', 'author_id': 1171544, 'author_profile': 'https://Stackoverflow.com/users/1171544', 'pm_score': 5, 'selected': False, 'text': '<p>Here is another good link to refer to.\n<a href="http://csharpindepth.com/Articles/Chapter2/Events.aspx" rel="nofollow noreferrer">http://csharpindepth.com/Articles/Chapter2/Events.aspx</a></p>\n\n<p>Briefly, the take away from the article - Events are encapsulation over delegates.</p>\n\n<p>Quote from article:</p>\n\n<blockquote>\n  <p>Suppose events didn\'t exist as a concept in C#/.NET. How would another class subscribe to an event? Three options:</p>\n  \n  <ol>\n  <li><p>A public delegate variable</p></li>\n  <li><p>A delegate variable backed by a property</p></li>\n  <li><p>A delegate variable with AddXXXHandler and RemoveXXXHandler methods</p></li>\n  </ol>\n  \n  <p>Option 1 is clearly horrible, for all the normal reasons we abhor public variables.</p>\n  \n  <p>Option 2 is slightly better, but allows subscribers to effectively override each other - it would be all too easy to write someInstance.MyEvent = eventHandler; which would replace any existing event handlers rather than adding a new one. In addition, you still need to write the properties.</p>\n  \n  <p>Option 3 is basically what events give you, but with a guaranteed convention (generated by the compiler and backed by extra flags in the IL) and a "free" implementation if you\'re happy with the semantics that field-like events give you. Subscribing to and unsubscribing from events is encapsulated without allowing arbitrary access to the list of event handlers, and languages can make things simpler by providing syntax for both declaration and subscription.</p>\n</blockquote>\n'}, {'answer_id': 24712324, 'author': 'faby', 'author_id': 3515042, 'author_profile': 'https://Stackoverflow.com/users/3515042', 'pm_score': 7, 'selected': False, 'text': '<p>To understand the differences you can look at this 2 examples</p>\n<p>Example with Delegates (in this case, an Action - that is a kind of delegate that doesn\'t return a value)</p>\n<pre><code>public class Animal\n{\n    public Action Run {get; set;}\n\n    public void RaiseEvent()\n    {\n        if (Run != null)\n        {\n            Run();\n        }\n    }\n}\n</code></pre>\n<p>To use the delegate, you should do something like this:</p>\n<pre><code>Animal animal= new Animal();\nanimal.Run += () =&gt; Console.WriteLine(&quot;I\'m running&quot;);\nanimal.Run += () =&gt; Console.WriteLine(&quot;I\'m still running&quot;) ;\nanimal.RaiseEvent();\n</code></pre>\n<p>This code works well but you could have some weak spots.</p>\n<p>For example, if I write this:</p>\n<pre><code>animal.Run += () =&gt; Console.WriteLine(&quot;I\'m running&quot;);\nanimal.Run += () =&gt; Console.WriteLine(&quot;I\'m still running&quot;);\nanimal.Run = () =&gt; Console.WriteLine(&quot;I\'m sleeping&quot;) ;\n</code></pre>\n<p>with the last line of code, I have overridden the previous behaviors just with one missing <code>+</code> (I have used <code>=</code> instead of <code>+=</code>)</p>\n<p>Another weak spot is that every class which uses your <code>Animal</code> class can invoke the delegate directly. For example, <code>animal.Run()</code> or <code>animal.Run.Invoke()</code> are valid outside the Animal class.</p>\n<p>To avoid these weak spots you can use <code>events</code> in c#.</p>\n<p>Your Animal class will change in this way:</p>\n<pre><code>public class ArgsSpecial : EventArgs\n{\n    public ArgsSpecial (string val)\n    {\n        Operation=val;\n    }\n\n    public string Operation {get; set;}\n} \n\npublic class Animal\n{\n    // Empty delegate. In this way you are sure that value is always != null \n    // because no one outside of the class can change it.\n    public event EventHandler&lt;ArgsSpecial&gt; Run = delegate{} \n        \n    public void RaiseEvent()\n    {  \n         Run(this, new ArgsSpecial(&quot;Run faster&quot;));\n    }\n}\n</code></pre>\n<p>to call events</p>\n<pre><code> Animal animal= new Animal();\n animal.Run += (sender, e) =&gt; Console.WriteLine(&quot;I\'m running. My value is {0}&quot;, e.Operation);\n animal.RaiseEvent();\n</code></pre>\n<p>Differences:</p>\n<ol>\n<li>You aren\'t using a public property but a public field (using events, the compiler protects your fields from unwanted access)</li>\n<li>Events can\'t be assigned directly. In this case, it won\'t give rise to the previous error that I have showed with overriding the behavior.</li>\n<li>No one outside of your class can raise or invoke the event. For example, <code>animal.Run()</code> or <code>animal.Run.Invoke()</code> are invalid outside the Animal class and will produce compiler errors.</li>\n<li>Events can be included in an interface declaration, whereas a field cannot</li>\n</ol>\n<p>Notes:</p>\n<p>EventHandler is declared as the following delegate:</p>\n<pre><code>public delegate void EventHandler (object sender, EventArgs e)\n</code></pre>\n<p>it takes a sender (of Object type) and event arguments. The sender is null if it comes from static methods.</p>\n<p>This example, which uses <code>EventHandler&lt;ArgsSpecial&gt;</code>, can also be written using <code>EventHandler</code> instead.</p>\n<p>Refer <a href="http://msdn.microsoft.com/it-it/library/db0etb8x(v=vs.110).aspx" rel="noreferrer">here</a> for documentation about EventHandler</p>\n'}, {'answer_id': 28581895, 'author': 'Miguel Gamboa', 'author_id': 1140754, 'author_profile': 'https://Stackoverflow.com/users/1140754', 'pm_score': 3, 'selected': False, 'text': '<p>What a great misunderstanding between events and delegates!!! A delegate specifies a TYPE (such as a <code>class</code>, or an <code>interface</code> does), whereas an event is just a kind of MEMBER (such as fields, properties, etc). And, just like any other kind of member an event also has a type. Yet, in the case of an event, the type of the event must be specified by a delegate.  For instance, you CANNOT declare an event of a type defined by an interface. </p>\n\n<p>Concluding, we can make the following <strong>Observation: the type of an event MUST be defined by a delegate</strong>. This is the main relation between an event and a delegate and is described in the section <strong>II.18  Defining events</strong> of <a href="http://www.ecma-international.org/publications/standards/Ecma-335.htm" rel="noreferrer">ECMA-335 (CLI) Partitions I to VI</a>:</p>\n\n<blockquote>\n  <p>In typical usage, the TypeSpec (if present) <strong>identifies a delegate</strong> whose signature matches the arguments passed to the event’s fire method.</p>\n</blockquote>\n\n<p>However, <strong>this fact does NOT imply that an event uses a backing delegate field</strong>. In truth, an event may use a backing field of any different data structure type of your choice. If you implement an event explicitly in C#, you are free to choose the way you store the <strong>event handlers</strong> (note that <strong>event handlers</strong> are instances of the <strong>type of the event</strong>, which in turn is mandatorily a <strong>delegate type</strong>---from the previous <strong>Observation</strong>). But, you can store those event handlers (which are delegate instances) in a data structure such as a <code>List</code> or a <code>Dictionary</code> or any other else, or even in a backing delegate field. But don’t forget that it is NOT mandatory that you use a delegate field.</p>\n'}, {'answer_id': 29595337, 'author': 'Trevor', 'author_id': 258982, 'author_profile': 'https://Stackoverflow.com/users/258982', 'pm_score': 3, 'selected': False, 'text': '<p>NOTE: If you have access to <a href="https://rads.stackoverflow.com/amzn/click/com/0672336901" rel="nofollow noreferrer" rel="nofollow noreferrer">C# 5.0 Unleashed</a>, read the "Limitations on Plain Use of Delegates" in Chapter 18 titled "Events" to understand better the differences between the two.</p>\n\n<hr>\n\n<p>It always helps me to have a simple, concrete example. So here\'s one for the community. First I show how you can use delegates alone to do what Events do for us. Then I show how the same solution would work with an instance of <code>EventHandler</code>. And then I explain why we DON\'T want to do what I explain in the first example. This post was inspired by <a href="http://csharpindepth.com/Articles/Chapter2/Events.aspx" rel="nofollow noreferrer">an article</a> by John Skeet.</p>\n\n<p>Example 1: Using public delegate</p>\n\n<p>Suppose I have a WinForms app with a single drop-down box. The drop-down is bound to an <code>List&lt;Person&gt;</code>. Where Person has properties of Id, Name, NickName, HairColor. On the main form is a custom user control that shows the properties of that person. When someone selects a person in the drop-down the labels in the user control update to show the properties of the person selected.</p>\n\n<p><img src="https://i.stack.imgur.com/gfk5j.png" alt="enter image description here"></p>\n\n<p>Here is how that works. We have three files that help us put this together:</p>\n\n<ul>\n<li>Mediator.cs -- static class holds the delegates</li>\n<li>Form1.cs -- main form </li>\n<li>DetailView.cs -- user control shows all details</li>\n</ul>\n\n<p>Here is the relevant code for each of the classes:</p>\n\n<pre><code>class Mediator\n{\n    public delegate void PersonChangedDelegate(Person p); //delegate type definition\n    public static PersonChangedDelegate PersonChangedDel; //delegate instance. Detail view will "subscribe" to this.\n    public static void OnPersonChanged(Person p) //Form1 will call this when the drop-down changes.\n    {\n        if (PersonChangedDel != null)\n        {\n            PersonChangedDel(p);\n        }\n    }\n}\n</code></pre>\n\n<p>Here is our user control:</p>\n\n<pre><code>public partial class DetailView : UserControl\n{\n    public DetailView()\n    {\n        InitializeComponent();\n        Mediator.PersonChangedDel += DetailView_PersonChanged;\n    }\n\n    void DetailView_PersonChanged(Person p)\n    {\n        BindData(p);\n    }\n\n    public void BindData(Person p)\n    {\n        lblPersonHairColor.Text = p.HairColor;\n        lblPersonId.Text = p.IdPerson.ToString();\n        lblPersonName.Text = p.Name;\n        lblPersonNickName.Text = p.NickName;\n\n    }\n}\n</code></pre>\n\n<p>Finally we have the following code in our Form1.cs. Here we are Calling OnPersonChanged, which calls any code subscribed to the delegate.</p>\n\n<pre><code>private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)\n{\n    Mediator.OnPersonChanged((Person)comboBox1.SelectedItem); //Call the mediator\'s OnPersonChanged method. This will in turn call all the methods assigned (i.e. subscribed to) to the delegate -- in this case `DetailView_PersonChanged`.\n}\n</code></pre>\n\n<p>Ok. So that\'s how you would get this working <em>without using events</em> and <em>just using delegates</em>. We just put a public delegate into a class -- you can make it static or a singleton, or whatever. Great. </p>\n\n<p>BUT, BUT, BUT, we do not want to do what I just described above. Because <a href="https://stackoverflow.com/questions/1180860/public-fields-versus-automatic-properties">public fields are bad</a> for many, many reason. So what are our options? As John Skeet describes, here are our options:</p>\n\n<ol>\n<li>A public delegate variable (this is what we just did above. don\'t do this. i just told you above why it\'s bad)</li>\n<li>Put the delegate into a property with a get/set (problem here is that subscribers could override each other -- so we could subscribe a bunch of methods to the delegate and then we could accidentally say <code>PersonChangedDel = null</code>, wiping out all of the other subscriptions. The other problem that remains here is that since the users have access to the delegate, they can invoke the targets in the invocation list -- we don\'t want external users having access to when to raise our events.</li>\n<li>A delegate variable with AddXXXHandler and RemoveXXXHandler methods</li>\n</ol>\n\n<p>This third option is essentially what an event gives us. When we declare an EventHandler, it gives us access to a delegate -- not publicly, not as a property, but as this thing we call an event that has just add/remove accessors.</p>\n\n<p>Let\'s see what the same program looks like, but now using an Event instead of the public delegate (I\'ve also changed our Mediator to a singleton):</p>\n\n<p>Example 2: With EventHandler instead of a public delegate</p>\n\n<p>Mediator:</p>\n\n<pre><code>class Mediator\n{\n\n    private static readonly Mediator _Instance = new Mediator();\n\n    private Mediator() { }\n\n    public static Mediator GetInstance()\n    {\n        return _Instance;\n    }\n\n    public event EventHandler&lt;PersonChangedEventArgs&gt; PersonChanged; //this is just a property we expose to add items to the delegate.\n\n    public void OnPersonChanged(object sender, Person p)\n    {\n        var personChangedDelegate = PersonChanged as EventHandler&lt;PersonChangedEventArgs&gt;;\n        if (personChangedDelegate != null)\n        {\n            personChangedDelegate(sender, new PersonChangedEventArgs() { Person = p });\n        }\n    }\n}\n</code></pre>\n\n<p>Notice that if you F12 on the EventHandler, it will show you the definition is just a generic-ified delegate with the extra "sender" object:</p>\n\n<pre><code>public delegate void EventHandler&lt;TEventArgs&gt;(object sender, TEventArgs e);\n</code></pre>\n\n<p>The User Control:</p>\n\n<pre><code>public partial class DetailView : UserControl\n{\n    public DetailView()\n    {\n        InitializeComponent();\n        Mediator.GetInstance().PersonChanged += DetailView_PersonChanged;\n    }\n\n    void DetailView_PersonChanged(object sender, PersonChangedEventArgs e)\n    {\n        BindData(e.Person);\n    }\n\n    public void BindData(Person p)\n    {\n        lblPersonHairColor.Text = p.HairColor;\n        lblPersonId.Text = p.IdPerson.ToString();\n        lblPersonName.Text = p.Name;\n        lblPersonNickName.Text = p.NickName;\n\n    }\n}\n</code></pre>\n\n<p>Finally, here\'s the Form1.cs code:</p>\n\n<pre><code>private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)\n{\n        Mediator.GetInstance().OnPersonChanged(this, (Person)comboBox1.SelectedItem);\n}\n</code></pre>\n\n<p>Because the EventHandler wants and EventArgs as a parameter, I created this class with just a single property in it:</p>\n\n<pre><code>class PersonChangedEventArgs\n{\n    public Person Person { get; set; }\n}\n</code></pre>\n\n<p>Hopefully that shows you a bit about why we have events and how they are different -- but functionally the same -- as delegates.</p>\n'}, {'answer_id': 39460456, 'author': 'Venkatesh Muniyandi', 'author_id': 4418611, 'author_profile': 'https://Stackoverflow.com/users/4418611', 'pm_score': 2, 'selected': False, 'text': '<p><strong>To define about event in simple way:</strong></p>\n\n<p>Event is a <strong>REFERENCE</strong> to a delegate with two restrictions</p>\n\n<ol>\n<li>Cannot be invoked directly</li>\n<li>Cannot be assigned values directly (e.g eventObj = delegateMethod)</li>\n</ol>\n\n<p>Above two are the weak points for delegates and it is addressed in event.  Complete code sample to show the difference in fiddler is here <a href="https://dotnetfiddle.net/5iR3fB" rel="nofollow">https://dotnetfiddle.net/5iR3fB</a> .</p>\n\n<p>Toggle the comment between Event and Delegate and client code that invokes/assign values to delegate to understand the difference</p>\n\n<p>Here is the inline code.</p>\n\n<pre><code> /*\nThis is working program in Visual Studio.  It is not running in fiddler because of infinite loop in code.\nThis code demonstrates the difference between event and delegate\n        Event is an delegate reference with two restrictions for increased protection\n\n            1. Cannot be invoked directly\n            2. Cannot assign value to delegate reference directly\n\nToggle between Event vs Delegate in the code by commenting/un commenting the relevant lines\n*/\n\npublic class RoomTemperatureController\n{\n    private int _roomTemperature = 25;//Default/Starting room Temperature\n    private bool _isAirConditionTurnedOn = false;//Default AC is Off\n    private bool _isHeatTurnedOn = false;//Default Heat is Off\n    private bool _tempSimulator = false;\n    public  delegate void OnRoomTemperatureChange(int roomTemperature); //OnRoomTemperatureChange is a type of Delegate (Check next line for proof)\n    // public  OnRoomTemperatureChange WhenRoomTemperatureChange;// { get; set; }//Exposing the delegate to outside world, cannot directly expose the delegate (line above), \n    public  event OnRoomTemperatureChange WhenRoomTemperatureChange;// { get; set; }//Exposing the delegate to outside world, cannot directly expose the delegate (line above), \n\n    public RoomTemperatureController()\n    {\n        WhenRoomTemperatureChange += InternalRoomTemperatuerHandler;\n    }\n    private void InternalRoomTemperatuerHandler(int roomTemp)\n    {\n        System.Console.WriteLine("Internal Room Temperature Handler - Mandatory to handle/ Should not be removed by external consumer of ths class: Note, if it is delegate this can be removed, if event cannot be removed");\n    }\n\n    //User cannot directly asign values to delegate (e.g. roomTempControllerObj.OnRoomTemperatureChange = delegateMethod (System will throw error)\n    public bool TurnRoomTeperatureSimulator\n    {\n        set\n        {\n            _tempSimulator = value;\n            if (value)\n            {\n                SimulateRoomTemperature(); //Turn on Simulator              \n            }\n        }\n        get { return _tempSimulator; }\n    }\n    public void TurnAirCondition(bool val)\n    {\n        _isAirConditionTurnedOn = val;\n        _isHeatTurnedOn = !val;//Binary switch If Heat is ON - AC will turned off automatically (binary)\n        System.Console.WriteLine("Aircondition :" + _isAirConditionTurnedOn);\n        System.Console.WriteLine("Heat :" + _isHeatTurnedOn);\n\n    }\n    public void TurnHeat(bool val)\n    {\n        _isHeatTurnedOn = val;\n        _isAirConditionTurnedOn = !val;//Binary switch If Heat is ON - AC will turned off automatically (binary)\n        System.Console.WriteLine("Aircondition :" + _isAirConditionTurnedOn);\n        System.Console.WriteLine("Heat :" + _isHeatTurnedOn);\n\n    }\n\n    public async void SimulateRoomTemperature()\n    {\n        while (_tempSimulator)\n        {\n            if (_isAirConditionTurnedOn)\n                _roomTemperature--;//Decrease Room Temperature if AC is turned On\n            if (_isHeatTurnedOn)\n                _roomTemperature++;//Decrease Room Temperature if AC is turned On\n            System.Console.WriteLine("Temperature :" + _roomTemperature);\n            if (WhenRoomTemperatureChange != null)\n                WhenRoomTemperatureChange(_roomTemperature);\n            System.Threading.Thread.Sleep(500);//Every second Temperature changes based on AC/Heat Status\n        }\n    }\n\n}\n\npublic class MySweetHome\n{\n    RoomTemperatureController roomController = null;\n    public MySweetHome()\n    {\n        roomController = new RoomTemperatureController();\n        roomController.WhenRoomTemperatureChange += TurnHeatOrACBasedOnTemp;\n        //roomController.WhenRoomTemperatureChange = null; //Setting NULL to delegate reference is possible where as for Event it is not possible.\n        //roomController.WhenRoomTemperatureChange.DynamicInvoke();//Dynamic Invoke is possible for Delgate and not possible with Event\n        roomController.SimulateRoomTemperature();\n        System.Threading.Thread.Sleep(5000);\n        roomController.TurnAirCondition (true);\n        roomController.TurnRoomTeperatureSimulator = true;\n\n    }\n    public void TurnHeatOrACBasedOnTemp(int temp)\n    {\n        if (temp &gt;= 30)\n            roomController.TurnAirCondition(true);\n        if (temp &lt;= 15)\n            roomController.TurnHeat(true);\n\n    }\n    public static void Main(string []args)\n    {\n        MySweetHome home = new MySweetHome();\n    }\n\n\n}\n</code></pre>\n'}, {'answer_id': 54174334, 'author': 'Weidong Shen', 'author_id': 971238, 'author_profile': 'https://Stackoverflow.com/users/971238', 'pm_score': 3, 'selected': False, 'text': '<p>Delegate is a type-safe function pointer. Event is an implementation of publisher-subscriber design pattern using delegate.</p>\n'}, {'answer_id': 64929776, 'author': 'VimNing', 'author_id': 5290519, 'author_profile': 'https://Stackoverflow.com/users/5290519', 'pm_score': 2, 'selected': False, 'text': '<p>For people live in 2020, and want a clean answer...</p>\n<h2>Definitions:</h2>\n<ul>\n<li><code>delegate</code>: defines a function pointer.</li>\n<li><code>event</code>: defines\n<ul>\n<li>(1) <em>protected</em> interfaces, <strong>and</strong></li>\n<li>(2) operations(<strong><code>+=</code></strong>, <strong><code>-=</code></strong>), <strong>and</strong></li>\n<li>(3) advantage: you don\'t need to use <code>new</code> keyword anymore.</li>\n</ul>\n</li>\n</ul>\n<p>Regarding the adjective <em>protected</em>:</p>\n<pre><code>// eventTest.SomeoneSay = null;              // Compile Error.\n// eventTest.SomeoneSay = new Say(SayHello); // Compile Error.\n</code></pre>\n<p>Also notice this section from Microsoft: <a href="https://learn.microsoft.com/en-us/dotnet/standard/events/#raising-multiple-events" rel="nofollow noreferrer">https://learn.microsoft.com/en-us/dotnet/standard/events/#raising-multiple-events</a></p>\n<h2>Code Example:</h2>\n<h3>with <code>delegate</code>:</h3>\n<pre><code>public class DelegateTest\n{\n    public delegate void Say(); // Define a pointer type &quot;void &lt;- ()&quot; named &quot;Say&quot;.\n    private Say say;\n\n    public DelegateTest() {\n        say  = new Say(SayHello);     // Setup the field, Say say, first.\n        say += new Say(SayGoodBye);\n        \n        say.Invoke();\n    }\n    \n    public void SayHello() { /* display &quot;Hello World!&quot; to your GUI. */ }\n    public void SayGoodBye() { /* display &quot;Good bye!&quot; to your GUI. */ }\n}\n</code></pre>\n<hr />\n<h3>with <code>event</code>:</h3>\n<pre><code>public class EventTest\n{\n    public delegate void Say();\n    public event Say SomeoneSay;  // Use the type &quot;Say&quot; to define event, an \n                                  // auto-setup-everything-good field for you.\n    public EventTest() {\n         SomeoneSay += SayHello;\n         SomeoneSay += SayGoodBye;\n\n         SomeoneSay();\n    }\n    \n    public void SayHello() { /* display &quot;Hello World!&quot; to your GUI. */ }\n    public void SayGoodBye() { /* display &quot;Good bye!&quot; to your GUI. */ }\n}\n</code></pre>\n<h2>Reference:</h2>\n<p><strong>Event vs. Delegate</strong> - <em>Explaining the important differences between the Event and Delegate patterns in C# and why they\'re useful.</em>: <a href="https://dzone.com/articles/event-vs-delegate" rel="nofollow noreferrer">https://dzone.com/articles/event-vs-delegate</a></p>\n'}]
['https://Stackoverflow.com/questions/29155', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2993/']