Пример #1
0
        public static void GenericDelegate()
        {
            MyDelegate <int> hasParam = new MyDelegate <int>(Simple.PrintNum);

            hasParam(5);

            //系统的泛型委托
            Action <int> ac = new Action <int>(Simple.PrintNum);

            ac(6);

            MyDelegate <string> strMy = new MyDelegate <string>(Simple.PrintString);

            strMy("7");

            MyDelegateTwo <int, string> strTwo = new MyDelegateTwo <int, string>(Simple.NumToString);

            strTwo(12);

            //系统的泛型委托
            Func <int, string> fun = new Func <int, string>(Simple.NumToString);

            fun(13);

            MyDelegateTwoPlus <int, string> twoPlus = new MyDelegateTwoPlus <int, string>(Simple.NumToString);

            twoPlus(14);
        }
Пример #2
0
    {//ExorTek(Mehmet Demirel)
        static void Main(string[] args)
        {
            CustomerManager customerManager = new CustomerManager();
            //customerManager.SendMessage();
            //customerManager.ShowAlert();

            MyDelegate myDelegate = customerManager.SendMessage;

            myDelegate += customerManager.ShowAlert;
            //myDelegate -= customerManager.SendMessage;
            myDelegate();

            Console.WriteLine("-----------------------");

            MyDelegateTwo myDelegateTwo = customerManager.SendMessageTwo;

            myDelegateTwo += customerManager.ShowAlertTwo;
            myDelegateTwo("Hello");

            Console.WriteLine("-----------------------");

            MathClass     mathClass     = new MathClass();
            MyDelegateInt myDelegateInt = mathClass.Sum;
            // myDelegateInt += mathClass.Multiplication;
            var sum = myDelegateInt(5, 4);

            Console.WriteLine(sum);

            Console.ReadLine();
        }
Пример #3
0
        static void Main(string[] args)
        {
            //Delegates are "placeholders" for functionss that will be called at some point
            //Just as a variable can hold a string, a variable can be used to hold a reference for a function
            //Delegates can be called in order and changed?
            //events: used to broadcast and listen to messages (triggers?)
            //those who listen to events are called listeners or subscribers

            //Delegatea are "like" C+ function pointers or JS callbakcs
            //C# checks signatures
            //can be used to define callback functions
            //Can be dinamically switched at runtime

            //public delegate int MyDelegate(int i, string s);

            //these lines test the static functions
            MyDelegate f = func1; //f assigned to a specific function (that matches the signature)

            Console.WriteLine("The number is: " + f(10, 20));
            f = func2;
            Console.WriteLine("the number is now: " + f(10, 20));
            //output should be 30 and 200

            //these lines test the instance of the class for delegates
            MyClassForDelegates mc = new MyClassForDelegates();

            f = mc.instanceMethod1;
            Console.WriteLine("The number is: " + f(10, 20));
            //outut should be 300


            //Anonymous Delegates
            // instead of explicitely ddecllaring and naming a delegate function, they can be created as anonymous functions
            // This can be usefull if the delegate is a one-off function, or a function small enough to be declared in-line in order to improve the redability of the code.
            MyDelegate aF = delegate(int arg1, int arg2)
            {
                return((arg1 + arg2).ToString());
            };//don't forget the semi colon

            Console.WriteLine("The value is: " + aF(20, 30));
            //the result should be 50


            //COMPOSABLE DELEGATES
            //delegates can be chained together. They will be called in the order that it wwas added to the chain.
            //things to keep in mind:
            //1. Unhandled exceptions will skipp remaining delegates
            //2. Only the LAST return value will be returned to the calling function
            //3. To pass results from one delegate to another, use ref variables.

            MyDelegate f1   = func1;
            MyDelegate f2   = func2;
            MyDelegate f1f2 = f1 + f2;  //this is the delegate chain

            Console.WriteLine("Check the results:" + f1f2(10, 20));
            //notice only the last function pops a result.
            //if each function had its own direct output (part of the function) they will be printed individually

            //now, subtract a delegate from a chain
            f1f2 -= f2;
            Console.WriteLine("unchained delegate: " + f1f2(10, 20));

            Console.WriteLine("-----------\n\n");

            int           a = 10, b = 20;
            MyDelegateTwo cF1   = func3;
            MyDelegateTwo cF2   = func4;
            MyDelegateTwo cF1F2 = cF1 + cF2;

            Console.WriteLine("The value of b is: {0}", b);
            cF1F2(a, ref b); //passing by reference
            Console.WriteLine("The value of b is: {0}", b);

            Console.ReadKey();
        }