Пример #1
0
        static void Main(string[] args)
        {
            DelegateSample tempObject = new DelegateSample();

            tempFunctionPointer funcPointer = tempObject.FirstFunction;

            funcPointer += tempObject.SecondFunction;

            //Calling the funcion explicitly
            //funcPointer("Hello 2nd Time", 2);

            tempFuncPointer += tempObject.FirstFunction;
            int retValue = tempFuncPointer("From Func methois", 1);

            Console.WriteLine("Return value from Func method is : " + retValue);

            tempActionPointer += tempObject.ThirdFunction;
            tempActionPointer("from Acion delegate", 3);

            tempPredicatePoiner += tempObject.FourthFunction;
            Employee emp = new Employee()
            {
                Name = "Employee Awesome",
                Age  = 28
            };

            tempPredicatePoiner(emp);
        }
Пример #2
0
        public static void Main()
        {
            DelegateSample tempObj = new DelegateSample();
            tempFunctionPointer funcPointer = tempObj.FirstTestFunction;
            //synchronous
            funcPointer("hello", 1);

            //asynchronous
            funcPointer.BeginInvoke("Hello", 1, null, null);

            Console.ReadKey();
            funcPointer = tempObj.SecondTestFunction;
            funcPointer("hello", 1);
            Console.ReadKey();

            //Func: See documentation below in flavors of delegates section
            Func<string, int, int> tempFuncPointer = tempObj.FirstTestFunction;
            int value = tempFuncPointer("hello", 3);
            Console.ReadKey();

            //action:
            Action<string, int> tempActionPointer = tempObj.ThirdTestFunction;
            tempActionPointer("hello", 4);
            Console.ReadKey();

            //predicate:
            Predicate<Employee> tempPredicatePointer = tempObj.FourthTestFunction;
            Employee[] lstEmployee = (new Employee[]
            {
                   new Employee(){ Name = "Ashwin", Age = 31},
                   new Employee(){ Name = "Bokil", Age = 25},
                   new Employee(){ Name = "Amit", Age = 28},
                   new Employee(){ Name = "Ajay", Age = 29},
                   new Employee(){ Name = "Sujay", Age = 26}
            });

            Employee tempEmployee = Array.Find(lstEmployee, tempPredicatePointer);
            var employeesBelow27 = Array.FindAll(lstEmployee, tempPredicatePointer);
            Console.WriteLine("Person below 27 age :" + tempEmployee.Name);

            foreach (var emp in employeesBelow27)
                Console.WriteLine("Person below 27 age :" + emp.Name);

            Console.ReadKey();

            //Converter Delegate:
            Converter<Employee, XEmployee> tempConvertorPointer
                = new Converter<Employee, XEmployee>(tempObj.ConvertToXEmployee);

            List<XEmployee> xEmployee = Array.ConvertAll(lstEmployee, tempConvertorPointer).ToList();
            Console.ReadKey();
        }