Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            Console.WriteLine("***** Generic Delegates *****\n");
            Console.WriteLine("-> Non-generic delegates");

            MyDelegate d = new MyDelegate(MyTarget);

            d("More string data");
            MyDelegate d2 = new MyDelegate(MyTarget);

            d2(9);

            // Create an instance of MyGenericDelegate<T>
            // with string as the type parameter.
            Console.WriteLine("\n-> Generic delegates");
            MyGenericDelegate <string> strTarget =
                new MyGenericDelegate <string>(StringTarget);

            strTarget("Some string data");

            // Create an instance of MyGenericDelegate<T>
            // with int as the type parameter.
            MyGenericDelegate <int> intTarget = IntTarget;

            intTarget(9);

            Console.ReadLine();
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            #region 泛型委托
            //无参数无返回值的委托
            Action ac1 = new Action(m1);
            ac1();

            //自定义的泛型委托
            MyGenericDelegate <string> MGD = new MyGenericDelegate <string>(getLen);
            Console.WriteLine(MGD("aaaa"));

            //系统提供的泛型委托
            // action都是无返回值的委托
            Action <string> ac2 = new Action <string>(sayHi);
            ac2("abcde");

            //func,最后一个类型就是返回值的类型
            Func <int, int, int, string> fun = M2;
            Console.WriteLine(M2(1, 2, 3));

            //lambda版本
            Func <string, int> f = (str) => { return(str.Length); };
            int x = f("aaa");
            Console.WriteLine(x);
            #endregion
            Console.Read();
        }
        static void Main(string[] args)
        {
            WriteLine("***** Generic Delegates *****\n");

            // Register targets.
            // Note how the arg to MyGenericDelegate constructor is a function.

            MyGenericDelegate <string> stringFunction =
                new MyGenericDelegate <string>(MakeUpperCase);

            stringFunction("Some string data");

            stringFunction = new MyGenericDelegate <string>(MakeLowerCase);
            stringFunction("SOME MORE string DATA");


            MyGenericDelegate <int> integerFunction =
                new MyGenericDelegate <int>(IncrementInteger);

            integerFunction(9);

            integerFunction = new MyGenericDelegate <int>(DecrementInteger);
            integerFunction(201);

            Console.ReadLine();
        }
Ejemplo n.º 4
0
        static void Main(string[] args)
        {
            Console.WriteLine("***** Generic Delegates *****\n");

            // Register targets.
            MyGenericDelegate <string> strTarget =
                new MyGenericDelegate <string>(StringTarget);

            strTarget("Some string data");

            MyGenericDelegate <int> intTarget =
                new MyGenericDelegate <int>(IntTarget);

            intTarget(9);

            MyDelegate d = new MyDelegate(MyTarget);

            d("More string data");

            // Method group conversion syntax.
            MyDelegate d2 = MyTarget;

            d2(9);  // Boxing penalty.
            Console.ReadLine();
        }
Ejemplo n.º 5
0
        public GenericDelegate()
        {
            MyGenericDelegate <string> stringDel = StringTarget;
            MyGenericDelegate <int>    intDel    = IntTarget;

            stringDel("Recai");
            intDel(58);
        }
Ejemplo n.º 6
0
        //Main_10_3_2
        //ʵ�ֲ��Դ���
        public static void Main()
        {
            MyGenericDelegate<string> myStrDel = new MyGenericDelegate<string>(GetMsg);
            Console.WriteLine(myStrDel("Hello, world."));

            MyGenericDelegate<Point> myPointDel = new MyGenericDelegate<Point>(GetPoint);
            Console.WriteLine(myPointDel(new Point(100, 200)));
        }
Ejemplo n.º 7
0
        static void Main(string[] args)
        {
            MyGenericDelegate<string> stDel = new MyGenericDelegate<string>(StringTarget);
            MyGenericDelegate<int> intDel = new MyGenericDelegate<int>(IntTarget);
            stDel("type string");
            intDel(15);

            Console.Read();
        }
Ejemplo n.º 8
0
        static void Main(string[] args) {
            MyGenericDelegate<string> strTarget = new MyGenericDelegate<string>(StringTarget);
            strTarget("Some String data");

            MyGenericDelegate<int> intTarget = new MyGenericDelegate<int>(IntTarget);
            intTarget(9);

            Console.ReadLine();
        }
Ejemplo n.º 9
0
        static void Main(string[] args)
        {
            MyGenericDelegate <string> myTStr = StringTarget;
            MyGenericDelegate <int>    myTInt = IntTarget;

            Console.WriteLine(myTStr("hi my friend!"));
            Console.WriteLine(myTInt(5));
            Console.ReadLine();
        }
        static void Main(string[] args)
        {
            MyGenericDelegate <string> stringDelegate = new MyGenericDelegate <string>(StringTarget);//parametre olarak bizden void döndüren string bir method istiyor.
            MyGenericDelegate <int>    intDelegate    = new MyGenericDelegate <int>(IntTarget);

            intDelegate(200);
            stringDelegate("Rabia Nur Kavi");
            Console.ReadLine();
        }
Ejemplo n.º 11
0
        static void Main(string[] args)
        {
            MyGenericDelegate <string> stDel  = new MyGenericDelegate <string>(StringTarget);
            MyGenericDelegate <int>    intDel = new MyGenericDelegate <int>(IntTarget);

            stDel("type string");
            intDel(15);

            Console.Read();
        }
Ejemplo n.º 12
0
    public static void Main()
    {
        var intFunction    = new MyGenericDelegate <int>(functionForInt);
        var stringFunction = new MyGenericDelegate <string>(functionForString);
        var doubleFunction = new MyGenericDelegate <double>(functionForAny);

        intFunction(18);
        stringFunction("hello");
        doubleFunction(18.1818);
    }
Ejemplo n.º 13
0
        static void Main(string[] args)
        {
            MyGenericDelegate <int> it = IntTarget;

            it(87);

            MyGenericDelegate <string> st = StringTarget;

            st("Yo!");
        }
Ejemplo n.º 14
0
        //实现测试代码
        public static void Main()//Main_10_3_2
        {
            MyGenericDelegate <string> myStrDel = new MyGenericDelegate <string>(GetMsg);

            Console.WriteLine(myStrDel("Hello, world."));

            MyGenericDelegate <Point> myPointDel = new MyGenericDelegate <Point>(GetPoint);

            Console.WriteLine(myPointDel(new Point(100, 200)));
        }
Ejemplo n.º 15
0
        public static void Run()
        {
            PrintUtility.PrintTitle("Generic Delegates");

            // Register targets.
            MyGenericDelegate <string> strTarget = new MyGenericDelegate <string>(StringTarget);
            MyGenericDelegate <int>    intTarget = new MyGenericDelegate <int>(IntTarget);

            strTarget("Some string data");
            intTarget(9);
        }
Ejemplo n.º 16
0
        static void Main(string[] args)
        {
            MyGenericDelegate <string> strTarget = StringTarget;

            strTarget("Some string data.");

            MyGenericDelegate <int> intTarget = IntTarget;

            intTarget(9);
            Console.ReadLine();
        }
Ejemplo n.º 17
0
        static void Main(string[] args)
        {
            //使用委托
            MyGenericDelegate <int>    mydelegate1 = Add;
            MyGenericDelegate <double> mydelegate2 = Sub;

            Console.WriteLine(mydelegate1(10, 20));
            Console.WriteLine(mydelegate2(10.5, 20.6));

            Console.Read();
        }
Ejemplo n.º 18
0
        static void Main(string[] args)
        {
            Console.WriteLine("Generic Delegate---------\n");
            MyGenericDelegate <string> strTarget = new MyGenericDelegate <string>(StringTarget);

            strTarget("Some string data");
            MyGenericDelegate <int> intTarget = new MyGenericDelegate <int>(IntTarget);

            intTarget(9);
            Console.ReadLine();
        }
Ejemplo n.º 19
0
        static void Main(string[] args)
        {
            //generic delegate

            //MyGenericDelegate<string> stringDelegate = new MyGenericDelegate<string>(StringTarget);
            //stringDelegate("Selçuk Akarın");

            MyGenericDelegate<int> intDelegate = new MyGenericDelegate<int>(IntTarget);
            intDelegate(6);

            Console.ReadKey();
        }
Ejemplo n.º 20
0
        static void Main(string[] args)
        {
            //使用匿名方法给泛型委托变量赋值
            MyGenericDelegate <int> del = delegate(int vlaue)
            {
                return(vlaue * 2);
            };

            //调用泛型委托变量引用的匿名方法
            Console.WriteLine(del(100));
            Console.ReadKey();
        }
Ejemplo n.º 21
0
        static void Main(string[] args)
        {
            Console.WriteLine("***** Generic Delegates *****\n");
            // Register targets.
            MyGenericDelegate <string> strTarget = new MyGenericDelegate <string>(StringTarget);

            strTarget("Some string data");

            MyGenericDelegate <int> intTarget = new MyGenericDelegate <int>(IntTarget);

            intTarget(9);
        }
        static void Main(string[] args)
        {
            Console.WriteLine("******Fun with GenericDelegate*******");

            MyGenericDelegate <string> stringHandler = new MyGenericDelegate <string>(StringTarget);

            stringHandler("i love you!");

            MyGenericDelegate <int> intHandler = new MyGenericDelegate <int>(IntTarget);

            intHandler(23);
        }
        static void Main(string[] args)
        {
            MyGenericDelegate<int> mydelegate1 = new MyGenericDelegate<int>(intdelegate);
            mydelegate1(25);
            mydelegate1 += intdelegate2;
            Console.WriteLine();
            mydelegate1(10);
            MyGenericDelegate<string> mydelegate2 = new MyGenericDelegate<string>(stringdelegate);
            mydelegate2("yo");

            Console.ReadKey();
        }
Ejemplo n.º 24
0
        static void Main(string[] args)
        {
            Console.WriteLine(" = > Generic Delegate");
            MyGenericDelegate <string> strTarget = new MyGenericDelegate <string>(StringTarget);

            strTarget("Generic delegete type - > string");


            MyGenericDelegate <int> intTarget = new MyGenericDelegate <int>(IntTarget);

            intTarget(15);
        }
Ejemplo n.º 25
0
        public static void Main(string[] args)
        {
            MyGenericDelegate <string> strTarget = new MyGenericDelegate <string>(StringTarget);

            strTarget("Some String Data");

            MyGenericDelegate <int> intTarget = new MyGenericDelegate <int>(IntegerTarget);

            intTarget(16);

            Console.ReadKey();
        }
        static void Main(string[] args)
        {
            Console.WriteLine("*****Generic Delegates * ****\n");
            // Зарегистрировать цели.
            MyGenericDelegate <string> strTarget = new MyGenericDelegate <string>(StringTarget);

            strTarget("Some string data");
            MyGenericDelegate <int> intTarget = new MyGenericDelegate <int>(IntTarget);

            intTarget(7);

            Console.ReadLine();
        }
Ejemplo n.º 27
0
        static void ExanpleGenericDelegate()
        {
            Console.WriteLine("*****Generic Delegates * ****\n");
            // Зарегистрировать цели.
            MyGenericDelegate <string> strTarget =
                new MyGenericDelegate <string>(StringTarget);

            strTarget("Some string data");
            MyGenericDelegate <int> intTarget =
                new MyGenericDelegate <int>(IntTarget);

            intTarget(9);
        }
Ejemplo n.º 28
0
        //internal static void CarRunner()
        //{
        //    Console.WriteLine("****** Delegates as event enablers *****\n");

        //    //First make a car
        //    Car c1 = new Car("SlugBug", 100, 10);

        //    //Now tell the car which method to call when it wants to send messages.
        //    c1.RegisterWithCarEngineHandler(new Car.CarEngineHandler(OnCarEngineEvent));

        //    //Hold onto the delegate object so we can unregister later.
        //    var handler2 = new Car.CarEngineHandler(OnCarEngineEvent2);
        //    c1.RegisterWithCarEngineHandler(handler2);

        //    //Unregister from the second handler.
        //    c1.UnRegisterWithCarEngineHandler(handler2);

        //    //Speed up (this will trigger events).
        //    Console.WriteLine("***** Speeding up ******");
        //    for (int i = 0; i < 6; i++)
        //        c1.Accelerate(20);
        //}

        //internal static void CarRunnerMethodGroup()
        //{
        //    Console.WriteLine("****** Method Group Conversion *****\n");

        //    Car c1 = new Car("SlugBug", 100, 10);

        //    //Register with the simple method name
        //    c1.RegisterWithCarEngineHandler(CallMeHere);

        //    //Speed up (this will trigger events).
        //    Console.WriteLine("***** Speeding up ******");
        //    for (int i = 0; i < 6; i++)
        //        c1.Accelerate(20);

        //    //Unregister the simple method name.
        //    c1.UnRegisterWithCarEngineHandler(CallMeHere);

        //    //No more notifications!
        //    for (int i = 0; i < 6; i++)
        //        c1.Accelerate(20);
        //}

        internal static void GenericDelegate()
        {
            Console.WriteLine("****** Generic Delegates *****\n");

            //register targets
            MyGenericDelegate <string> strTarget = new MyGenericDelegate <string>(StringTarget);

            strTarget("Some string data");

            MyGenericDelegate <int> intTarget = new MyGenericDelegate <int>(IntTarget);

            intTarget(2);
        }
Ejemplo n.º 29
0
        static void GenericDeleg()
        {
            Console.ForegroundColor = ConsoleColor.Cyan;
            Console.WriteLine("=> Generic Delegates");

            MyGenericDelegate <string> strTarget = new MyGenericDelegate <string>(StringTarget);

            strTarget("Some string data");

            MyGenericDelegate <int> intTarget = new MyGenericDelegate <int>(IntTarget);

            intTarget(9);
        }
Ejemplo n.º 30
0
        static void Main(string[] args)
        {
            Console.WriteLine("***** Generic Delegates *****\n");

            // Регистрация назначений.
            MyGenericDelegate <string> strTarget = new MyGenericDelegate <string>(StringTarget);

            strTarget("Some string data");

            MyGenericDelegate <int> intTarget = new MyGenericDelegate <int>(IntTarget);

            intTarget(9);
            Console.ReadLine();
        }
        static void Main(string[] args)
        {
            MyGenericDelegate <int> mydelegate1 = new MyGenericDelegate <int>(intdelegate);

            mydelegate1(25);
            mydelegate1 += intdelegate2;
            Console.WriteLine();
            mydelegate1(10);
            MyGenericDelegate <string> mydelegate2 = new MyGenericDelegate <string>(stringdelegate);

            mydelegate2("yo");

            Console.ReadKey();
        }
Ejemplo n.º 32
0
        private static void Main(string[] args)
        {
            Console.WriteLine("***** Generic Delegates *****\n");

            var strTarget = new MyGenericDelegate <string>(StringTarget);

            strTarget("Some string data");

            var intTarget = new MyGenericDelegate <int>(IntTarget);

            intTarget(9);

            Console.ReadLine();
        }
Ejemplo n.º 33
0
        private static void ThirdEvolution()
        {
            // generic delegates
            Console.WriteLine("***** Generic Delegates *****\n");

            // Register targets.
            MyGenericDelegate <string> strTarget = new MyGenericDelegate <string>(StringTarget);

            strTarget("Some string data");

            MyGenericDelegate <int> intTarget = new MyGenericDelegate <int>(IntTarget);

            intTarget(9);
        }
Ejemplo n.º 34
0
        static void GenericDelegate()
        {
            System.Console.WriteLine("=> generic delegate:");

            MyGenericDelegate <string> mgd1 = new MyGenericDelegate <string>(StringTarget);

            mgd1("hello world! Bonjour tout le monde!");

            MyGenericDelegate <int> mgd2 = new MyGenericDelegate <int>(IntTarget);

            mgd2(9);

            Console.ReadLine();
        }
Ejemplo n.º 35
0
        static void Main(string[] args)
        {
            Console.WriteLine("**** Generic Delegates *****\n");

            //register targets

            //create an instance of MyGenericDelegate<T> w string as param type
            MyGenericDelegate<string> strTarget = new MyGenericDelegate<string>(StringTarget);
            strTarget("Some string data");

            MyGenericDelegate<int> intTarget = new MyGenericDelegate<int>(IntTarget);
            intTarget(9);

            Console.ReadLine();
        }
Ejemplo n.º 36
0
        static void Main(string[] args)
        {
            //SimpleMath s = new SimpleMath();
            //BinaryOp b = new BinaryOp(s.Add);

            //WriteLine("10 + 10 = {0}", b(10, 10));

            //DisplayDelegateInfo(b);

            Car c1 = new Car("SlugBug", 100, 10);
            c1.RegisterWithCarEngine(OnCarEngineEvent);
            //c1.RegisterWithCarEngine(OnCarEngineEvent2);
            WriteLine("Speeding up");
            
            for (int i = 0; i < 8; i++)
            {
                c1.Accelerate(20);
            }

            ObtainCarDelegate targetA = new ObtainCarDelegate(GetBasicCar);
            Car c = targetA();
            WriteLine("Obtained a {0}", c);

            ObtainCarDelegate targetB = new ObtainCarDelegate(GetBasicCar2);
            SportCar d = (SportCar)targetB();
            WriteLine("Obtained a {0}", d);

            MyGenericDelegate<string> strTarget = new MyGenericDelegate<string>(StringTarget);
            strTarget("Some string data");

            MyGenericDelegate<int> intTarget = new MyGenericDelegate<int>(IntTarget);
            intTarget(9);

            ReadLine();
        }
Ejemplo n.º 37
0
    public static void TestGenDelegate()
    {
        MyGenericDelegate<string> strTarget =
            new MyGenericDelegate<string>(StringTarget);
        strTarget("yo str target");

        MyGenericDelegate<int> intTarget =
            new MyGenericDelegate<int>(IntTarget);
        intTarget(42);
    }
Ejemplo n.º 38
0
        /// <summary>
        /// 使用委托可以将多个方法绑定到委托变量,当调用此变量时,可以一次调用所有绑定的方法
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            heater2 heater22 = new heater2();
            Alarm2 alarm2 = new Alarm2();
            //为Boiled 事件绑定方法
            heater22.Boiled += alarm2.MakeAlert;
            heater22.Boiled += (new Alarm2()).MakeAlert;
            heater22.Boiled += new heater2.BoiledEventHandler(alarm2.MakeAlert);
            heater22.Boiled += Display2.ShowMsg;
            heater22.BoilWater();

            //
            Heater heater = new Heater();
            Alarm alarm = new Alarm();
            heater.BoilEvent += alarm.MakeAlert;                //绑定事件
            heater.BoilEvent += (new Alarm()).MakeAlert;        //绑定事件
            heater.BoilEvent += Display.ShowMsg;
            
            heater.BoilWater();

           // heater.BoilWater();


            GreetingManager2 gm2 = new GreetingManager2();
            //gm2.MakeGreet = ChineseGeting;
            gm2.MakeGreet += EnglishGeting;
            gm2.GreetPeople("hello world");
            GreetingManager gm = new GreetingManager();
            gm.GreetPeople("Jim", EnglishGeting);
            gm.GreetPeople("章子怡", ChineseGeting);
            //GreetingDelegate gd1, gd2;
            gm.gd1 = EnglishGeting;
            gm.gd1 += ChineseGeting;
            gm.GreetPeople("John", gm.gd1);



           // GreetingDelegate gd1,gd2;
           // gd1 = EnglishGeting;//如果第一次就使用“+=”,将出现“使用了未赋值的局部变量”的编译错误。
           // gd1 += ChineseGeting;
           // GreetPeople("Jim",EnglishGeting);
           // GreetPeople("Jim2", gd1);
           // Console.WriteLine("***下面测试绑定的顺序和执行的顺序***");
           // gd2 = ChineseGeting;
           // gd2 += EnglishGeting;
           // gd2 += ChineseGeting;
           // gd2("章子怡");
           // Console.WriteLine("***测试结束***");
           // gd1-=EnglishGeting;
           //// gd1 -= ChineseGeting;将方法全部解除之后会产生错误
           // GreetPeople("章子怡", ChineseGeting);
           // gd1("zhang");
           // GreetPeople("Andy",sayHello);

            Console.WriteLine("\u0001");
            Console.WriteLine("\u0002");
            Console.WriteLine("\u0003");
            Console.WriteLine("\u0004");
            Console.WriteLine('Q');
            MyGenericDelegate<string> strTarget = new MyGenericDelegate<string>(StringTarget);
            strTarget("hello world");
        }
Ejemplo n.º 39
-1
        static void Main(string[] args)
        {
            Console.WriteLine("***** Generic Delegates *****\n");

            // Register targets.
            MyGenericDelegate<string> strTarget = new MyGenericDelegate<string>(StringTarget);
            strTarget("Some string data");

            MyGenericDelegate<int> intTarget = new MyGenericDelegate<int>(IntTarget);
            intTarget(9);
            Console.ReadLine();
        }