Пример #1
0
        static void Main(string[] args)
        {
            //Ejemplo de constructor estático
            //Cuando ejecuto esta línea, se llama sólo al constructor estático y puedo acceder a las propiedades estáticas que éste inicializa
            System.Console.WriteLine("The background color is {0}", UserPreferences.BackgroundColor.Name);

            //Cuando creo una instancia usando el constructor no estático, se invoca ese constructor, y, en caso de que esta sea la primera carga de la clase
            //se llama antes al constructor estático
            UserPreferences ThisUserPreferences = new UserPreferences();

            //En este punto ya las propiedades estáticas están inicializadas
            ThisUserPreferences.PrintColor();


            //En este ejemplo veo que es lo mismo usar la propiedad estática Instance
            //que asignar una variable de tipo adecuado a la propiedad instance. Ambas referencias apuntan al mismo objeto
            System.Console.WriteLine("En la linea que sigue hago una declaración");
            SingletonClass unSingleton;

            System.Console.WriteLine("En la linea que sigue asigno instance a la variable declarada más arriba");
            unSingleton = SingletonClass.Instance;
            System.Console.WriteLine("Hecha la asignación");
            unSingleton.DoSomethingAlready();
            SingletonClass.Instance.DoSomethingAlready();


            //Ahora viene el código para los WeakEvents
            //Uso los mismos nombres que usé antes para poder comparar más fácilmente

            var deAutos = new CarDealer("deAutos.com");
            var carlos  = new Subscriber("Ricky Fort");

            NewCarWeakEventManager.AddListener(deAutos, carlos);
            //Antes era: deAutos.NewCarEvent += carlos.NewCarEventListener;


            //En la sig. línea no se invoca al evento directamente,
            //Se invoca un método que genera el evento. Esto quedó como antes
            deAutos.NewCar("Toyota Celica");

            var roberto = new Subscriber("Roberto Planta");

            NewCarWeakEventManager.AddListener(deAutos, roberto);
            //Antes era: deAutos.NewCarEvent += roberto.NewCarEventListener;
            deAutos.NewCar("Lumina");

            //Ahora quito un suscriptor
            NewCarWeakEventManager.RemoveListener(deAutos, roberto);
            //Antes era: deAutos.NewCarEvent -= roberto.NewCarEventListener;
            deAutos.NewCar("Rolls Royce");
        }
Пример #2
0
        static void Main(string[] args)
        {
            var newCarDealer = new CarDealer();
            var valtteri     = new Consumer("Valtteri");

            newCarDealer.NewCarInfo += valtteri.NewCarIsHere;
            newCarDealer.NewCar("Williams");

            var max = new Consumer("Max");

            newCarDealer.NewCarInfo += max.NewCarIsHere;
            newCarDealer.NewCar("Mercedes");
            newCarDealer.NewCarInfo -= valtteri.NewCarIsHere;
            newCarDealer.NewCar("Ferrari");
        }
Пример #3
0
        public static void main()
        {
            var dealer  = new CarDealer();
            var michael = new Consumer("Michael");

            dealer.NewCarInfo += michael.NewCarIsHere;

            dealer.NewCar("Ferrari");

            var sebastian = new Consumer("Sebastian");

            dealer.NewCarInfo += sebastian.NewCarIsHere;
            dealer.NewCar("Mercedes");

            dealer.NewCarInfo -= michael.NewCarIsHere;
            dealer.NewCar("Red Bull Racing");
        }
Пример #4
0
        public void TestWeakEvents()
        {
            var dealer = new CarDealer();

            var michael = new Consumer("Michael");

            WeakCarInfoEventManager.AddListener(dealer, michael);
            dealer.NewCar("Mercedes");

            var sebastian = new Consumer("Sebastian");

            WeakCarInfoEventManager.AddListener(dealer, sebastian);
            dealer.NewCar("Ferrari");

            WeakCarInfoEventManager.RemoveListener(dealer, michael);
            dealer.NewCar("Red Bull Racing");
        }
Пример #5
0
    public static void Main()
    {
        // Create dealer and customer
        var dealer  = new CarDealer();
        var michael = new Consumer("Michael");

        // attach a listener to and event - use the += notation
        dealer.NewCarEvent += michael.NewCarIsHere;
        dealer.NewCar("Ferrari");
        dealer.NewCarEvent -= michael.NewCarIsHere;

        var nick = new Consumer("Sebastian");

        dealer.NewCarEvent += nick.NewCarIsHere;
        dealer.NewCar("Mercedes");
        dealer.NewCarEvent -= nick.NewCarIsHere;
        dealer.NewCar("Red Bull Racing");
    }
Пример #6
0
        static void Main()
        {
            var dealer = new CarDealer();

            var michael = new Consumer("Michael");
            dealer.NewCarInfo += michael.NewCarIsHere;

            dealer.NewCar("Mercedes");

            var nick = new Consumer("Nick");
            dealer.NewCarInfo += nick.NewCarIsHere;

            dealer.NewCar("Ferrari");

            dealer.NewCarInfo -= michael.NewCarIsHere;

            dealer.NewCar("Toyota");
        }
Пример #7
0
        static void Main()
        {
            var dealer = new CarDealer();

            var michael = new Consumer("Michael");
            WeakCarInfoEventManager.AddListener(dealer, michael);

            dealer.NewCar("Mercedes");

            var nick = new Consumer("Nick");
            WeakCarInfoEventManager.AddListener(dealer, nick);

            dealer.NewCar("Ferrari");

            WeakCarInfoEventManager.RemoveListener(dealer, michael);

            dealer.NewCar("Toyota");
        }
Пример #8
0
        static void Main()
        {
            var dealer = new CarDealer();

              var michael = new Consumer("Michael");
              WeakCarInfoEventManager.AddListener(dealer, michael);

              dealer.NewCar("Mercedes");

              var sebastian = new Consumer("Sebastian");
              WeakCarInfoEventManager.AddListener(dealer, sebastian);

              dealer.NewCar("Ferrari");

              WeakCarInfoEventManager.RemoveListener(dealer, michael);

              dealer.NewCar("Red Bull Racing");
        }
    static void Main()
    {
      var dealer = new CarDealer();

      var daniel = new Consumer("Daniel");
      dealer.NewCarInfo += daniel.NewCarIsHere;

      dealer.NewCar("Mercedes");

      var sebastian = new Consumer("Sebastian");
      dealer.NewCarInfo += sebastian.NewCarIsHere;

      dealer.NewCar("Ferrari");

      dealer.NewCarInfo -= sebastian.NewCarIsHere;


      dealer.NewCar("Red Bull Racing");
    }
Пример #10
0
        static void Main06()
        {
            var dealer = new CarDealer();

            var michael = new Consumer("Micheal");

            dealer.NewCarInfo += michael.NewCarIsHere;

            dealer.NewCar("Mercedes");

            var nick = new Consumer("Nick");

            dealer.NewCarInfo += nick.NewCarIsHere;

            dealer.NewCar("Ferrari");

            dealer.NewCarInfo -= michael.NewCarIsHere;

            dealer.NewCar("Toyota");
        }
Пример #11
0
        static void Main()
        {
            var dealer = new CarDealer();

            var michael = new Consumer("Michael");

            WeakCarInfoEventManager.AddListener(dealer, michael);

            dealer.NewCar("Mercedes");

            var nick = new Consumer("Nick");

            WeakCarInfoEventManager.AddListener(dealer, nick);

            dealer.NewCar("Ferrari");

            WeakCarInfoEventManager.RemoveListener(dealer, michael);

            dealer.NewCar("Toyota");
        }
Пример #12
0
        static void Main()
        {
            var dealer = new CarDealer();

            var daniel = new Consumer("Daniel");

            dealer.NewCarInfo += daniel.NewCarIsHere;

            dealer.NewCar("Mercedes");

            var sebastian = new Consumer("Sebastian");

            dealer.NewCarInfo += sebastian.NewCarIsHere;

            dealer.NewCar("Ferrari");

            dealer.NewCarInfo -= sebastian.NewCarIsHere;


            dealer.NewCar("Red Bull Racing");
        }
Пример #13
0
        static void Main()
        {
            var dealer = new CarDealer();

            var micheal = new Consumer("micheal");

            dealer.NewCarInfo += micheal.NewcarIsHere;
            dealer.NewCar("Ferrari");

            //var sebastian = new Consumer("sebastian");
            //dealer.NewCarInfo += sebastian.NewcarIsHere;
            //dealer.NewCar("Mercedes");
            //dealer.NewCarInfo += sebastian.NewcarIsHere;

            //dealer.NewCarInfo -= micheal.NewcarIsHere;
            //dealer.NewCar("Red Bull Racing");

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
Пример #14
0
        // 弱事件示例
        private static void WeakEvent()
        {
            var dealer = new CarDealer();
            var michael = new Consumer("Michael");

            WeakCarInfoEventManager.AddListener(dealer, michael);

            dealer.NewCar("Mercedes");

            Console.ReadKey();
        }
Пример #15
0
        static void Main(string[] args)
        {
            //int x = 40;
            //GetAString firstStringMethod=new GetAString(x.ToString);
            //Console.WriteLine($"String is {firstStringMethod}");

            //int x = 40;
            //GetAString firstStringMethod = x.ToString;
            //Console.WriteLine($"{firstStringMethod}");
            //var balance=new Currency(34,50);
            //firstStringMethod = balance.ToString;
            //Console.WriteLine($"{firstStringMethod}");
            //firstStringMethod=new GetAString(Currency.GetCurrencyUnit);
            //Console.WriteLine($"{firstStringMethod}");

            //DoubleOp[] operations =
            //{
            //    MathOperations.MultiplyByTwo,
            //    MathOperations.Square
            //};
            //for (int i = 0; i < operations.Length; i++)
            //{
            //    Console.WriteLine($"Using operations[{i}]");
            //    ProcessAndDisplayNumber(operations[i], 2.0);
            //    ProcessAndDisplayNumber(operations[i], 7.94);
            //    ProcessAndDisplayNumber(operations[i],1.414);
            //    Console.WriteLine();
            //}

            //Employee[] employees =
            //{
            //    new Employee("a", 20000),
            //    new Employee("b",20000),
            //    new Employee("c",20000),
            //    new Employee("d",20000),
            //    new Employee("e",20000),
            //};
            //BubbleSorter.Sort(employees,Employee.CompareSalary);
            //foreach (var employee in employees)       {
            //    Console.WriteLine(employee);
            //}
            //Action<double> operation = MathOperations.MultiplyByTwo;
            //operation += MathOperations.Square;
            //ProcessAndDisplayNumber(operation,2.0);
            //ProcessAndDisplayNumber(operation, 7.94);
            //ProcessAndDisplayNumber(operation, 1.414);
            //Console.WriteLine(1);
            Action d1 = One;

            d1 += Two;
            try
            {
                d1();
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception caught1");
            }

            string mid = ",middle part,";
            Func <string, string> lambda = param =>
            {
                param += mid;
                param += " amd this was added to the string";
                return(param);
            };

            Console.WriteLine(lambda("Start of string"));

            var dealer   = new CarDealer();
            var valtteri = new Consumer("Valtteri");

            dealer.NewCarInfo += valtteri.NewCarIsHere;
            dealer.NewCar("Williams");

            var max = new Consumer("Max");

            dealer.NewCarInfo += max.NewCarIsHere;
            dealer.NewCar("Mercedes");
            dealer.NewCarInfo -= valtteri.NewCarIsHere;
            dealer.NewCar("Ferrari");
        }
        static void Main(string[] args)
        {
            int        x = 40;
            GetAString firstStringMethod = x.ToString;

            Console.WriteLine($"String is {firstStringMethod()}");
            var balance = new Currency(34, 50);

            firstStringMethod = balance.ToString;
            Console.WriteLine($"String is {firstStringMethod()}");

            firstStringMethod = new GetAString(Currency.GetCurrencyUnit);
            Console.WriteLine($"String is {firstStringMethod()}");
            Console.WriteLine($"------------------");
            DoubleOp[] operations =
            {
                MathOperations.MultiplayByTwo,
                MathOperations.Square
            };
            //声明参数类型是double,返回类型是double的Func委托,
            //对于Func<a,b>, b类型是返回类型,a类型是输入参数类型,输入参数类型可以有多个
            //例如Func<a,a,c,b>,表示3个参数,分别是a类型,a类型,c类型,返回值是b类型
            Func <double, double>[] funcOperations =
            {
                MathOperations.MultiplayByTwo,
                MathOperations.Square
            };
            for (int i = 0; i < operations.Length; i++)
            {
                Console.WriteLine($"Using operation[{i}]");
                ProcessAndDisplayNumber(operations[i], 2.0);
                ProcessAndDisplayNumber(operations[i], 7.94);
                ProcessAndDisplayNumber(operations[i], 1.414);
            }

            for (int i = 0; i < funcOperations.Length; i++)
            {
                Console.WriteLine($"Using operation[{i}]");
                ProcessFuncAndDisplayNumber(funcOperations[i], 2.0);
                ProcessFuncAndDisplayNumber(funcOperations[i], 7.94);
                ProcessFuncAndDisplayNumber(funcOperations[i], 1.414);
            }

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

            Employee[] employees =
            {
                new Employee("Tom",       20000),
                new Employee("Jack",      10000),
                new Employee("Bob",       15000),
                new Employee("Roy",  100000.38m),
                new Employee("Lily",      23000),
                new Employee("Jean",      50000),
            };
            //将Employee.CompareSalary方法作为委托参数传入BubbleSorter.Sort方法
            BubbleSorter.Sort(employees, Employee.CompareSalary);
            foreach (var item in employees)
            {
                Console.WriteLine(item);
            }

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

            //多播委托,一个委托可以包含多个方法,但是委托签名必须返回void,否则只能得到委托调用最后一个方法的结果
            Action <double> actionOperations = MathOperations.ActionMultiplayByTwo;

            actionOperations += MathOperations.ActionSquare;
            ProcesActionAndDisplayNumber(actionOperations, 2.0);
            ProcesActionAndDisplayNumber(actionOperations, 7.94);
            ProcesActionAndDisplayNumber(actionOperations, 1.414);
            Console.WriteLine($"------------------");

            string mid = ", middle part,";
            //匿名方法
            //   Func<string, string> anonDel = delegate(string param)
            //   {
            //       param += mid;
            //       param += " and this was added to the string";
            //       return param;
            //};
            //使用lambda表达式的匿名方法,参数的类型和委托定义的类型对照
            Func <string, string> anonDel = param =>
            {
                param += mid;
                param += " and this was added to the string";
                return(param);
            };

            Console.WriteLine(anonDel("Start of string"));

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

            var dealer   = new CarDealer();
            var valtteri = new Consumer("Valtteri");

            dealer.NewCarInfo += valtteri.NewCarIsHere;
            dealer.NewCar("Williams");

            var max = new Consumer("Max");

            dealer.NewCarInfo += max.NewCarIsHere;
            dealer.NewCar("Mercedes");
            dealer.NewCarInfo -= valtteri.NewCarIsHere;
            dealer.NewCar("Ferrari");
        }