Advertisement
wingman007

C#EventDriven_Human

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