Ejemplo n.º 1
0
        // 定义一个Calc委托类型, 计算a*b
        //private delegate int Calc(int a, int b);

        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            // 调用GetClosureFunction()返回一个Func<int,int> 再传入一个int参数.
            // 那么即使在外部 GetClosureFunction里引用到的val值的空间不会被清除掉?

            // val 作为一个局部变量。它的生命周期本应该在GetClosureFunction执行完毕后就结束了。为什么还会对之后的结果产生影响呢?

            Console.WriteLine(GetClosureFunction()(30));
            Console.WriteLine("Check here");



            //// 这里本质还是Delegate.
            //Action<int, string> eat = Eat;
            //eat(5, "Apple");
            //// 这样可以把eat指向不同的函数.
            //// 从而可以不改变eat(5, "Apple")这种函数调用.

            //Calc calc = calcMul;

            //Func<int, int, int> calc = delegate (int x, int y)
            //  {
            //      return x * y;
            //  };

            //Func<int, int, int> calc = (x, y) =>
            //{
            //    return x * y;
            //};
            //Console.WriteLine(calc(10, 12));
        }
Ejemplo n.º 2
0
        //执行流程是Main函数调用GetClosureFunction函数,GetClosureFunction返回了委托internalAdd并被立即执行了
        static void Main(string[] args)
        {
            Console.WriteLine(GetClosureFunction()(30));//打印60=30+30,会引用局部变量val的最终值30

            int x = 5;

            Console.WriteLine("x1= {0}", x);
            outParaFunc(out x);
            Console.WriteLine("x2= {0}", x);


            Console.ReadKey();
        }
Ejemplo n.º 3
0
        static void Main(string[] args)
        {
            Console.WriteLine(GetClosureFunction()(30));


            //Program program = new Program();
            //// program.test(10);
            //float f = -123.567F; int i = (int)f;
            //Console.WriteLine(i);
            //Console.WriteLine("Hello World!");


            ////short s1 = 1; s1 = s1 + 1;//错误
            //short s1 = 1; s1 += 1;// 正确。
            //MyIndexer<int, string> myIndexer = new MyIndexerImp<int, string>();


            //myIndexer[0] = "1";
            //myIndexer[2] = "2";
            //myIndexer[3] = "3";


            //Console.WriteLine(myIndexer[0]);
        }
Ejemplo n.º 4
0
 static void Main(string[] args)
 {
     Console.WriteLine(GetClosureFunction()(30));
     Console.ReadKey();
 }
Ejemplo n.º 5
0
 static void Main(string[] args)
 {
     Console.WriteLine(GetClosureFunction()(30));//依次输出:20 40 60
 }