Tuesday 24 December 2013

Delegate in C#

                                                                                                                                            Previous.....
                                                                                                                                                      Next....       Delegates

A delegate is a typesafe function pointer. delgate is pointing to function.
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.

A delegate is a function pointer delegate is a type safe function pointer. Actually a delegates points to the function when we invoke the delegate the function will be invoked.

Use of Delegate: Reason is because of the flexibilty we will get in this approach(Delegate)
30. What is the main use of delegates in C#?
Delegates are mainly used to define call back methods.

syntax of delegate is similar to the function.

Delegate can be used to point to a function which is similar signature. This delegate can point to any function that has got void return type & string parameter

How to make delegate point to a function to do that u have to create a instance of the delegate this is where a delegate is similar to a class.

And to the constructor of this delegate you pass in the name of the function to which you want this delegate to point.
the parameter must be a method name that method should have a void return type and string paramer.
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.


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

Some of the advantages of Delagates are:-

1.It encapsulates the method call
2.It increases the performance of the application
3.We can call a method Asynchronously using a delegate.We have multicast delegates as well..

There is a lot confusion around in developer community about the exact use of delegates. Here is what delegates are.

Delegate is a way to encapulate a method call as an object. This is very important to understand that when you create a delegate you are in essense creating an object that encapsulates a method call. Since you are wrapping a method call as an object, you can do things with delegates that are only possible with objects. you can store them, you can pass them as method parameters, you can invoke them (so that they invoke target methods) when timings and conditions are right.

just consider following statement

string str = obj.SomeMethod();

when CLR comes accross this statement, it will execute the targer method(obj.SomeMethod()). But what if I want to store this call to a method and invoke only if some conditions are met?? can i do

List<method> myMethods = new List<method>(); to store them?? NO!!!!

but this is possible with delegates.

here is a code.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Delegate1
{
    public delegate void HellowFunctionDelegate(string message);

    class Program
    {
        static void Main(string[] args)
        {
            HellowFunctionDelegate del = new HellowFunctionDelegate(Hellow);
            del("Hellow from Delegate");
            Console.ReadLine();
        }

        public static void Hellow(string message)
        {
            Console.WriteLine(message);
        }
    }
}


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

namespace Delegate1
{
    public delegate void HellowFunctionDelegate(string message);

    class Program
    {
        static void Main(string[] args)
        {
            List<Employee> emplist = new List<Employee>();
            emplist.Add(new Employee() { ID = 1, Name = "Sachin", Salary = 50000, Experience = 3 });
            emplist.Add(new Employee() { ID = 2, Name = "Yuvraj", Salary = 30000, Experience = 2 });
            emplist.Add(new Employee() { ID = 3, Name = "Rahul", Salary = 80000, Experience = 7 });
            emplist.Add(new Employee() { ID = 4, Name = "Kiran", Salary = 70000, Experience = 5 });


            IsPromotable isPromotable = new IsPromotable(Pramote);
            Employee.PromotEmployee(emplist, isPromotable);
Employee.PromotEmployee(emplist, emp => emp.Experience >= 5);        }
        public static bool Pramote(Employee e)
        {
            if (e.Experience >= 5)
            {
                return true;
            }
            else
            {
                return false;                    
            }
        }
    }

    delegate bool IsPromotable(Employee emp);

    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 PromotEmployee(List<Employee> employeeList, IsPromotable IsEligibleToPramote)
        {
            foreach (Employee e in employeeList)
            {
                if (IsEligibleToPramote(e))
                {
                    Console.WriteLine(e.Name + " Promoted");
                }
            }

        }

    }
}


  


                                                                                                                                            Previous.....                                                                                                                                                                                                                                                                                                                        Next....       




C# program Selection Sorting

Selection sort is a straightforward sorting algorithm. This algorithm search for the smallest number in the elements array and then swap i...