Example #1
0
    public static void Main()
    {
        HelloFunctionDelegate del = new HelloFunctionDelegate(Hello);

        del("Hello from Delegate");//Hello from Delegate
        //A delegate is a type safe function pointer
    }
Example #2
0
        static void Main(string[] args)
        {
            HelloFunctionDelegate del = new HelloFunctionDelegate(Hello);

            del("Hello from Delegate");
            //A delegates is a type safe function pointer
        }
Example #3
0
        public static void Delegates()
        {
            HelloFunctionDelegate dele = new HelloFunctionDelegate(Delegate.hello);

            dele("Hello from INDIA");

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



            empList.Add(new Employee {
                Name = "Marry", Id = 2, Experience = 5, Salary = 1202
            });
            empList.Add(new Employee {
                Name = "Henry", Id = 3, Experience = 3, Salary = 20502
            });
            empList.Add(new Employee {
                Name = "Deepak", Id = 4, Experience = 5, Salary = 125002
            });
            empList.Add(new Employee {
                Name = "Muthu", Id = 5, Experience = 1, Salary = 10002
            });

            IsPromotableDelegate promoteDele = new IsPromotableDelegate(PromoteBasedOnExp);

            Employee.PromoteEmployess(empList, promoteDele);
            ///  OR Using Lambda
            Employee.PromoteEmployess(empList, emp => emp.Salary > 1000);
        }
Example #4
0
        public static void Main(string[] args)
        {
            HelloFunctionDelegate del = new HelloFunctionDelegate(Hello);

            del("Hello from Delegate");

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

            emplist.Add(new Employee()
            {
                ID = 101, Name = "Mary", Salary = 5000, Experience = 5
            });
            emplist.Add(new Employee()
            {
                ID = 101, Name = "Mike", Salary = 4000, Experience = 4
            });
            emplist.Add(new Employee()
            {
                ID = 101, Name = "John", Salary = 6000, Experience = 6
            });
            emplist.Add(new Employee()
            {
                ID = 101, Name = "Todd", Salary = 3000, Experience = 3
            });

            isPromotable ispromotable = new isPromotable(Promote);

            Employee.PromoteEmployee(emplist, ispromotable);

            // or use lamba expression

            Employee.PromoteEmployee(emplist, emp => emp.Experience >= 5);
        }
Example #5
0
        static void Main(string[] args)
        {
            HelloFunctionDelegate del = new HelloFunctionDelegate(Hello);

            del("Hello from delegate");
            Console.ReadKey();
        }
        public static void Main(string[] args)
        {
            //A Delegates is a type safe function pointer.
            HelloFunctionDelegate hfd = new HelloFunctionDelegate(Hello);

            hfd("Hello from delegate.");
        }
Example #7
0
        static void Main(string[] args)
        {
            // A delegate is a type safe function pointer --> function is invoke when it is pointed at
            HelloFunctionDelegate del = new HelloFunctionDelegate(Hello2);

            del("Hello from Delegate");
        }
Example #8
0
        static void Main(string[] args)
        {
            HelloFunctionDelegate del = new HelloFunctionDelegate(Hello);

            del("Hello from the delegate!");
            Console.ReadLine();

            //Employees example
            List <Employee> empList = new List <Employee>();

            empList.Add(new Employee()
            {
                ID = 101, Name = "Mary", Salary = 50000, Experience = 5
            });
            empList.Add(new Employee()
            {
                ID = 101, Name = "Mike", Salary = 60000, Experience = 6
            });
            empList.Add(new Employee()
            {
                ID = 101, Name = "John", Salary = 40000, Experience = 4
            });
            empList.Add(new Employee()
            {
                ID = 101, Name = "Todd", Salary = 30000, Experience = 3
            });

            Employee.PromoteEmployees(empList);
            Console.ReadLine();
        }
Example #9
0
        public static void Main(string[] args)
        {
            //A delegate is a type safe pointer
            //ot must match the signature of the function
            HelloFunctionDelegate del = new HelloFunctionDelegate(Hello);

            del("Hello from delegate");
        }
        static void Main(string[] args)
        {
            // int the constructer of a delegate we pass the  name of method to which delegate to point to
            HelloFunctionDelegate helloFunctionDelegate = new HelloFunctionDelegate(Hello);

            // we call the delegate and pass parameter
            helloFunctionDelegate("Hello from Youtube");
        }
Example #11
0
        static void Main(string[] args)
        {
            // A delegate is a type safe function pointer.
            HelloFunctionDelegate del = new HelloFunctionDelegate(Hello);

            del("Hello from delegate!!!");
            Console.ReadLine();
        }
Example #12
0
    public static void Main()
    {
        //	A delegate is a type safe function pointer
        //	pass in a function you want to invoke
        HelloFunctionDelegate del = new HelloFunctionDelegate(Hello);

        del("Hello from Delegate");
    }
        static void Main(string[] args)
        {
            //A delegate is a type safe function
            //Singanature of Delegates should match the Signatureof the Function

            HelloFunctionDelegate del = new HelloFunctionDelegate(Hello);  //Instance of Delegate

            del("Hello From Delegates...");
        }
Example #14
0
    static void Main()
    {
        // A delegate is a type safe function pointer
        HelloFunctionDelegate del = new HelloFunctionDelegate(Hello);

        del("Hello from Delegate");

        Console.ReadKey();
    }
Example #15
0
        public static void Main()
        {
            //1. a delegate is a type safe function pointer. This means that a delegate points to a function and then when invoked, the funtion will be
            //invoked. THE SIGNATURE OF THE DELEGATE MUST MATCH THE SIGNATURE OF THE FUNCTION.

            HelloFunctionDelegate del = new HelloFunctionDelegate(Hello);

            del("test from delegate");
        }
Example #16
0
        public static void Main()
        {
            //Can create a new instance of the delegate as you would for a class.
            //Create for the function you point to 
            HelloFunctionDelegate del = new HelloFunctionDelegate(Hello);

            //Use the funtion you point to this way:
            del("Hello from delegate");

        }
Example #17
0
        static void Main(string[] args)
        {
            ///A delegate is a type safe function pointer
            HelloFunctionDelegate del = new HelloFunctionDelegate(Hello);

            del("Hello from Delegates");

            //OR WE CAN DIRECTLY USE
            Hello("Alternative way to call delegates");
        }
Example #18
0
        static void Main(string[] args)
        {
            // Create the instance of the delegate and pass in the function
            // name as the parameter to the constructor. The passed in
            // function signature must match the signature of the delegate
            HelloFunctionDelegate del = new HelloFunctionDelegate(Hello);

            // Invoke the delegate, which will invoke the method
            del("Hello from Delegte");
        }
Example #19
0
        static void Main(string[] args)
        {
            HelloFunctionDelegate del = new HelloFunctionDelegate(Hello);

            // A delegate is a type safe function pointer

            del("Hello from Delegate");

            Console.ReadKey();
        }
Example #20
0
        /*
         * 1. A delegate is a type safe function pointer. i.e, it holds a reference(pointer) to a function.
         * 2. It is of reference type.
         * 3. The signature of the delegate must match the signature of the function the delegate points to, otherwise you will get a compiler error.
         * this is why delegate is called as type safe function pointers.
         * 4. It is similar to class, you can create an instance of it, and when you do so. You pass in the function name as paramerter to delegate constructor.
         * and it is to this function the delegate will point to.
         *
         * TIPS :: To remember delegate syntax:
         * It looks very similar to function syntax with delegate keyword with it.
         */
        static void Main(string[] args)
        {
            HelloFunctionDelegate del = new HelloFunctionDelegate(Hello);
            del("Hello from delegate");

            SampleFunctionDelegate del2 = new SampleFunctionDelegate(Sample);
            string s = del2(1, "Sample from delegate ");
            Console.WriteLine(s);

            Console.ReadKey();
        }
Example #21
0
        static void Main(string[] args)
        {
//here Delegates constructors gets called or Delegates instantiation done and its expect reference(pointing) to function "hello".

            HelloFunctionDelegate del = new HelloFunctionDelegate(Hello);

            //Invoking Delegates expect input same as function and it calls the function "Hello".
            del("Hello from Delegate");

            Console.ReadKey();
        }
Example #22
0
        static void Main(string[] args)
        {
            HelloFunctionDelegate del = new HelloFunctionDelegate(Hello);

            del("This is a call to Hello method using HelloFunctionDelegate");

            HelloInteger hel = new HelloInteger(NumberDisplay);
            int          n   = hel(12);

            Console.WriteLine(n);
        }
        static void Main(string[] args)
        {
            HelloFunctionDelegate del = new HelloFunctionDelegate(Hello);

            del("dupa");
            Test(del);
            Console.ReadKey();
            Operation op = Double;

            op += Triple; //chaining functions
            ExecuteOperation(2, op);
            Console.ReadKey();
        }
        /*
         * 1. A delegate is a type safe function pointer. i.e, it holds a reference(pointer) to a function.
         * 2. It is of reference type.
         * 3. The signature of the delegate must match the signature of the function the delegate points to, otherwise you will get a compiler error.
         * this is why delegate is called as type safe function pointers.
         * 4. It is similar to class, you can create an instance of it, and when you do so. You pass in the function name as paramerter to delegate constructor.
         * and it is to this function the delegate will point to.
         *
         * TIPS :: To remember delegate syntax:
         * It looks very similar to function syntax with delegate keyword with it.
         */
        static void Main(string[] args)
        {
            HelloFunctionDelegate del = new HelloFunctionDelegate(Hello);

            del("Hello from delegate");

            SampleFunctionDelegate del2 = new SampleFunctionDelegate(Sample);
            string s = del2(1, "Sample from delegate ");

            Console.WriteLine(s);

            Console.ReadKey();
        }
Example #25
0
        static void Main()
        {
            // untuk membuat delegate kita membuat instance dari delegate
            HelloFunctionDelegate del = new HelloFunctionDelegate(Hello);
            SumDelegate           sum = new SumDelegate(SumNumber);

            // secara otomatis function Hello akan di invoke dan delegate memberikan argumentnya
            del("Hello delegate");
            int num1, num2;

            sum(out num1, out num2);
            Console.WriteLine("num 1 = {0}, num2 = {1}", num1, num2);
        }
Example #26
0
        static void Main(string[] args)
        {
            // A delegate is a type safe function pointer - meaning it points to a function and when you invoke this delegate the function will be invoked.
            // we have to make an instance of the delegate
            HelloFunctionDelegate del = new HelloFunctionDelegate(Hello);

            del("Hello from delegate");

            void Hello(string strMessage)
            {
                Console.WriteLine(strMessage);
                Console.ReadLine();
            }
        }
Example #27
0
        static void Main(string[] args)
        {
            /*poiting a delegate to point to hello function like this:*/
            HelloFunctionDelegate del = new HelloFunctionDelegate(Hello);

            //passing method to Hello Method
            del("Hello from Delegate");

            /*Type safe function means that the value of return type of the delegate and the function which delegate is pointing to should be same.
             * A signatue of the delegate must match the signature of the function, the delegate points to, otherwise you get a complier error. This is
             * the reason delegates are called as type sage 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 paramet to the
             * delegate constructor and it is to this fucntion the delegate will point to*/
            System.Console.ReadLine();
        }
        public static void TestMethod()
        {
            //First way

            //Do the multiplication
            HelloFunctionDelegate deligateObject = new HelloFunctionDelegate(Multiplication);

            // pass the values to the methods by delegate object
            deligateObject(1, 2);

            //Do the Summation
            deligateObject = Summation; //Remove redundant delegate constructor call -> removed -> new HelloFunctionDelegate(Summation)

            // pass the values to the methods by delegate object
            deligateObject(5, 7);
        }
        public static void DelegateFeatureMain()
        {
            //Second Way
            // Creating object "obj" of class "DelegateFeature"
            DelegateFeature abstractClassObject = new DelegateFeature();

            //Do the Dividation
            // creating object of delegate, name as "deligateObject2"
            // for method "Dividation" & pass the parameter as the two methods by class object "obj"
            // instantiating the delegates
            HelloFunctionDelegate deligateObject2 = new HelloFunctionDelegate(abstractClassObject.Dividation);

            // pass the values to the methods by delegate object
            deligateObject2(6, 2);

            deligateObject2.Invoke(100, 40);
        }
        static void Main(string[] args)
        {
            HelloFunctionDelegate del = new HelloFunctionDelegate(Hello);

            del("Hello, world!");

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

            employeeList.Add(new Employee()
            {
                ID = 101, Name = "Tanvir", Salary = 30000, Experience = 3
            });
            employeeList.Add(new Employee()
            {
                ID = 102, Name = "Iqbal", Salary = 40000, Experience = 4
            });
            employeeList.Add(new Employee()
            {
                ID = 103, Name = "Fahim", Salary = 50000, Experience = 5
            });
            employeeList.Add(new Employee()
            {
                ID = 104, Name = "Jamil", Salary = 60000, Experience = 6
            });

            //With delegate function
            Console.WriteLine("With delegate function");
            IsPromotable isPromotable = new IsPromotable(Promote);

            Employee.PromotEmployee(employeeList, isPromotable);

            //With lambda expression, No delegate instance and promote() function needed
            Console.WriteLine("With lambda expression");
            Employee.PromotEmployee(employeeList, emp => emp.Experience >= 5);
            //Now we can change our logic and don't have to change the Employee class as this is our framework class
            Employee.PromotEmployee(employeeList, emp => emp.Salary >= 50000);

            Console.Read();
        }
        public Delegates()// class constructor
        {
            // create an instance of delegate, to point delegate to specific function  with same return type and same parameters
            // pass the method name as parameter
            HelloFunctionDelegate del = new HelloFunctionDelegate(Hello);

            // calling delegate:
            del("Hello from delegate");


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

            empList.Add(new Employee2()
            {
                ID = 101, Name = "Mary", Salary = 5000, Experience = 5
            });
            empList.Add(new Employee2()
            {
                ID = 102, Name = "Mike", Salary = 4000, Experience = 4
            });
            empList.Add(new Employee2()
            {
                ID = 103, Name = "John", Salary = 6000, Experience = 6
            });
            empList.Add(new Employee2()
            {
                ID = 104, Name = "Todd", Salary = 3000, Experience = 3
            });

            // on "new IsPromotable" pass a function of type boolean that takes a Emplyee2 as parameter
            // not required when using lambda expression
            IsPromotable isPromotable = new IsPromotable(Promote);

            Employee2.PromoteEmployee(empList, isPromotable);

            // using lambda expression (compiler creates a delegate, a function then passes it to current method)
            Employee2.PromoteEmployee(empList, emp => emp.Experience >= 5);
        }
Example #32
0
 public static void Main()
 {
     BaseClass B = new DerivedClass();
     B.Print();
     //show the add f(x)
     Overloading.Add(2, 3);
     //show the overloaded add f(x)
     Overloading.Add(2.3F, 3.4F);
     //create a customer
     Customer2 C1 = new Customer2("Tony", "Wittinger");
     C1.PrintFullName();
     //show the use of 'this'
     Circle Cir = new Circle(5);
     float Area = Cir.CalculateArea();
     Console.WriteLine("Area = {0}\n", Area);
     //Show the struct feature
     Student S1 = new Student(234, "Tony");
     S1.PrintDetails();
     Person P1 = new Person();
     P1.Print();
     HelloFunctionDelegate del = new HelloFunctionDelegate(Hello);
     del("Hello from Delegate");
           
 }
Example #33
0
        static void Main(string[] args)
        {
            HelloFunctionDelegate del = new HelloFunctionDelegate(SayHallo);

            del("Hello from delegate");

            List <Employee> empList = new List <Employee>()
            {
                new Employee()
                {
                    Id = 1, Name = "Vanco", Experience = 4, Salary = 1200
                },
                new Employee()
                {
                    Id = 2, Name = "Viktor", Experience = 1, Salary = 800
                },
                new Employee()
                {
                    Id = 3, Name = "Vaska", Experience = 8, Salary = 10000
                },
            };

            empList.Add(new Employee()
            {
                Id = 4, Name = "Hristo", Experience = 20, Salary = 1600
            });
            empList.Add(new Employee()
            {
                Id = 5, Name = "Krsto", Experience = 26, Salary = 16500
            });

            //IsPromotable isPromote = new IsPromotable(Promote);
            // Employee.PromtOfEmployee(empList,isPromote);
            Employee.PromtOfEmployee(empList, emp => emp.Experience >= 5);
            Console.ReadLine();
        }
Example #34
0
 static void Main(string[] args)
 {
     HelloFunctionDelegate del = new HelloFunctionDelegate(Hello);
     del("Hello world");
     Console.ReadLine();
 }
Example #35
0
 public static void Main()
 {
 HelloFunctionDelegate del = new HelloFunctionDelegate(Hello);
 del("Hello from Delegate");
 }