Example #1
0
        static void Main(string[] args)
        {
            //A delegate is a type safe function pointer.

            MyDelegate myDelegate = new MyDelegate(MyFunc);

            myDelegate("Hello from the MyFunc method Lohith....");

            List <Employee> empList = new List <Employee>();

            empList.Add(new Employee()
            {
                ID = 1, Name = "Mary", Salary = 5000, Experience = 5
            });
            empList.Add(new Employee()
            {
                ID = 2, Name = "John", Salary = 9000, Experience = 8
            });
            empList.Add(new Employee()
            {
                ID = 3, Name = "Rachel", Salary = 3500, Experience = 2
            });
            empList.Add(new Employee()
            {
                ID = 4, Name = "Mariesa", Salary = 7000, Experience = 7
            });
            empList.Add(new Employee()
            {
                ID = 5, Name = "Burt", Salary = 4200, Experience = 3
            });
            empList.Add(new Employee()
            {
                ID = 6, Name = "George", Salary = 6000, Experience = 6
            });

            IsPromotable isPromotable = new IsPromotable(Promote);

            Console.WriteLine("********Promoting by Experience*********");
            //Passing delegate function as a parameter
            Employee.PromoteEmployee(empList, isPromotable);

            Console.WriteLine("********Promoting by Salary*********");
            //Passing delegate function as a lambda function
            Employee.PromoteEmployee(empList, emp => emp.Salary > 4500);

            Console.WriteLine("***********Multi-cast delegates*********");
            //Multi-cast delegates: A delegate that points to more than 1 delegate.
            SampleDelegate del1, del2, del3, del4;

            del1 = new SampleDelegate(SampleOne);
            del2 = new SampleDelegate(SampleTwo);
            del3 = new SampleDelegate(SampleThree);
            del4 = del1 + del2 + del3 - del2;//delegate 4 is pointing to all 3 methods

            del4.Invoke();

            Console.WriteLine("***Multi-cast delegates other way****");
            SampleDelegate sd = new SampleDelegate(SampleOne);

            sd += SampleTwo;
            sd += SampleThree;
            sd -= SampleOne;
            sd();

            Console.WriteLine("*****Sample int return delegate******");
            SampleReturnDelegate srd = new SampleReturnDelegate(SampleReturnOne);

            srd += SampleReturnTwo;//Returns the last value.

            int retVal = srd();

            Console.WriteLine("Returned value from SampleReturn: " + retVal);
            Console.ReadKey();
        }