Advertisement
ivandrofly

Event vs Delegate c#

Mar 23rd, 2016
507
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.88 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace ConsoleApplication1
  8. {
  9.     class Person
  10.     {
  11.         public event Action PersonEat;
  12.         public Action PersonEatAction;
  13.         public Person()
  14.         {
  15.  
  16.         }
  17.         protected void OnPersonEat()
  18.         {
  19.             PersonEat.Invoke();
  20.             PersonEat = null;
  21.         }
  22.     }
  23.     class Program
  24.     {
  25.         private static void NotifyMe()
  26.         {
  27.             ///
  28.         }
  29.         static void Main(string[] args)
  30.         {
  31.             var person = new Person();
  32.  
  33.             // Only allowed inside type Person.
  34.             //person.PersonEat(); // not allowed
  35.             //person.PersonEat = NotifyMe; // fails
  36.  
  37.  
  38.             person.PersonEat += NotifyMe;
  39.             person.PersonEatAction();
  40.         }
  41.     }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement