Advertisement
wingman007

C#EventDriven_Human_2а

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