Beispiel #1
0
        static void Main(string[] args)
        {
            var heater = new Heater();
            var alarm  = new Alarm();

            heater.Boiled += alarm.MakeAlert; //注册方法
            //heater.Boiled += new BoiledEventHandler(alarm.MakeAlert);    //也可以这么注册(实例化委托并传入方法即为push/注册方法)
            heater.Boiled += Display.ShowMsg; //注册静态方法

            heater.BoilWater();               //烧水,会自动调用注册过对象的方法
        }
Beispiel #2
0
        static void Main1()
        {
            Heater heater = new Heater();
            Alarm  alarm  = new Alarm();

            heater.Boiled += alarm.MakeAlert;                                //注册方法
            heater.Boiled += (new Alarm()).MakeAlert;                        //给匿名对象注册方法
            heater.Boiled += new Heater.BoiledEventHandler(alarm.MakeAlert); //也可以这么注册
            heater.Boiled += Display.ShowMsg;                                //注册静态方法

            heater.BoilWater();                                              //烧水,会自动调用注册过对象的方法
        }
Beispiel #3
0
        static void Main1()
        {
            Heater heater = new Heater();
            Alarm  alarm  = new Alarm();

            //heater.Boiled += alarm.MakeAlert; //注册方法
            //heater.Boiled += (new Alarm()).MakeAlert; //给匿名对象注册方法

            heater.Boiled += new Heater.BoiledEventHandler(alarm.MakeAlert); //也可以这么注册

            heater.Boiled += Display.ShowMsg;                                //注册静态方法

            heater.BoilWater();                                              //烧水,会自动调用注册过对象的方法



            Console.Write(TicketType.MasterCookieName.ToString() == "MasterCookieName");

            string xml = "<xml><row><AreaId>1</AreaId></row><row><AreaId>2</AreaId></row><row><AreaId>3</AreaId></row></xml>";

            System.Xml.XmlDocument xmlString = new System.Xml.XmlDocument();
            xmlString.LoadXml(xml);

            IList <CityBase> city  = new List <CityBase>();
            CityBase         model = new CityBase();

            model.CityName = "A";
            model.ID       = 1;
            CityBase model1 = new CityBase();

            model.CityName = "B";
            model.ID       = 2;
            city.Add(model);
            city.Add(model1);

            IList <CityBase> city2 = city.Where(item => (item.ID == 2 && item.CityName == "C")).ToArray();

            Console.Write(city2.Count);

            Console.Read();
        }
Beispiel #4
0
        static void Main(string[] args)
        {
            #region 委托示例1 (事件)
            ///* 项目场景描述:
            //* 我从明天早上开始要早起晨读,室友每天都七点起床晨跑,我告诉他明天起来的时候把我叫醒,并让我晨读
            //* PS:还有个王八犊子非要跟我赌五毛我起不来,还让明天室友也叫他,见证我到底有没有起来
            //*/

            ////首先你们三个得存在吧
            //var 中国好室友 = new 室友();
            //var 我 = new 要晨读的我();
            //var 王八犊子 = new 王八犊子();

            ////我先告诉室友:我要早起晨读
            //中国好室友.叫别人起床该干嘛干嘛 += 我.起床晨读;
            ////王八犊子来搅屎了
            //中国好室友.叫别人起床该干嘛干嘛 += 王八犊子.赌五毛起不来;
            ////于是第二天到了
            //Console.WriteLine("闹铃:早上七点钟...");
            //Console.WriteLine("系统提示:室友起床了!");
            //Console.WriteLine();
            //中国好室友.起床晨跑去();
            //Console.WriteLine();
            //Console.WriteLine("==========全剧终==========");
            #endregion
            Console.ReadLine();
            #region 委托示例2
            //GreetPeople("Jimmy Zhang", EnglishGreeting);
            //GreetPeople("张子阳", ChineseGreeting);//ChineseGreeting是实参
            //Console.ReadKey();
            #endregion
            #region 委托示例3
            //string name1, name2;
            //name1 = "Jimmy Zhang";
            //name2 = "张子阳";

            //GreetPeople(name1, EnglishGreeting);
            //GreetPeople(name2, ChineseGreeting);
            //Console.ReadKey();
            #endregion
            #region 委托示例4
            //GreetingDelegate delegate1, delegate2;
            //delegate1 = EnglishGreeting;
            //delegate2 = ChineseGreeting;

            //GreetPeople("Jimmy Zhang", delegate1);
            //GreetPeople("张子阳", delegate2);
            //Console.ReadKey();
            #endregion
            #region 委托示例5 用+=方式的委托,一次会执行所有+=过的方法
            //GreetingDelegate delegate1;
            //delegate1 = EnglishGreeting; // 先给委托类型的变量赋值
            //delegate1 += ChineseGreeting;   // 给此委托变量再绑定一个方法

            //// 将先后调用 EnglishGreeting 与 ChineseGreeting 方法
            //GreetPeople("Jimmy Zhang", delegate1);
            //Console.ReadKey();
            #endregion
            #region 委托示例6 直接调用委托
            //GreetingDelegate delegate1;//事件的定义其实和这里类似,是对委托的一种实例化,进而实现+=,-=的注册方法
            #region 事件和委托的关系eg
            ////public delegate void delegate2(int param);   //把delegate2声明成委托类型
            ////public event delegate2 delegate3;            //把delegate3声明成一个delegate2类型的事件,这里的delegate3可以和delegate1具有相同的功能了(+=和-=)
            #endregion
            //delegate1 = EnglishGreeting; // 先给委托类型的变量赋值
            //delegate1 += ChineseGreeting;   // 给此委托变量再绑定一个方法

            //// 将先后调用 EnglishGreeting 与 ChineseGreeting 方法
            //delegate1("Jimmy Zhang");
            //Console.ReadKey();
            #endregion

            #region 观察者模式 委托 热水器示例
            Heater heater = new Heater();
            Alarm  alarm  = new Alarm();

            heater.BoilEvent += alarm.MakeAlert;         //注册方法
            heater.BoilEvent += (new Alarm()).MakeAlert; //给匿名对象注册方法(效果同上)
            heater.BoilEvent += Display.ShowMsg;         //注册静态方法

            heater.BoilWater();                          //烧水,会自动调用注册过对象的方法
            Console.ReadLine();
            #endregion
        }
Beispiel #5
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");
        }