public delegate void MyDelegateEventHandler(); public class Event { private MyDelegateEventHandler myDelegateEventHandler; public event MyDelegateEventHandler MyDelegate { add { this.myDelegateEventHandler += value; } remove { this.myDelegateEventHandler -= value; } } }
public delegate void MyDelegateEventHandler(); public class Event { public event MyDelegateEventHandler myDelegate; }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace ConsoleApp14 { class Program { public static TextBox textBox; public static Button button; static void Main(string[] args) { Form form = new Form(); TextBox textBox = new TextBox(); Button button = new Button(); form.Controls.Add(textBox); form.Controls.Add(button); textBox.Width = 400; button.Top = 100; button.Click += Button_Click; form.ShowDialog(); } private static void Button_Click(object sender, EventArgs e) { textBox.Text = DateTime.Now.ToString(); } } }
namespace ConsoleApp15 { class Program { static void Main(string[] args) { Customer customer = new Customer(); Waitor waitor = new Waitor(); customer.Order += waitor.Serve; customer.Eat(); customer.Pay(); } } public delegate void OrderEventHandler(Customer customer, OrderEventArgs e); public class Customer { public int Money { get; set; } public event OrderEventHandler Order; public void Pay() { Console.WriteLine($"OK,{Money} dollars"); } public void Eat() { Console.WriteLine("Let's go to the KFC..."); Console.WriteLine("Stand in front of the waitor..."); Console.WriteLine("A hamburger,Please..."); OnOrder(); } protected void OnOrder() { OrderEventArgs orderEventArgs = new OrderEventArgs(); orderEventArgs.Snack = "Hamburger"; orderEventArgs.Size = "large"; this.Order.Invoke(this, orderEventArgs); } } public class OrderEventArgs : EventArgs { public string Snack { get; set; } public string Size { get; set; } } class Waitor { public void Serve(Customer customer, OrderEventArgs e) { Console.WriteLine($"Here is your snack {e.Snack}"); int price = 20; switch (e.Size) { case "large": price *= 2; break; case "small": price *= 1; break; default: break; } customer.Money += price; } } }