Im folgenden Beispiel für eine Konsolenanwendung wird die Veranstaltung ist wie folgt definiert:
public delegate void PurchaseHandler(object obj, PurchaseArgs args);
public event PurchaseHandler OnPurchaseMade;
Nachdem ich mich ein wenig umgehört habe, habe ich den Eindruck, dass dies ein wenig " " sein könnte. C#2 ".
Gibt es einen kürzeren Weg, um dies mit C#3 und C#4 auszudrücken?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TestEvents288202
{
class Program
{
static void Main(string[] args)
{
Product product1 = Product.LoadProduct(222);
EmailManager.NotifyAdministrator(product1);
product1.OnPurchaseMade += new Product.PurchaseHandler(NotifyUser);
product1.Purchase();
Product product2 = Product.LoadProduct(333);
EmailManager.NotifyAdministrator(product2);
product2.OnPurchaseMade += new Product.PurchaseHandler(NotifyUser);
product2.Purchase();
Console.ReadLine();
}
static void NotifyUser(object sender, PurchaseArgs e)
{
((Product)sender).Log();
Console.WriteLine(e.Message);
}
}
public static class EmailManager
{
public static void NotifyAdministrator(Product product)
{
product.OnPurchaseMade += new Product.PurchaseHandler(SendEmail);
}
public static void SendEmail(object sender, PurchaseArgs e)
{
Product product = sender as Product;
Console.WriteLine("Just sent e-mail to administrator notifying of purchase of article {0}", product.ProductNumber);
}
}
public class PurchaseArgs : EventArgs
{
public string Message { get; set; }
public PurchaseArgs(string message)
{
Message = message;
}
}
public class Product
{
public int ProductNumber { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public delegate void PurchaseHandler(object obj, PurchaseArgs args);
public event PurchaseHandler OnPurchaseMade;
public static Product LoadProduct(int productNumber)
{
List<Product> products = new List<Product>();
products.Add(new Product { ProductNumber = 111, Name = "Intel CPU", Description = "Newest model, very fast." });
products.Add(new Product { ProductNumber = 222, Name = "Philips Monitor", Description = "22-inch, very nice." });
products.Add(new Product { ProductNumber = 333, Name = "Sony Camera", Description = "10 Megapixels, sharp pictures." });
return products.Where(p => p.ProductNumber == productNumber).SingleOrDefault();
}
public void Purchase()
{
PurchaseArgs purchaseArgs = new PurchaseArgs(String.Format("The product \"{0}\" was just purchased.", this.Name));
OnPurchaseMade(this, purchaseArgs);
}
public void Log()
{
Console.WriteLine("Log: #{0} purchased.", this.ProductNumber);
}
}
}