neontapir
-3
Q:

C# delegate

    using System;

	public class CargoAircraft
    {
      	// Create a delegate type (no return no arguments)
        public delegate void CheckQuantity();
		// Create an instance of the delegate type
        public CheckQuantity ProcessQuantity;

        public void ProcessRequirements()
        {
          // Call the instance delegate
          // Will invoke all methods mapped
            ProcessQuantity();
        }
    }

    public class CargoCounter
    {
        public void CountQuantity() { }
    }

    class Program
    {
        static void Main(string[] args)
        {
            CargoAircraft cargo = new CargoAircraft();
            CargoCounter cargoCounter = new CargoCounter();
			
          	// Map a method to the created delegate
            cargo.ProcessQuantity += cargoCounter.CountQuantity;
          	
          	// Will call instance delegate invoking mapped methods
            cargo.ProcessRequirements();
        }
    }
}
10
// Create the Delgate method.
public delegate void Del(string message);

// Create a method for a delgate.
public static void DelegateMethod(string message)
{
  Console.WriteLine(message);
}

// Instatiate the delegate.
Del hadler = DelegateMethod;

// Call the delegate.
hadler("Hello World");

// Output
// Hello World
2
using System;
namespace MVC
{
    public class Book
    {
        // declare a delegate for the bookpricechanged event
        public delegate void BookPriceChangedHandler(objectsender,
        BookPriceChangedEventArgs e);
        
        // declare the bookpricechanged event using the bookpricechangeddelegate
        public event BookPriceChangedHandlerBookPriceChanged;
        
        // instance variable for book price
        object _bookPrice;
        
        // property for book price
        public object BookPrice
        {
            set
            {
                // set the instance variable
                _bookPrice=value;
                
                // the price changed so fire the event!
                OnBookPriceChanged();
            }
        }
        
        // method to fire price canged event delegate with proper name
        // this is the method our observers should be implenting!
        protected void OnBookPriceChanged()
        {
            BookPriceChanged(this, new BookPriceChangedEventArgs(_bookPrice));
        }
    }
}
0

New to Communities?

Join the community