Tuesday, September 18, 2018

What is a Delegate in C#

delegate is a type safe function pointer.That is, they hold 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 a 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.

Tip to remember delegate syntax: Delegates syntax look very much similar to a method with a delegate keyword.

Example:-

public delegate void HelloDelegate(string message);

    

namespace DelegateDemo
{
    public delegate void HelloDelegate(string message);
    class Program
    {
        static void Main(string[] args)
        {
            HelloDelegate d1 = new HelloDelegate(Hello);
            d1("hello Jitendra");
            d1.Invoke("hello Jitendra");
            Console.ReadKey();
        }
        public static void Hello(string message)
        {
            Console.WriteLine(message);
        }
    }
}


Why do we use delegate like to re use the code.

namespace DelegateDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Employee> employees = new List<Employee>();
            employees.Add(new Employee() { ID = 1, Name = "Jitu", Experience = 5, Salary = 10000 });
            employees.Add(new Employee() { ID = 2, Name = "Ramu", Experience = 1, Salary = 3000 });
            employees.Add(new Employee() { ID = 3, Name = "Raj", Experience = 3, Salary = 5000 });
            employees.Add(new Employee() { ID = 4, Name = "Sita", Experience = 2, Salary = 1500 });
            employees.Add(new Employee() { ID = 5, Name = "Jay", Experience = 8, Salary = 20000 });

            //IsPromatable p = new IsPromatable(Promote);
            //Employee.PromoteEmploye(employees, p);


            Employee.PromoteEmploye(employees, emp => emp.Experience >= 5);

            Console.ReadKey();
        }


        public static bool Promote(Employee emp)
        {
            if (emp.Experience > 5)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

    }
    public delegate bool IsPromatable(Employee emp);
    public 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 PromoteEmploye(List<Employee> employees, IsPromatable promatable)
        {
            foreach (var emp in employees)
            {
                if (promatable(emp))
                {
                    Console.WriteLine(emp.Name + "Promoted");
                }
            }

        }
    }

}

No comments:

Post a Comment

Find the value from array when age is more than 30

 const data = [   { id: 1, name: 'Alice', age: 25 },   { id: 2, name: 'Bob', age: 30 },   { id: 3, name: 'Charlie', ...