Exemplo n.º 1
0
        static void Main(string[] args)
        {
            mydel md = new mydel(add);

            Console.WriteLine(md(10, 20));
            md = md + new mydel(sub);
            Console.WriteLine(md(2, 3));
            mydel1 md1 = new mydel1(square);

            Console.WriteLine(md1(2));
            Console.ReadKey();
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            Program p  = new Program();
            mydel1  d1 = p.myfunc1;

            System.Console.WriteLine(d1(5));
            d1 = p.myfunc2;
            System.Console.WriteLine(d1(5));
            // lambda operator "=>": anonymous, inline, on the fly
            mydel2 d2 = (a, b) => a + b * 2;

            System.Console.WriteLine(d2(2, 3));
        }
Exemplo n.º 3
0
        public void UseMulticast()
        {
            //dlegate è immutabile
            //internamente viene crato uin nuovo delegat
            //il delegate è un tipo e
            //quindi adesso assegno al
            //multicast di tipo delegate
            //un metodo che rispetta la firma del delegate
            //e poi uso la variabile di tipo delegate
            //come se stessi usando il metodo
            //posso assegnare ad un delegate in 4 modi :
            //1 metodo anonimo delegate{espressione}
            //2 lambda
            //3 nome della funzione
            //4 espressione

            EmptyDel multicast = ToLow;

            multicast += ToUp;

            //metodo anonimo
            multicast = delegate { return(""); };
            multicast = Convert.ToString;

            //lambda expression C#6
            //(parametro) => espressione
            string mystring = "closure";

            multicast = s => mystring;//CLOSURE uso variabile dichiarata fuori
            //ATTENZIONE LA VARIABILE MYSTRING VIENE VALUTATA QUANDO VIENE USATO
            //IL DELEGATE!!!
            mystring = "closure!";//multicast stampa closure!
            Console.WriteLine(multicast("roberto"));
            int       x          = 0;
            EmptyDel1 multicast1 = Add1;

            multicast1 += Add2;
            multicast1(ref x);//ESEGUO LE FUNZIONI CHE SI SONO REGISTRATE A MULTICAST

            Console.WriteLine("RISULTATO = " + x);
            multicast -= ToUp;

            var m2 = new mydel1(Convert.ToInt32);
            var a  = m2("3");
            var b  = m2.Invoke("3");
        }
Exemplo n.º 4
0
 public int MyCon(mydel1 callback, string tocon)
 {
     return(callback(tocon));
 }