예제 #1
0
        static void Main(string[] args)
        {
            CallBackFunc(CallPrint, "Hello");                                        //함수 연결
            CallBackFunc(delegate(string str) { Console.WriteLine(str); }, "Hello"); //delegate 직접
            CallBackFunc((string str) => { Console.WriteLine(str); }, "Hello");      //람다의 식형태
            CallBackFunc((str) => Console.WriteLine(str), "Hello");                  //람다식 기본..
            CallBackFunc(str => Console.WriteLine(str), "Hello");

            dFunc dfunc = () => Console.WriteLine("No Params"); //파라미터가 없는 경우 () 반드시 사용
        }
예제 #2
0
        static void Main(string[] args)
        {
            CallBackFunc(CallPrint, "Hello"); //함수 연결
            CallBackFunc(delegate(string str) { Console.WriteLine(str); }, "Hello");
            CallBackFunc((string str) => { Console.WriteLine(str); }, "Hello");
            CallBackFunc((str) => { Console.WriteLine(str); }, "Hello");
            CallBackFunc(str => Console.WriteLine(str), "Hello"); // 가장 심플해서 많이 쓰임

            dFunc dfunc = () => Console.WriteLine("No Params");   // 파라미터가 없으면 ()를 써야함
        }
예제 #3
0
        public static void TestFunctionExpression()
        {
            Assert.AreEqual(2, mFunc());
            Assert.AreEqual(9, aFunc(3));

            dFunc func = new dFunc(Area);

            Console.WriteLine(func(4));
            dFunc circumference = new dFunc(delegate(int r) { return(6 * r); });

            Assert.AreEqual(30, circumference(5));

            dFunc circ = r => 6 * r;

            Assert.AreEqual(48, circ(8));
            Func <double, double> f = r => 6 * r;

            Assert.AreEqual(54, f(9));

            int ai(int _x) => _x + 1;

            Assert.AreEqual(6, ai(5));

            // Action<int> a = ai;
            // Assert.AreEqual(6, a(5));

            Action av = () => Console.WriteLine("av");

            av();

            Predicate <int> p = _x => _x == 3;

            Assert.False(p(4));

            int x = 3, y = 4;
            BinaryExpression b1 = Expression.MakeBinary(ExpressionType.Add,
                                                        Expression.Constant(1), Expression.Constant(2));
            BinaryExpression b2 = Expression.MakeBinary(ExpressionType.Add,
                                                        Expression.Constant(x), Expression.Constant(y));

            BinaryExpression b3 = Expression.MakeBinary(ExpressionType.Subtract, b1, b2);
            int result          = Expression.Lambda <Func <int> >(b3).Compile()();

            Assert.AreEqual(-4, result); // 3-7
        }