using System;
//delegate
// it holds a reference(pointer) to a function
// used to point any funcion
public delegate void HelloFunctionDelegate(string Message);
class Program
{
static void Main(string[] args)
{
//A delegate is a type safe function pointer
HelloFunctionDelegate del = new HelloFunctionDelegate(Hello);
//Hello("Hello from Delegate");
del("Hello from Delegate");
}
public static void Hello(string strMessage)
{
Console.WriteLine(strMessage);
}
}
==================================
make reuseable
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
List<Employee> empList = new List<Employee>();
empList.Add(new Employee() { ID = 101, Name = "Mary", Salary = 5000, Experience = 5 });
empList.Add(new Employee() { ID = 102, Name = "Mike", Salary = 4000, Experience = 4 });
empList.Add(new Employee() { ID = 103, Name = "John", Salary = 6000, Experience = 6 });
empList.Add(new Employee() { ID = 104, Name = "Todd", Salary = 3000, Experience = 3 });
//IsPromotable promotable = new IsPromotable(Promote);
//Employee.PromoteEmployee(empList, promotable);
Employee.PromoteEmployee(empList, emp => emp.Experience >= 5); //using lambda expression
}
//public static bool Promote(Employee emp)
//{
// if (emp.Experience >= 5)
// {
// return true;
// }
// else
// {
// return false;
// }
//}
}
delegate bool IsPromotable(Employee empl);
class Employee
{
public int ID { get; set; }
public string Name { get; set; }
public int Salary { get; set; }
public int Experience { get; set; }
public static void PromoteEmployee(List<Employee> employeeList, IsPromotable IsEligibleToPromote)
{
foreach (Employee employee in employeeList)
{
if (IsEligibleToPromote(employee))
{
Console.WriteLine(employee.Name + " promoted");
}
}
}
}
=================================================
a multicast delegate is a delegate that references to more than one function.
using System;
using System.Collections.Generic;
public delegate void SampleDelegate();
class Program
{
static void Main()
{
//SampleDelegate del = new SampleDelegate(SampleMethodOne);
//del();
//SampleDelegate del1, del2, del3, del4;
//del1 = new SampleDelegate(SampleMethodOne);
//del2 = new SampleDelegate(SampleMethodTwo);
//del3 = new SampleDelegate(SampleMethodThree);
//del4 = del1 + del2 + del3 - del2;
//del4(); //multicast delegate
SampleDelegate del = new SampleDelegate(SampleMethodOne);
del += SampleMethodTwo; // +=, +, -=, -
del += SampleMethodThree;
del();
}
public static void SampleMethodOne()
{
Console.WriteLine("SampleMethodOne Invoked");
}
public static void SampleMethodTwo()
{
Console.WriteLine("SampleMethodTwo Invoked");
}
public static void SampleMethodThree()
{
Console.WriteLine("SampleMethodThree Invoked");
}
}
=====================================
using System;
//where do you use multicast delegates?
// multicast delegate makes implementation of observer design pattern very simple.
// Observer pattern is also called as publish/ pubscribes pattern
public delegate void SampleDelegate(out int Integer);
class Program
{
static void Main()
{
SampleDelegate del = new SampleDelegate(SampleMethodOne);
del += SampleMethodTwo;
int DelegateOutputParameterValue = -1;
del(out DelegateOutputParameterValue);
Console.WriteLine("DelegateReturnedValue = {0}", DelegateOutputParameterValue); //only return last value
}
public static void SampleMethodOne(out int Number)
{
Number = 1;
}
public static void SampleMethodTwo(out int Number)
{
Number = 2;
}
}