Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace EventDrive2a
- {
- class Program
- {
- static void Main(string[] args)
- {
- Human person1 = new Human("Stoyan", 50);
- person1.Introduced += delegate(object sender, EventArgs e) { Console.WriteLine("I am a delegate and event handler"); };
- person1.Introduced += delegate(object sender, EventArgs e) { Console.WriteLine("I am a second event handler!"); };
- person1.Introduced += person1_Introduced;
- person1.Introduced += (object sender, EventArgs e) => Console.WriteLine("I am a lambda");
- /* example for closure!!!
- int age = 54; // it is going to be used as a "free variable" in an anonymous function.
- // closure!!!!!!! anonymous function with "free variable"
- person1.OnIntroduce = delegate() { Console.WriteLine("I am a delegate and event handler {0}", age); };
- person1.IntroduceYourSelf();
- // or
- // closure!!!!!!! anonymous function with free variable
- // closure!!!!!!! anonymous function with free variable
- person1.Introduced += delegate(object sender, EventArgs e) { Human human = (Human)sender; Console.WriteLine("I am a delgate and event handler {0}", (human.Age > age) ? "Old" : "Young"); };
- // lambda expression
- person1.Introduced += (object sender, EventArgs e) => {Human human = (Human)sender; Console.WriteLine("I am a lambda closure {0}", (human.Age > age) ? "Old" : "Young");};
- */
- Time myTime = new Time();
- // myTime.Tick += delegate(object sender, EventArgs e) { Console.WriteLine("The current time is {0}", DateTime.Now); };
- myTime.Tick += delegate(object sender, TickEventArgs e) { Console.WriteLine("The current time is {0}", e.TimeNow); };
- myTime.Start();
- }
- private static void person1_Introduced(object sender, EventArgs e)
- {
- Console.WriteLine("I am a static event handler!");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement