using System;
namespace MyCompany
{
public class Employee
{
private string firstName;
private string lastName;
private decimal salary;
public Employee(string first, string last, decimal salary)
{
this.firstName = first;
this.lastName = last;
this.salary = salary;
}
public string FirstName
{
get { return firstName; }
set { firstName = value; }
}
public string LastName
{
get { return lastName; }
set { lastName = value; }
}
public decimal Salary
{
get { return salary; }
set { salary = value; }
}
public override string ToString()
{
return String.Format("{0} {1} with a payroll of {2}",
firstName, lastName, salary);
}
}
public class Department
{
private Employee[] emps;
private string name;
public Department(Employee[] theEmps, string name)
{
emps = theEmps;
this.name = name;
}
public string Name
{
get
{
return name;
}
}
// declaring a nested delegate type that accepts an Employee instance
public delegate void EmployeeCallback(Employee emp);
// This method accepts an EmployeeCallback instance thus
// providing the Callback mechanism
public void ProcessEmployees(EmployeeCallback callback)
{
foreach (Employee emp in emps)
{
callback(emp);
}
}
}
public class Sys
{
private static void UpdatePayroll(Employee emp)
{
emp.Salary *= 1.2m;
Console.WriteLine("The Employee {0} {1}'s salary increased to {2}",
emp.FirstName, emp.LastName, emp.Salary);
}
public static void Main()
{
Employee emp1 = new Employee("Marina", "Joe", 7000m);
Employee emp2 = new Employee("Mina", "Nader", 7000m);
Employee emp3 = new Employee("Johny", "Hany", 9000m);
Employee[] emps = new Employee[3];
emps[0] = emp1;
emps[1] = emp2;
emps[2] = emp3;
Department dep = new Department(emps, "IT");
foreach (Employee emp in emps)
{
Console.WriteLine(emp.ToString());
}
Console.WriteLine("nCreating the delegate object");
// creating the delegate instance
Department.EmployeeCallback updateCallback = new Department.EmployeeCallback(UpdatePayroll);
Console.WriteLine("calling the method ProcessEmployees()n");
dep.ProcessEmployees(updateCallback);
Console.ReadLine();
}
}
}