前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C#闭包陷阱

C#闭包陷阱

作者头像
祝你万事顺利
发布2019-06-01 14:01:03
7040
发布2019-06-01 14:01:03
举报
文章被收录于专栏:Unity游戏开发

用例:

代码语言: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]();
            }
        }
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019.05.31 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档