本文介绍了在循环中匿名C#委托的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好我试图写匿名委托。因为整型变量委托之间共享我需要它是每一个代表的本地实例,这样RS [0]总是得到网卡[0],RS [1]总是得到网卡[1]等等......如何。我将做到这一点。

Hi all i am trying to write and anonymous delegate. as the integer variable is shared among the delegate i need it to be the local instance of every delegate such that rs[0] always gets nics[0], rs[1] always gets nics[1] and so on... how will i achieve this.

for (int i = 0; i < nics.Count; i++)
   {
         rs[i] = new RollingSeries(monitor, new RollingSeries.NextValueDelegate(delegate()
         {
            return GetNetworkUtilization(nics[i]);
          }));
    }



·阿卜杜勒·哈利克

Abdul khaliq

推荐答案

请我:

   for (int i = 0; i < nics.Count; i++)
   {
         int j = i;
         rs[i] = new RollingSeries(monitor, new RollingSeries.NextValueDelegate(delegate()
         {
            return GetNetworkUtilization(nics[j]);
          }));
    }

The Beauty of Closures

这篇关于在循环中匿名C#委托的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!