Tuesday, 1 August 2017

Delegates in C#

Delegate

Delegate is type safe function pointer. That is, it holds a reference (pointer) to a function.
The signature of the Delegate must match the signature of the function,
The Delegate points to; otherwise you get a compiler error.
This is the reason Delegates are called as type safe function pointers.

A Delegate is similar to class. You can create an instance of it, and when you do so you pass in the function name as a parameter to the delegate constructor, and it is to this function the delegate will point to.

Delegate syntax look very much similar to a method with a delegate keyword.
Structures are value types in case Classes, Interfaces and Delegates are reference types

Application 1
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

public delegate void SimpleDelegateMethod(string FirstName);
public delegate int IntDelegateMethod(int FirstVal, int SecondVal);

namespace Delegates
{

    class DelegatesDemo
    {
        static void Main(string[] args)
        {
            // Delegate is type safe function pointer.

            // Without Delegates method call
            PrintFirstName("Rahul");

            DelegatesDemo ObjDelegatesDemo = new DelegatesDemo();
            Console.WriteLine("Sum without Delegate is {0}", ObjDelegatesDemo.SumOfInteger(7, 5));

            Console.WriteLine("====================================================" + Environment.NewLine);

            // Using delegates method call
            SimpleDelegateMethod ObjSimpleDelegateMethod = new SimpleDelegateMethod(PrintFirstName);
            ObjSimpleDelegateMethod("Sagar - Using Delegates");

            IntDelegateMethod ObjIntDelegateMethod = new IntDelegateMethod(ObjDelegatesDemo.SumOfInteger);
            int Sum = ObjIntDelegateMethod(8, 7);
            Console.WriteLine("Sum using Delegate is {0}", Sum);

            Console.ReadLine();

        }


        public static void PrintFirstName(string strFirstName)
        {
            Console.WriteLine("First Name is {0}", strFirstName);
        }

        public int SumOfInteger(int a, int b)
        {
            int c = a + b;
            return c;
        }

    }
}


Application 2
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DelegateExample2
{
    public delegate bool IsEmployeeApplicableDelegate(Employee employee);
   
    public class Employee
    {
        public int EmployeeID { get; set; }
        public string Name { get; set; }
        public int Salary { get; set; }
        public int Experience { get; set; }
        public int Grade { get; set; }
        public string Designation { get; set; }

        public static void FindEmployee(List<Employee> Employees, IsEmployeeApplicableDelegate empDelegate)
        {
            Console.WriteLine("Below are the Senior Employees in Organization.");
            foreach (Employee emp in Employees)
            {
                if (empDelegate(emp))
                {
                    Console.WriteLine(emp.Name);
                }
            }
        }

        public static bool IsApplicable(Employee EmployeesList)
        {

            if (EmployeesList.Grade >= 5)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public static List<Employee> CreateEmployee()
        {
            List<Employee> employee = new List<Employee>();
            employee.Add(new Employee() { EmployeeID = 1, Name = "Avinash", Salary = 5200221, Grade = 4, Designation = "Software Engineer" });
            employee.Add(new Employee() { EmployeeID = 2, Name = "Pradip", Salary = 6200000, Grade = 5, Designation = "Team Leader" });
            employee.Add(new Employee() { EmployeeID = 3, Name = "Rajeev", Salary = 7800548, Grade = 6, Designation = "Project Leader" });
            employee.Add(new Employee() { EmployeeID = 4, Name = "Paresh", Salary = 8700278, Grade = 7, Designation = "vice President" });
            employee.Add(new Employee() { EmployeeID = 5, Name = "Karna", Salary = 9200232, Grade = 8, Designation = "Sr vice President" });
            employee.Add(new Employee() { EmployeeID = 6, Name = "Arjun", Salary = 10200271, Grade = 9, Designation = "CEO" });
            employee.Add(new Employee() { EmployeeID = 7, Name = "Bhishma", Salary = 4278785, Grade = 4, Designation = "Software Engineer" });
            employee.Add(new Employee() { EmployeeID = 8, Name = "Ram", Salary = 4270231, Grade = 4, Designation = "Software Engineer" });
            employee.Add(new Employee() { EmployeeID = 9, Name = "Sham", Salary = 3022544, Grade = 3, Designation = "Jr Software Engineer" });
            employee.Add(new Employee() { EmployeeID = 10, Name = "Santosh", Salary = 2054454, Grade = 3, Designation = "Jr Software Engineer" });
            return employee;
        }
    }
    public class Program
    {
        static void Main(string[] args)
        {
            // Call Create Employye
            List<Employee> EmployeeList = Employee.CreateEmployee();
            IsEmployeeApplicableDelegate ObjIsEmployeeApplicableDelegate = new IsEmployeeApplicableDelegate(Employee.IsApplicable);
            Employee.FindEmployee(EmployeeList, ObjIsEmployeeApplicableDelegate);
            Console.ReadLine();
        }
    }
}