简体   繁体   中英

How to iterate through variables by reference c#

In one class I have a bunch of class variables that hold the position of game pieces. After a move I need to iterate through those variables and change each of them. The problem I ran into was that by simply making a list and adding these variables, their values get passed into the list and changing the list elements does not change the variables.

int _var1;
int _var2;
int _var3;

List<int> varList = new List<int>() { _var1, _var2, _var3 };
for (int i = 0; i < varList.Count; i++) 
{
    varList[i]++;
}

Something like this does not work.

I feel like I'm missing some really simple oop concepts here but I can't think of how to iterate through all these variables without doing something more complicated than feels necessary for just one method.

Value types are copied when you add them to List. List allocates different memory.

Even though it's better not to have side effects, function should return Tuple of values and you set them explicitly.

If you want to pass them by reference you need to wrap it in object:

    public class ValueStorage
    {
        public int Value { get; set; }
    }
    class Program
    {
        static void Main(string[] args)
        {
            var _var1 = new ValueStorage() { Value = 0 };
            var _var2 = new ValueStorage() { Value = 1 };
            var _var3 = new ValueStorage() { Value = 2 };

            var varList = new List<ValueStorage> { _var1, _var2, _var3 };
            for (int i = 0; i < varList.Count; i++)
            {
                varList[i].Value++;
            }
        }
    }

you can make a class and put your Var1,Var2 and Var3 in it and initialize and object and call them in a loop.

something like this.

List<MyClass> obj_list = get_the_list();

foreach( MyClass obj in obj_list ){ obj.property = 42;}

This is just how value types work. Boxing wont help either

From the specs 1.3 Types and Variables

When a value of a value type is converted to type object, an object instance, also called a “box,” is allocated to hold the value, and the value is copied into that box. Conversely, when an object reference is cast to a value type, a check is made that the referenced object is a box of the correct value type, and, if the check succeeds, the value in the box is copied out.

  • You will need to store them in a reference type
  • or another trivial way is to use an array of pointers ( int* ). (This is assuming an array is suitable), this would allow functional transformations without the need to encase them in another type

Example

private int _var1 = 1;
private int _var2 = 2;
private int _var3 = 3;

...

var varList = new[] { &_var1, &_var2, &_var3 };

...

foreach (var value in varList)
   *value += 1;

Console.WriteLine(_var1);
Console.WriteLine(_var2);
Console.WriteLine(_var3);

Output

2
3
4

Additional Resources

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM