예제 #1
0
        static void Main(string[] args)
        {
            Del arithmeticMethod = Add;
            var result           = arithmeticMethod(10, 5);

            Console.WriteLine($"Result is : {result}");//Prints 15

            arithmeticMethod = Subtract;
            result           = arithmeticMethod(10, 5);
            Console.WriteLine($"Result is : {result}");//Prints 5

            Organization myOrg = new Organization();

            LogExpense(myOrg.GetOfficeExpense, "OfficeExpense");
            LogExpense(myOrg.GetAdministrationExpense, "Administration");
            LogExpense(myOrg.GetSalary, "Salary");
            LogExpense(() => { return((new Random()).Next(100, 200)); }, "Others");
        }
예제 #2
0
        static void Main(string[] args)
        {
            Program p = new Program();
            //p.DivideTwoIntegers(5, 2, out int remainder);
            //DivideTwoIntegers(1, 2, out int remainder2); Have to be static in order to use it without the p object
            CalcInts mySum = p.Sum;

            mySum(1, 2);
            CalcInts myDiv = p.Div;

            myDiv(1, 2);
            CalcInts mySub = p.Sub;

            mySub(1, 2);

            CalcInts myCalcs = p.Sum;

            myCalcs(1, 2);
            myCalcs = p.Div;
            myCalcs(1, 2);
            myCalcs = p.Sub;
            myCalcs(1, 2);

            CalcInts myCalcAll = myDiv + mySub + mySum;//MULTICAST DELEGATE

            //myCalcs(1, 2);
            Console.WriteLine(myCalcAll(1, 2));

            Del x = i => i * 10 + 1 - 2;

            x(5);
            //--------------------------
            Home home = new Home();

            home.Address = "Tritonos";
            home.Name    = "Michalopoulos";


            PrintHome printDel;

            printDel = give => Console.WriteLine($"{home.Address}, {home.Name}");
            printDel(home);
            //Console.WriteLine();
        }
예제 #3
0
        static void Main(string[] args)
        {
            Console.WriteLine("Let's talk about delegates. These may seem a little confusing at first," +
                              " but their main purpose is to pass methods as arguments to other methods.");
            Console.WriteLine("Take event handlers for example. They are really just methods that are " +
                              "invoked using delegates.");
            Console.WriteLine();

            Console.WriteLine("So let's create a super simple delegate to see what it does.");
            Console.WriteLine("We already created 'Del' which can handle any method that takes in a string.");
            Console.WriteLine("We can now make a method into an object by calling:");
            Console.WriteLine("Del handler = DelegateMethod");
            Console.WriteLine("This is super useful because we can now put this in classes and structs.");
            Console.WriteLine("Let's run this delegate and see what it does:");
            Console.WriteLine();

            // Instantiate the delegate.
            Del handler = DelegateMethod;

            // Call the delegate.
            handler("Look I am a working delegate!");

            Console.WriteLine();

            Console.WriteLine("Delegates help us because we can then pass them into other methods.");
            Console.WriteLine("Take the next example. We can pass in a string and the delegate, ");
            Console.WriteLine("which we can then use to call the delegate method with the parameter");
            Console.WriteLine("Take a look at what this method call does with Del as a param:");
            Console.WriteLine();

            MethodWithCallback("I'm called from a method, but use the delegate to write this message", handler);

            Console.WriteLine();

            Console.WriteLine("Let's look at a more practical example.");
        }
예제 #4
0
 private static void MethodWithCallback(string param, Del callback)
 {
     callback(param);
 }
예제 #5
0
 public static void MethodWithCallback(int param1, int param2, Del callback)
 {
     callback("The number is: " + (param1 + param2).ToString());
 }
예제 #6
0
 static void PerformArithmeticOps(Del method, int first, int second)
 {
     method(first, second);
 }