Different ways to use Delegates
Here is the example covers all different ways to use the delegate – Just copy and paste the code:
- How to use Delegate
- How to use Multicast Delegate
- How to Call specific method from Multicast Delegate with executing in given Sequence
- How to call Delegate Asynchronously
- How to use Event
using System;
using System.Collections;
using System.Text;
using System.Threading;
namespace ConsoleApplication1
{
public class Book
{
public string Name { get; set; }
public decimal Price { get; set; }
public Book(string Name, decimal Price)
{
this.Name = Name;
this.Price = Price;
}
}
public delegate void ProcessBookDelegate(Book bk);
public class Stock
{
public ArrayList Books = new ArrayList();
public delegate void NewBookAddedHandler(Book newbook);
public event NewBookAddedHandler NewBookAdded;
public void AddBook(string Name, decimal Price)
{
Book bk = new Book(Name,Price);
Books.Add(bk);
if (NewBookAdded != null)
NewBookAdded(bk);
}
public void ProcessBook(ProcessBookDelegate dele)
{
foreach (Book item in Books)
{
dele(item);
}
}
}
public class TotalStock
{
int NumberOfBooks ;
decimal TotalAmount ;
public void AddTotalToCount(Book bk)
{
NumberOfBooks++;
TotalAmount += bk.Price;
}
public void ShowTotal()
{
Console.WriteLine(NumberOfBooks.ToString() + " " + TotalAmount.ToString());
}
}
class Program
{
static void PrintBookTitalAndPrice(Book bk)
{
Console.WriteLine(bk.Name + " " + bk.Price.ToString());
}
static void PrintBookTital(Book bk)
{
Console.WriteLine(bk.Name );
}
static void Main(string[] args)
{
Stock bookstock = new Stock();
bookstock.NewBookAdded += new Stock.NewBookAddedHandler(bookstock_NewBookAdded);
bookstock.AddBook("ABC", 9);
bookstock.AddBook("ABC1", 19);
bookstock.AddBook("ABC2", 29);
bookstock.AddBook("ABC3", 39);
bookstock.AddBook("ABC4", 49);
TotalStock tstk = new TotalStock();
// Multicast delegate
ProcessBookDelegate pbd = new ProcessBookDelegate(tstk.AddTotalToCount);
pbd += new ProcessBookDelegate(PrintBookTitalAndPrice);
pbd += new ProcessBookDelegate(PrintBookTital);
bookstock.ProcessBook(pbd);
// Do not want to call the methods in the same sequance they are registered
// call the method without sequance
Delegate[] del = pbd.GetInvocationList();
del[2].DynamicInvoke(new Book("a", 9));
// Asyncronous Calling of delegate
ProcessBookDelegate pbd1 = new ProcessBookDelegate(tstk.AddTotalToCount);
// 1st way - without any endinvoide handler
IAsyncResult result = pbd1.BeginInvoke(new Book("b", 2), null, null);
pbd1.EndInvoke(result); // the end invoke will exceute once the delete call finishes
// 2nd way - without handleer but use the iscompareled to check the status
IAsyncResult result1 = pbd1.BeginInvoke(new Book("b", 2), null, null);
while(!result1.IsCompleted)
{
Thread.Sleep(100);
}
pbd1.EndInvoke(result1); // the end invoke will exceute once the delete call finishes
// 3rd way - without handler but use waitone to get tigger to the current thread
IAsyncResult result2 = pbd1.BeginInvoke(new Book("b", 2), null, null);
result2.AsyncWaitHandle.WaitOne();
pbd1.EndInvoke(result2); // the end invoke will exceute once the delete call finishes
// 4th way - with callback handler
pbd1.BeginInvoke(new Book("b", 2), new AsyncCallback(CallBack), pbd1);
Console.WriteLine("Press Enter to see total.");
Console.ReadLine();
tstk.ShowTotal();
Console.ReadLine();
}
static void bookstock_NewBookAdded(Book newbook)
{
Console.WriteLine("New Book Added: " + newbook.Name);
}
static void CallBack(IAsyncResult result)
{
ProcessBookDelegate pbd1 = (ProcessBookDelegate) result.AsyncState;
pbd1.EndInvoke(result);
Console.WriteLine("CallBack ended.");
}
}
}

