Example #1
0
        static void Main(string[] args)
        {
            // Create a basic delegate that squares a number
            MyDelegate foo = (x) => x * x;

            Console.WriteLine("The result of foo is: {0}", foo(5));

            // Dynamically change the delegate to something else
            foo = (x) => x * 10;
            Console.WriteLine("The result of bar is: {0}", foo(5));

            // Create a delegate that takes multiple arguments
            MyDelegate2 bar = (x, y) => {
                Console.WriteLine("The two-arg lambda: {1}, {0}", x * 10, y);
            };

            bar(25, "Some string");

            // Define an expression delegate
            ExprDelegate baz = (x) => x > 10;

            Console.WriteLine("Calling baz with 5: {0}", baz(5));
            Console.WriteLine("Calling bax with 15: {0}", baz(15));

            // Keep the Command Line window open
            Console.WriteLine("\nPress Enter Key to Continue...");
            Console.ReadLine();
        }
Example #2
0
        public void DoIt()
        {
            MyDelegate foo = (x) => x * x;

            Console.WriteLine($"The result of foo is: {foo(5)}");

            // Dynamically change the delegate to something else
            foo = (x) => x * 10;
            Console.WriteLine($"The result of bar is: {foo(5)}");

            // Create a delegate that takes multiple arguments
            MyDelegate2 bar = (x, y) => {
                Console.WriteLine($"The two-arg lambda: {x*10}, {y}");
            };

            bar(25, "Some string");

            // Define an expression delegate
            ExprDelegate baz = (x) => x > 10;

            Console.WriteLine("Calling baz with 5: {0}", baz(5));
            Console.WriteLine("Calling bax with 15: {0}", baz(15));

            Console.WriteLine("\nPress Enter to Continue...");
            Console.ReadLine();
        }
Example #3
0
        static void Main(string[] args)
        {
            MyDelegate foo = (x) => x * x;

            Console.WriteLine("The result of foo is: {0}", foo(5));

            // Dynamically change the delegate to something else
            foo = (x) => x * 10;
            Console.WriteLine("The result of bar is: {0}", foo(5));

            // Create a delegate that takes multiple arguments
            MyDelegate2 bar = (x, y) =>
            {
                Console.WriteLine("The two-arg lambda: {1}, {0}", x * 10, y);
            };

            bar(25, "Some String");

            // Define an expression delegate
            ExprDelegate baz = (x) => x > 10;

            Console.WriteLine("Calling baz with 5: {0}", baz(5));
            Console.WriteLine("Calling baz with 15: {0}", baz(15));

            Console.WriteLine("\nPress Enter to continue...");
            Console.WriteLine();
        }
Example #4
0
        public static void Test1()
        {
            MyDelegate foo = (x) => x * x;

            Console.WriteLine("Result {0}", foo(5));

            foo = (x) => x * 10;
            Console.WriteLine("Result {0}", foo(5));

            MyDelegate2 bar = (x, y) =>
            {
                Console.WriteLine("Arguments x:{0} and y:{1}", x, y);
            };

            bar(25, "Some string");

            ExprDelegate baz = (x) => x > 10;

            Console.WriteLine("Baz with 5: {0}", baz(5));
            Console.WriteLine("Baz with 10: {0}", baz(15));
        }
Example #5
0
        delegate int ExprDelegate(int a, int b); // 리턴이 있는 무명 메소드

        private void Form1_Load(object sender, EventArgs e)
        {
            // 무명메소드
            // 기본형태는  delegate *** (int p) {본문}; 이다.
            // 이름이 없어서, 무명메소드이다. 기본적으로 delegate를 앞에 쓰고 마디를 만든 다음 ;를 찍어 완성한다.
            // 무명메소드는 delegate 타입에 무명메소드 자체를 할당할 수 있다.
            RunDelegate r = delegate(int p) { MessageBox.Show(p.ToString()); };

            r(123);  // 123부분이 int p로들어가서  메세지 박스로 가서 123이 출력되게 된다.

            ExprDelegate expr = delegate(int a, int b)
            {
                return(2 * a + b);
            };
            int result = expr(1, 2);

            button1.Click += Button1_Click; // 아래 적힌대로, inline 무명메소드를 사용해보자.
            button2.Click += delegate(object s, EventArgs ae)
            {
                MessageBox.Show("Button 2");
            };
        }
        static void Main(string[] args)
        {
            MyDelegate foo = (x) => x * x;

            Console.WriteLine("The result of foo is {0}", foo(5));
            foo = (x) => x * 10;
            Console.WriteLine("The result of bar is {0}", foo(5));


            MyDelegate2 bar = (x, y) =>
            {
                Console.WriteLine("The two arg lambda: {1} {0}", x * 10, y);
            };

            bar(25, "some string");

            ExprDelegate baz = (x) => x > 10;

            Console.WriteLine("Calling baz with 5 {0}", baz(5));
            Console.WriteLine("Calling baz with 15 {0}", baz(15));


            Console.ReadKey();
        }