Is there a more abbreviated way to define a custom event in C # 3 and C # 4?

In the following sample console application, an event is defined as follows:

public delegate void PurchaseHandler(object obj, PurchaseArgs args);
public event PurchaseHandler OnPurchaseMade;

It seems to me that after reading it may be a little " C # 2 ".

Is there a more abbreviated way to express this with C # 3 and C # 4?

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);
        }
    }
}
+3
source share
3 answers

Always define such events, do not use user delegates:

event EventHandler<EventArgsClassType> MyEventHandler;

or, if they do not accept arguments:

event EventHandler MyEventHandler;

Based on the class System.EventHandler, this provides an even signature for all events, following .NET guidelines.

, EventArgsClassType System.EventArgs.


, :

product1.OnPurchaseMade += new Product.PurchaseHandler(NotifyUser);

. , :

product1.OnPurchaseMade += NotifyUser;
+9

:

public event EventHandler<PurchaseArgs> OnPurchaseMade;

, , :

product1.OnPurchaseMade += new Product.PurchaseHandler(NotifyUser);

:

product1.OnPurchaseMade += NotifyUser;
+3

First you can use the EventHandler template to create a delegate, so your code will look like this:

public event EventHandler<PurchaseArgs> OnPurchaseMade;
+1
source

Source: https://habr.com/ru/post/1745126/


All Articles