Advertisement
wingman007

C#EventDriven_Human_3а

Sep 30th, 2014
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.61 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 EventDriven3a
  8. {
  9.     public delegate void IntroducedEventHandler(object sender, EventArgs e);
  10.     class Human
  11.     {
  12.         private string name;
  13.         private int age;
  14.  
  15.         public event IntroducedEventHandler Introduced;
  16.  
  17. //        public event IntroducedEventHandler Introduced
  18. //        {
  19. //            add { /*...*/ }
  20. //            remove { /*...*/ }
  21. //        }
  22.  
  23.         public virtual void OnIntroduced(EventArgs e)
  24.         {
  25.             if (Introduced != null)
  26.             {
  27.                 Introduced(this, e);
  28.             }    
  29.         }
  30.  
  31.         public string Name
  32.         {
  33.             get { return name; }
  34.             set { name = value; }
  35.         }
  36.  
  37.         public int Age
  38.         {
  39.             get { return age; }
  40.             set { age = value; }
  41.         }
  42.  
  43.         public Human(string name, int age)
  44.         {
  45.             this.name = name;
  46.             this.age = age;
  47.         }
  48.  
  49.         public void IntroduceYourSelf()
  50.         {
  51.             // 1. hard coded
  52.             // OnIntroduceYourSelf();
  53.  
  54.             // 2. delegate
  55.             // if (Introduced != null) {
  56.             //    Introduced(this, EventArgs.Empty);
  57.             // }
  58.  
  59.             // 3.
  60.             OnIntroduced(EventArgs.Empty);
  61.  
  62.             Console.WriteLine("I am a human! My name is {0}. I am {1} years old!", name, age);
  63.         }
  64.  
  65.         private void OnIntroduceYourSelf()
  66.         {
  67.             Console.WriteLine("I am an event handler static!");
  68.         }
  69.     }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement