1

I'm creating a program where I need to change the value of an integer that is stored within an array. Here is an instance of what I am talking about:

int num = 0;
int[] nums = new int[] {num};

Console.WriteLine(nums[0]);

nums[0] = 1;

Console.WriteLine(nums[0]);
Console.WriteLine(num);

At the moment, the program outputs 0, 1, 0. Is there a way to modify the value of num by only using the array so that it would output 0, 1, 1?

ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
  • I guess you could do `num = 1 - num; Console.WriteLine(num);` three times after you set `nums[0] = 1;`, but I've no idea what you're really asking. – ProgrammingLlama Jul 18 '18 at 02:19
  • I'm asking whether or not you can change the value of a variable without using the variable's name and using the array instead. – Logan South Jul 18 '18 at 02:20
  • You can't modify the value of one variable using another (even an array variable) without writing code to do so. Computers do exactly what you tell them to, no more, no less. You would have to reassign `num` to change its value. – ProgrammingLlama Jul 18 '18 at 02:21
  • Possible duplicate of [What is the difference between a reference type and value type in c#?](https://stackoverflow.com/questions/5057267/what-is-the-difference-between-a-reference-type-and-value-type-in-c) – Mick Jul 18 '18 at 03:01

2 Answers2

4

Your mistake seems to be in thinking there's a reference between the variables num and nums.

int[] nums = new int[] {num} 

simply takes the value 0 and assigns it to the first element of the array. and changing either of them later won't the change the other. int is a value type and that is why this is not possible. You should read up on value and reference types.

You could however try something like this, since, like I said, you need 2 references to the same thing in order to change one thing and have the other one changed.

class IntContainer { public int val;}            //class with int field 
IntContainer num = new IntContainer {val = 1};   //original set to 1
IntContainer[] nums = new[] {num};
nums[0].val = 200;                               //setting the array element
Console.WriteLine(num.val);                      //200 not 1 

This works because it changes the value pointed to by the underlying reference.

Ash
  • 5,786
  • 5
  • 22
  • 42
2

You're either getting mixed with the names of your variables or you don't understand the distinction between reference and value types in C#.

The num variable in your example is a value type. Whilst int[] nums is a reference type.

When you assign a value type you are copying a value from one variable to another. When you assign a reference type you are assigning a reference to the object from variable to another. In that instance when two variables are pointing to a reference of the same object and you change within that object, you will see that change in both.

Here's another example to illustrate the point....

int valueA = 1;       // Copying value 1 to valueA
int valueB = valueA;  // Copying valueA to valueB

int[] arrayReferenceA = new[] { valueA };  // Creates a reference of an array of value types
int[] arrayReferenceB = arrayReferenceA;   // Copies a reference to the array

valueB = 2;   // Copying value 2 to valueB

arrayReferenceB[0] = valueB; // Copying valueB to the first element of an array of values referenced by arrayReferenceB
Console.WriteLine(valueA);
Console.WriteLine(valueB);
Console.WriteLine(arrayReferenceA[0]);
Console.WriteLine(arrayReferenceB[0]);

the output of which is...

1 2 2 2

Mick
  • 6,527
  • 4
  • 52
  • 67