예제 #1
0
        static void Main(string[] args)
        {
            // now we'll call sayhello by passing it as a parameter
            // SayGreeting sayGreeting = new SayGreeting(SayHello);
            // telling the delegate to run the sayhello method when it's called
            // now we can call saygreeting like it's a method

/*
 *    Console.WriteLine("what's your name?");
 *    string input = Console.ReadLine();
 *    sayGreeting(input);
 *    Console.ReadLine();
 *    sayGreeting = new SayGreeting(SayGoodbye);
 *    sayGreeting(input);
 */
            // now we clean up the above
            SayGreeting sayGreeting = delegate(string name)
            {
                Console.WriteLine(string.Format("Hello, {0}", name));
                // now the delegate is pointing at an anonymous method
                // we can now delete say hello method because we won't need it. we only run it once
            };


            Console.WriteLine("what's your name?");
            string input = Console.ReadLine();

            sayGreeting(input);
            Console.ReadLine();
            sayGreeting = new SayGreeting(SayGoodbye);
            sayGreeting(input);
        }
예제 #2
0
        static void Main(string[] args)
        {
            // assigned SayGreeting to the delegate, this will be triggered only once.
            SayGreeting sayGreeting = delegate(string name)
            {
                Console.WriteLine(string.Format("Hello, {0}", name));
            };

            Func <string, string> conversate = delegate(string message)
            {
                Console.WriteLine(message);
                return(Console.ReadLine());
            };


            Console.WriteLine("Whats your name:");
            string input = Console.ReadLine();

            sayGreeting(input); // Hello

            //use this for lambda
            //sayGreeting = (greeting) =>
            sayGreeting = delegate(string name)
            {
                Console.WriteLine(string.Format("Later, {0}", name));
            };
            Console.ReadLine();
            sayGreeting(input); //Later
            Console.ReadKey();

            // example using func
            conversate("TEST");
        }
예제 #3
0
        static void Main(string[] args)
        {
            SayGreeting sayGreeting = delegate(string name) {
                Console.WriteLine(string.Format("Hello, {0}", name));
            };

            Console.WriteLine("What's your name?");
            string input = Console.ReadLine();

            sayGreeting(input);

            sayGreeting = new SayGreeting(SayGoodbye);
        }
        static void TestDelegates()
        {
            // these three delegate declarations produce exactly the same code
            var             sayGreeting  = new SayGreeting(SayHello);
            SayGreeting     sayGreeting1 = SayHello;
            Action <string> sayGreeting2 = SayHello;

            sayGreeting("Nick");

            // use an anonymous method instead of a named one
            Action <string> sayBye = (name) => Console.WriteLine(string.Format("Bye, {0}", name));

            sayBye("Nick");
        }
예제 #5
0
        static void Main(string[] args)
        {
            //SayGreeting sayGreeting = new SayGreeting(SayHello);
            SayGreeting sayGreeting = delegate(string name)  //anonymous method, a method that doesn't have a name. functionality is defined as we assign it to the delegate
            {
                Console.WriteLine(string.Format("Hello, {0}", name));
            };

            Console.WriteLine("What's your name? ");
            string input = Console.ReadLine();

            sayGreeting(input);

            Console.ReadLine();

            sayGreeting = new SayGreeting(SayGoodbye);
            sayGreeting(input);

            Console.ReadLine(); //used to wait before closing CLI
        }