Covariance and essays

When commissioned, usually there are two roles: broadcaster (broadcaster) and subscribers (subscriber).
The broadcaster is the type of field contains commission, commission decides when it is broadcast by calling.
The subscriber is the intended recipient approach. Subscribers by calling on the commission broadcaster + = and - = to decide when to listen and when to start listening end. Subscribers do not know will not interfere with other subscribers.
The event is the formal definition of the language features of this model. Event is a delegate with limited functionality implementation structure broadcast / subscriber model. The main purpose is to use the event to ensure that subscribers do not affect each other.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DelegateDemo
{
    class Program
    {
        public delegate void ProgressReporter(int percentComplete);
        static void Main(string[] args)
        {
            {
                //X x = new X();
                //ProgressReporter p = x.InstanceProcess;
                //p(99);
                //Console.WriteLine(p.Target == x);
                //Console.WriteLine(p.Method);

                //ProgressReporter pt = StaticProcess;
                //pt(100);
                //Console.WriteLine(pt.Target);
                //Console.WriteLine(pt.Method);
            }
            {
                //int[] values = { 1, 2, 3 };
                //Util.Transform(values, Square);
                //foreach (var item in values)
                //{
                //    Console.Write(item + " ");
                //}
            }
            {
                // suitable type of interface references
                 // inverter covariance inverse inverter can refer to a word, it is a sub-inverter => group, a group = covariant> promoter (more specifically type)
                 // covariant with key may output word is output out subclass   
                 // inverter keyword is input in the input parameters may be the base class
                 // example ITestOut interface <out T> Method, {T ();}
                 // example interface ITestIn <in <T> Method, void {(T obj);}
                 // event event is a limited use of the structure delegate function for broadcast / subscriber model using event does not affect the primary purpose is to ensure each other subscribers
                 // int [] = {. 4 Collection,. 5,. 6};
                 // UtilForItrans.TransformAll (Collection, new new Squarer ());
                 // the foreach (var Item in Collection)
                 //{
                //    Console.Write(item + " ");
                //}
            }
            {
                //StringAction sa = new StringAction(ActOnObject);
                //sa("hello");
                //ObjectRetriever or = new ObjectRetriever(RetrieveString);
                //Console.WriteLine(or());
            }
            Console.Read();

        }
        delegate object ObjectRetriever();
        delegate void StringAction(string s);
        private static void ActOnObject(object o) => Console.WriteLine(o);
        private static int Square(int arg) => arg * arg;
        static string RetrieveString() => "Hello";
        static void StaticProcess(int percentComplete)
        {
            Console.WriteLine(percentComplete);
        }
    }

    class X
    {
        public void InstanceProcess(int percentComplete)
        {
            Console.WriteLine(percentComplete);
        }
    }
    public delegate T Transformer<T>(T arg);
    public class Util
    {
        public static void Transform<T>(T[] values,Transformer<T> t)
        {
            for (int i = 0; i < values.Length; i++)
            {
                values[i] = t(values[i]);
            }
        }
    }

    public interface ITransformer
    {
        int Transform(int x);
    }

    public class UtilForItrans
    {
        public static void TransformAll(int[] values,ITransformer t)
        {
            for(int i = 0; i < values.Length; i++)
            {
                values[i] = t.Transform(values[i]);
            }
        }
    }

    class Squarer : ITransformer
    {
        public int Transform(int x) => x * x;
    }

    class Broadcaster
    { 
         PriceChangedHandler priceChanged; 
        public  Event{PriceChanged PriceChangedHandler 
            the Add {priceChanged + = value;} 
            Remove {priceChanged - = value;} 
        } 
    } 
    public  the delegate  void PriceChangedHandler ( decimal oldPrice, decimal NewPrice); 

    // if Event removed, without affecting the operation results, but the following effects:
     / / by re-assigning PriceChanged substitute other subscribers (not operator + =)
     // clear all subscribers (the setting position PriceChanged null)
     // by calling its delegate broadcast to the other subscribers 
    public  class Stock 
    { 
        String Symbol;
         decimal . price;
        public Stock(string symbol)
        {
            this.symbol = symbol;
        }

        public event PriceChangedHandler PriceChanged;

        public decimal Price {
            get {
                return price;
            }
            set {
                if (price == value) return;
                decimal oldPrice = price;
                price = value;
                if(PriceChanged != null)
                {
                    PriceChanged(oldPrice, price);
                }
            }
        }

    }
}

 

Guess you like

Origin www.cnblogs.com/morec/p/11904268.html