问题在C#中的代表 [英] Problem with delegates in C#

查看:99
本文介绍了问题在C#中的代表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的程序,DummyMethod总是打印5.但是,如果我们使用的注释代码代替,我们得到不同的值(即1,2,3,4)。 ?任何人都可以请解释为什么这是happenning

 委托INT Methodx(obj对象); 

静态INT DummyMethod(int i)以
{
Console.WriteLine(在DummyMethod方法I =+ I);
返回1 + 10;
}

静态无效的主要(字串[] args)
{
名单,LT; Methodx>方法=新的List< Methodx>();

的for(int i = 0;我小于5; ++ I)
{
methods.Add(委托(obj对象){返回DummyMethod(我);} );
}

//methods.Add(delegate(object物镜){返回DummyMethod(1);});
//methods.Add(delegate(object物镜){返回DummyMethod(2);});
//methods.Add(delegate(object OBJ){返回DummyMethod(3);});
//methods.Add(delegate(object物镜){返回DummyMethod(4);});

的foreach(在方法VAR法)
{
INT C =法(NULL);
Console.WriteLine(在main方法C =+ C);
}
}



此外,如果使用下面的代码,我得到的。期望的结果

 的for(int i = 0; I< 5; ++ I)
{
INT J =;
methods.Add(委托(obj对象){返回DummyMethod(J);});
}


解决方案

的问题是,你再在每一个委托捕捉相同的变量 I - 它通过循环刚刚结束的值为5



相反,你希望每个代表捕捉到一个不同的变量,这意味着在循环声明一个新的变量:

 为(INT我= 0;我小于5; ++ I)
{
INT localCopy = I;
methods.Add(委托(obj对象){返回DummyMethod(localCopy);});
}

这是一个很常见的疑难杂症 - 你可以阅读更多的关于捕获的变量和封锁在我关闭文章


In the following program, DummyMethod always print 5. But if we use the commented code instead, we get different values (i.e. 1, 2, 3, 4). Can anybody please explain why this is happenning?

        delegate int Methodx(object obj);

        static int DummyMethod(int i)
        {
            Console.WriteLine("In DummyMethod method i = " + i);
            return i + 10;
        }

        static void Main(string[] args)
        {    
            List<Methodx> methods = new List<Methodx>();

            for (int i = 0; i < 5; ++i)
            {
                methods.Add(delegate(object obj) { return DummyMethod(i); });
            }

            //methods.Add(delegate(object obj) { return DummyMethod(1); });
            //methods.Add(delegate(object obj) { return DummyMethod(2); });
            //methods.Add(delegate(object obj) { return DummyMethod(3); });
            //methods.Add(delegate(object obj) { return DummyMethod(4); });

            foreach (var method in methods)
            {
                int c = method(null);
                Console.WriteLine("In main method c = " + c);
            }
        }

Also if the following code is used, I get the desired result.

        for (int i = 0; i < 5; ++i)
        {
            int j = i;
            methods.Add(delegate(object obj) { return DummyMethod(j); });
        }

解决方案

The problem is that you're capturing the same variable i in every delegate - which by the end of the loop just has the value 5.

Instead, you want each delegate to capture a different variable, which means declaring a new variable in the loop:

for (int i = 0; i < 5; ++i)
{
    int localCopy = i;
    methods.Add(delegate(object obj) { return DummyMethod(localCopy); });
}

This is a pretty common "gotcha" - you can read a bit more about captured variables and closures in my closures article.

这篇关于问题在C#中的代表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆