-1

Let's say I have 5 int's which are not an array! and I want to change them all in 1 loop. Is there any way to make an array which will get the values of those 5 numbers and could change them directly?

Something like:

int a = 10;
int b = 20;

int[] rihanna = // .... ? 

for(int a=0; a<rihanna.length;a++)
    rihanna[a]++;

Console.WriteLine("{0} {1} ",a,b) // what I want to see here is 11 and 21
Jonathon Reinhart
  • 132,704
  • 33
  • 254
  • 328
Superzarzar
  • 509
  • 2
  • 8
  • 16
  • 3
    You give actual _names_ to your variables? That's sweet! In any case, your question is not clear, you should rephrase it. – Guillaume Mar 15 '13 at 04:12
  • How are you getting your integers ? – Mudassir Hasan Mar 15 '13 at 04:13
  • 3
    There is nothing, and I do mean **nothing**, (ok, *almost* nothing) that makes the collective blood boil more than "plz give me the code." – Anthony Pegram Mar 15 '13 at 04:13
  • 5
    You declare a new instance of an array and you add the numbers to the array yourself. Or you can make a ChrisBrown class helper to do it lol! – failedprogramming Mar 15 '13 at 04:16
  • 3
    Don't forget to run GC on mileyCyrus – djv Mar 15 '13 at 04:23
  • 1
    Upvoted because it's been a good mental exercise trying to make this work better or even as good as @AlexeiLevenkov's answer - maybe not real life code but sometimes you learn just playing around with these silly things... and because of a variable named rihanna! – Dmitriy Khaykin Mar 15 '13 at 06:04

4 Answers4

4

In general, the answer here is no. This would require C# to allow for having a reference to an integer (or other value-type), which it does not (and for good reason.)

The best solution I can come up with for you is to use a function with a ref parameter. Since you're trying to operate on a bunch of arbitrary integer variables anyway, you are still going to have to list them all out. How about do that with a function call, like this:

void Increase(ref int x) {
    x++;
}

int a = 10;
int b = 20;

Increase(ref a);
Increase(ref b);
Community
  • 1
  • 1
Jonathon Reinhart
  • 132,704
  • 33
  • 254
  • 328
4

I want to preface this with the following statement. Although this does what you want.. it's quite ugly. This is more in line with what you were assuming you could "just do" with C#.. when in fact it's much harder.

unsafe {
    int* a = stackalloc int[1];
    int* b = stackalloc int[1];

    *a = 10;
    *b = 20;

    int*[] rihanna = { a, b };

    for (int i = 0; i < rihanna.Length; i++) {
        (*rihanna[i])++;
    }

    Console.WriteLine("{0} {1} ", *a, *b); // "11 21"
}
Simon Whitehead
  • 63,300
  • 9
  • 114
  • 138
  • "get the values of those 5 numbers and could change them directly" - I take that to mean he wants the array just to refer to the original variables and affect them in-place. – Jonathon Reinhart Mar 15 '13 at 04:19
  • 1
    I removed my downvote. What you have looks correct to me, but suggesting this to the OP is, well, terrifying. – Jonathon Reinhart Mar 15 '13 at 04:28
  • 1
    @JonathonReinhart I agree.. but I'll leave that up to the OP (P.S, just tested this.. and it does output "11 21"). The idea was to show that it is possible to do it the way the OP may have wanted.. but it is definitely ugly. – Simon Whitehead Mar 15 '13 at 04:29
4

I think there is a way to solve it if you relax requirement - have array of entities that would allow modifying set of local int variables via operations on array.

To do so it is possible to capture references to the variables in an array of delegates that each take ref int val.

void Increase(ref int x) 
{
    x++;
}
void Set(ref int x, int amount) 
{
    x = amount;
}

void Sample()
{
  int a = 10;
  int b = 20;

  // Array of "increase variable" delegates
  var increaseElements = new Action[] { 
    () =>  Increase(ref a), 
    () =>  Increase(ref b) 
  };
  increaseElements[0](); // call delegate, unfortunately () instead of ++ 
  Console.WriteLine(a); // writes 11

  // And now with array of "set the variable" delegates:
  var setElements = new Action<int>[] { 
    v =>  Set(ref a,v), 
    v =>  Set(ref b,v) 
  };
  setElements[0](3); 
  Console.WriteLine(a); // writes 3 
}

Notes

  • directly using delegates you have to call them with ().
  • it may be possible to fix () instead of ++ issue by wrapping delegate into an object that will call Increase as its ++ implementation....
  • Issue with Set version where one need to call (3) instead of = 3 will require more trickery - implementing custom class with indexing to redirect set [index] to call saved setter function.

Warning: This is really done for entertainment purposes - please don't try it in real code.

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
2

You can't actually do this in .NET. ints are value types, so you can't make a reference to them directly.

There are a number of ways around this, however. Here's just one:

class IntReference
{
    int Value { get; set; }
}

IntReference a = new IntReference() { Value = 10 };
IntReference b = new IntReference() { Value = 20 };

IntReference[] rihanna = { a, b };

for (int i = 0; i < rihanna.Length; i++)
    rihanna[i].Value = rihanna[i].Value + 1;

Console.WriteLine("{0} {1} ", a.Value, b.Value); // "11 21"

Although you really shouldn't have to do this, ever. It goes counter to the design of value types in .NET.

p.s.w.g
  • 146,324
  • 30
  • 291
  • 331