Exemple #1
0
        static void Main(string[] args)
        {
            //FuncHandler f = Fn;

            //f();

            //
            //string s = "123";
            //string ss = new string(new char[] { '1', '2', '3' });


            //FuncHandler f = delegate()
            //{
            //    Console.WriteLine("我是匿名方法");
            //};

            //f();

            //FuncReturnIntHandler f = delegate() {
            //    return 1;
            //};
            //Console.WriteLine(f() + 1);

            //SumHandler sum = delegate(int num1, int num2)
            //{
            //    return num1 + num2;
            //};

            SumHandler sum = (n1, n2) => n1 + n2;

            // Lambda表达式,是对匿名方法的进一步简化

            Console.ReadKey();
        }
        public void Work02()
        {
            sumHandler  = Sum;
            sumHandler += Minus;
            //sumHandler(10, 5);

            calFloat = PlusFloat;
            calFloat(20.0f, 4.5f);

            //무명 델리게이트 메서드(Anoynimus Delegate)
            calString = delegate(string a, string b)
            {
                string sumStr = a + b;
                Console.WriteLine(sumStr);
            };

            calString("UserID ", "Zack");

            //람다식(Lamda Statement)
            calString = (string a, string b) =>
            {
                string sumStr = a + b;
                Console.WriteLine(sumStr);
            };

            calString = (string a, string b) => Console.WriteLine(a + b);
            calString("Player Level ", "Gold");
        }
Exemple #3
0
            public void StartExample()
            {
                ShowOne = delegate(string message)
                {
                    Console.BackgroundColor = ConsoleColor.Red;
                    IOService.ShowUserStringWithLineBreak(message);
                    Console.BackgroundColor = ConsoleColor.White;
                };
                ShowMessage("Hello from first anonim example!", ShowOne);
                ShowMessage("Hello from second anonim example!", ShowTwo = delegate(string message) // Объявление непосредственно перед
                                                                                                    // передачей в метод
                {
                    Console.BackgroundColor = ConsoleColor.Yellow;
                    IOService.ShowUserStringWithLineBreak(message);
                    Console.BackgroundColor = ConsoleColor.White;
                });
                ShowSum("Third function (returns int)", delegate(int x, int y)
                {
                    return(x + y);
                });
                ShowMessage("ExampleNumberFour", delegate
                {
                    IOService.ShowUserStringWithLineBreak("Nice One!");
                });

                SumTwo = delegate(int x, int y)
                {
                    IOService.ShowUserStringWithLineBreak("SumTwo anonim");
                    return(x + y);
                };

                SumTwo(5, 10);
            }
Exemple #4
0
        public void Work02()
        {
            sumHandler  = Sum;
            sumHandler += Minus;
            sumHandler(10, 5);

            calFloat = PlusFloat(20.0f, 4.5f);
        }
Exemple #5
0
    // Start is called before the first frame update
    void Start()
    {
        sumHandler = Sum;

        float sum = sumHandler(10.0f, 5.0f);

        Debug.Log($"Sum = {sum}");
    }
Exemple #6
0
    void Start()
    {
        //델리게이트 타입으로 선언된 변수에 함수를 저장
        //sumHandler = Sum;

        //무명 메소드 방식
        sumHandler += delegate(int a, int b)
        {
            sum = a + b;
            Debug.Log($"{a} + {b} = {sum}");
        };

        //람다식
        sumHandler += (int a, int b) => Debug.Log($"{a} * {b} = {a*b}");

        sumHandler(10, 20);
    }
        public double GeneralSum()
        {
            SumHandler += Sum;
            SumHandler += Sum;

            double result = 0;

            TryCatchMethod(() =>
            {
                foreach (var item in SumHandler.GetInvocationList())
                {
                    result += (double)item.DynamicInvoke(3, 5);
                }
            });
            Console.WriteLine(result);
            return(result);
        }
    void Start()
    {
        //델리게이트 변수에 함수(메소드) 연결(할당)
        sumHandler = Sum;
        //델리게이트 실행
        float sum = sumHandler(10.0f, 5.0f);

        //결괏값 출력
        Debug.Log($"Sum = {sum}");

        //델리게이트 변수에 람다식 선언
        sumHandler = (float a, float b) => (a + b);
        float sum2 = sumHandler(10.0f, 5.0f);

        Debug.Log($"Sum2 = {sum2}");

        //델리게이트 변수에 무명 메서드 연결
        sumHandler = delegate(float a, float b) { return(a + b); };
        float sum3 = sumHandler(2.0f, 3.0f);

        Debug.Log($"Sum3 = {sum3}");
    }
Exemple #9
0
        static void Main(string[] args)
        {
            Handler sum  = new SumHandler();
            Handler sub  = new SubHandler();
            Handler div  = new DivHandler();
            Handler mult = new MultHandler();

            sum.SetNext(sub);
            sub.SetNext(div);
            div.SetNext(mult);

            Request r1 = new Request(2, 2, "+");

            sum.HandleRequest(r1);
            r1 = new Request(10, 5, "-");
            sum.HandleRequest(r1);
            r1 = new Request(5, 2, "/");
            sum.HandleRequest(r1);
            r1 = new Request(2, 2, "*");
            sum.HandleRequest(r1);


            Console.ReadKey();
        }
Exemple #10
0
 private void ShowSum(string message, SumHandler show)
 {
     Console.Write(message + " ");
     IOService.ShowUserStringWithLineBreak(show(5, 5));
 }