Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Anonymous c# delegate within a loop

Tags:

c#

delegates

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

like image 244
Abdul Khaliq Avatar asked Nov 18 '09 16:11

Abdul Khaliq


People also ask

What is anonymous structure in C?

A Microsoft C extension allows you to declare a structure variable within another structure without giving it a name. These nested structures are called anonymous structures.

What is the difference between a union and an anonymous union?

An anonymous union is a union without a name. It cannot be followed by a declarator. An anonymous union is not a type; it defines an unnamed object. The member names of an anonymous union must be distinct from other names within the scope in which the union is declared.

Can you define a structure without a name?

Anonymous unions/structures are also known as unnamed unions/structures as they don't have names. Since there is no names, direct objects(or variables) of them are not created and we use them in nested structure or unions. Definition is just like that of a normal union just without a name or tag.

What is union in C with example?

Like Structures, union is a user defined data type. In union, all members share the same memory location. For example in the following C program, both x and y share the same location. If we change x, we can see the changes being reflected in y. #include <stdio.h>

What is an anonymous type in C++?

An anonymous type is a temporary data type that is inferred based on the data that you include in an object initializer. Properties of anonymous types will be read-only properties so you cannot change their values. Anonymous types have intellisense support in Visual Studio: Intellisense support for anonymous type.

What is the anonymous union and structures in C?

Here we will see what is the anonymous union and structures in C. The anonymous unions and structures are unnamed unions and structures. As they have no names, so we cannot create direct objects of it. We use it as nested structures or unions. These are the examples of anonymous union and structures.

What is the purpose of an anonymous type?

The declaration initializes a new type that uses only two properties from Product. Using anonymous types causes a smaller amount of data to be returned in the query. If you do not specify member names in the anonymous type, the compiler gives the anonymous type members the same name as the property being used to initialize them.

How to create an anonymous type in Python?

It cannot contain other members, such as fields, methods, events, etc. You create an anonymous type using the new operator with an object initializer syntax. The implicitly typed variable- var is used to hold the reference of anonymous types.


2 Answers

Make a local copy of i:

   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

like image 92
bruno conde Avatar answered Oct 18 '22 05:10

bruno conde


Use a local to get a different value per iteration

for (int i = 0; i < nics.Count; i++)
   {
         int localI = i;
         rs[i] = new RollingSeries(monitor, new RollingSeries.NextValueDelegate(delegate()
         {
            return GetNetworkUtilization(nics[localI]);
          }));
    }
like image 45
JaredPar Avatar answered Oct 18 '22 05:10

JaredPar