Ejemplo n.º 1
0
    public static void Main()
    {
        Application.Init();

        MonDelegate deleg = new MonDelegate(onClick);
        //Create the Window
        Window myWin = new Window("Brave new world");

        myWin.Resize(200, 200);
        HBox myBox = new HBox(false, 10);

        myWin.Add(myBox);

        // Set up a button object.
        Button ping = new Button("Ping");

        ping.Clicked += new EventHandler(deleg);
        myBox.Add(ping);

        // Set up a button object.
        Button pong = new Button("Pong");

        pong.Clicked += new EventHandler(deleg);
        myBox.Add(pong);

        //Test t = new Test (ping, pong);
        //t.b1.Clicked += new EventHandler(deleg);
        //myBox.Add(ping);
        //t.b2.Clicked += new EventHandler(deleg);
        //myBox.Add(pong);


        //Show Everything
        myWin.ShowAll();
        Application.Run();
    }
Ejemplo n.º 2
0
        public static void Run()
        {
            // 1 - Délégué classique
            // Assignation
            MonDelegate monDelegate = monImplementationDeDelegate;

            // Appel au délégué.
            monDelegate();

            // 2 - Délégué anonyme
            // Exemple de delegué anonyme
            MonDelegate monDelegateAnonyme = delegate()
            {
                // Implémentation du délégué anonyme
                Console.WriteLine("Mon implementation anonyme");
            };

            // Appel au délégué anonyme
            monDelegateAnonyme();

            // 3 - Expression Lambda
            MonDelegate monDelegateLambda = () =>
            {
                // Implémentation du délégué Lambda
                Console.WriteLine("Mon implementation Lambda");
            };

            // Appel au délégué Lambda
            monDelegateLambda();


            // 4 - Utilisation des delegués définis par le framework
            // Assignation
            Action monDelegateAction = monImplementationDeDelegate;

            // Appel au délégué.
            monDelegate();

            // 5 - Utilisation d'une action avec un parametre
            Action <string> monDelegateActionString = monImplementationDeDelegate;

            monDelegateActionString("Jean-Pierre");

            // 6 - Utilisation d'une fonction
            Func <string, int> maFunc = delegate(string chaine)
            {
                Console.WriteLine("Mon implementation Func");
                return(0);
            };
            int result = maFunc("Fonction");

            // 7 - Function + Lambda
            Func <int, int, int> maFuncLamda = (int a, int b) =>
            {
                Console.WriteLine("Mon implementation FuncLamda");
                return(a + b);
            };
            int resultmaFuncLamda = maFuncLamda(2, 3);

            Console.WriteLine("Resultat = " + resultmaFuncLamda);

            // 8 - Expression Lambda passée au framework dotnet
            List <int> l = new List <int>()
            {
                4, 6, 8, 2, 1
            };

            //bool resultExist = l.Exists(x => l.Contains(8));
            //bool resultExist = l.Exists(x => { return l.Contains(8); });
            //bool resultExist = l.Exists(delegate (int x) { return l.Contains(8); });
            listeCapturee = l;
            bool resultExist = l.Exists(monImplementationExist);

            // 9 - Exemple 2 :
            //var listeInferieurA5 = l.Where(x => x < 5);
            var listeInferieurA5 = from x in l where x < 5 select x;

            Console.WriteLine("Liste <5");
            foreach (var item in listeInferieurA5)
            {
                Console.WriteLine("  " + item);
            }

            Console.WriteLine("Exemple de Predicat : " + resultExist);
        }