C#闭包陷阱

2019-06-01 14:01:03 浏览数 (1)

用例:

代码语言:javascript复制
class Program
    {
        static void Main(string[] args)
        {
            List<Person> li = new List<Person>()
            {
                new Person("kk"),
                new Person("cc"),
            };
            Action[] array = new Action[2];
            for (int i = 0; i < 2; i  )
            {
                array[i] = () => { Console.WriteLine(li[i].name);};
            }
            for (int i = 0; i < 2; i  )
            {
                array[i]();
            }
        }
    }
    public class Person
    {
        public string name;

        public Person()
        {
        }

        public Person(string name)
        {
            this.name = name;
        }
    }

反编译 反编译可以看到在Main中有一个<>c__DisplayClass0_2.i,此变量会在复制后变为赋值时循环的次数加一

代码语言:javascript复制
private static void Main(string[] args)
        {
            Program.<>c__DisplayClass0_0 <>c__DisplayClass0_ = new Program.<>c__DisplayClass0_0();
            <>c__DisplayClass0_.li = new List<Person>
            {
                new Person("kk"),
                new Person("cc")
            };
            Action[] array = new Action[2];
            Program.<>c__DisplayClass0_1 <>c__DisplayClass0_2 = new Program.<>c__DisplayClass0_1();
            <>c__DisplayClass0_2.CS$<>8__locals1 = <>c__DisplayClass0_;
            <>c__DisplayClass0_2.i = 0;
            while (<>c__DisplayClass0_2.i < 2)
            {
                array[<>c__DisplayClass0_2.i] = new Action(<>c__DisplayClass0_2.<Main>b__0);
                int i = <>c__DisplayClass0_2.i;
                <>c__DisplayClass0_2.i = i   1;
            }
            for (int j = 0; j < 2; j  )
            {
                array[j]();
            }
        }

修改后:

代码语言:javascript复制
static void Main(string[] args)
        {
            List<Person> li = new List<Person>()
            {
                new Person("kk"),
                new Person("cc"),
            };
            Action[] array = new Action[2];
            for (int i = 0; i < 2; i  )
            {
                int index = i;
                array[index] = () => { Console.WriteLine(li[index].name);};
            }
            for (int i = 0; i < 2; i  )
            {
                array[i]();
            }
        }

反编译:

代码语言:javascript复制
private static void Main(string[] args)
        {
            Program.<>c__DisplayClass0_0 <>c__DisplayClass0_ = new Program.<>c__DisplayClass0_0();
            <>c__DisplayClass0_.li = new List<Person>
            {
                new Person("kk"),
                new Person("cc")
            };
            Action[] array = new Action[2];
            for (int i = 0; i < 2; i  )
            {
                Program.<>c__DisplayClass0_1 <>c__DisplayClass0_2 = new Program.<>c__DisplayClass0_1();
                <>c__DisplayClass0_2.CS$<>8__locals1 = <>c__DisplayClass0_;
                <>c__DisplayClass0_2.index = i;
                array[<>c__DisplayClass0_2.index] = new Action(<>c__DisplayClass0_2.<Main>b__0);
            }
            for (int j = 0; j < 2; j  )
            {
                array[j]();
            }
        }

0 人点赞