C#模仿JS闭包

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Test
{
    delegate void MyDelegate();
    delegate MyClosure Closure();
    class Program
    {
        static void Main(string[] args)
        {
            Closure clo = new Closure(Fun);
            MyClosure mm = clo();
            mm.Show();
            mm.Add();
            mm.Show();
            mm.Add();
            mm.Show();
        }

        static MyClosure Fun()
        {
            int num = 2;
            MyClosure myClosure = new MyClosure();
            myClosure.Add = () => { num++; };
            myClosure.Show = () => { Console.WriteLine(num); };
            return myClosure;


        }
    }

    class MyClosure
    {
        public MyDelegate Add;
        public MyDelegate Show;
    }
}
原文地址:https://www.cnblogs.com/mxw09/p/1790475.html