3

I suspect there's something really fundamental behind this that I'm completely oblivious to. I can write

int b = 5;
int a = b;
a = 2; 

From the best I can tell, this gives me two separate variables. Initially, a is set to 5, but then I can change a to 2 without changing b

However, I can then write

double[] b = { 1, 2, 3, 4};
double[] a = b; 
a[2] = 9; 

Now, it appears that instead of having 2 separate variables, I have 2 references to the same entity. Changing a[2] now changes b[2]. What's going on?

user2864293
  • 380
  • 2
  • 10
  • 9
    "value type" vs "reference type" Jon Skeet has an explanation: http://yoda.arachsys.com/csharp/references.html – Ben Voigt Oct 09 '13 at 19:03
  • 2
    [What is the difference between a reference type and value type in c#?](http://stackoverflow.com/questions/5057267/what-is-the-difference-between-a-reference-type-and-value-type-in-c) – valverij Oct 09 '13 at 19:03
  • 1
    @user2864293 Welcome to Stack overflow and the cool kids table – DotNetRussell Oct 09 '13 at 19:05
  • but kids none the less – Jonesopolis Oct 09 '13 at 19:05
  • See link: http://www.albahari.com/valuevsreftypes.aspx – Bit Oct 09 '13 at 19:06
  • Please also see [`Array.Clone`](http://msdn.microsoft.com/en-us/library/system.array.clone.aspx) to resolve your issue – tnw Oct 09 '13 at 19:07
  • Value types vs reference types might be confusing, but the issue here isn't about that. If OP only used reference types, the question would still be valid. This question is even more fundamental - it's about variables vs. values/objects. At least considering the line **a = 2;** – lightbricko Oct 09 '13 at 19:22
  • Good lord that was fast. I did not expect such quick responses! I'll follow up with these links. Thanks all! – user2864293 Oct 09 '13 at 19:22

5 Answers5

6

There are two types of variable in C#. The first is called a "Value" type. When you assign it a value, the value is copied to that location, so when you write

int b = a;

You are copying the value of a to b.

However, there is also a "Reference" type. This only copies over a reference to the variable - in other words, it gets a sort of handle to the variable, so that when changes are made to that variable, they get reflected in both places.

Value types include structs and all primitives - ints, doubles, chars, etc. except strings. Reference types are everything else.

Some commenters have already provided good links, so I won't add any here.

Steve Westbrook
  • 1,698
  • 2
  • 20
  • 21
2

In C# Arrays are Reference Types. See this

Though I assumed this would end up being a duplicate question, I do apologize for not expounding further on the link.

In C# you have Value Types and Reference Types. All Arrays (even if they are arrays of Value types) are Reference Types.

A Value Type behaves as in your first example and the memory necessary to store its value is allocated on the Stack which lives and dies with the scope of the containing object.

A Reference type stores only a pointer to a location on the Heap where memory is allocated and it lives until the Garbage Collector determines nothing is using it anymore and it can be cleaned up. So if you assign a reference type to another reference type, they are the same object with different names.

This is of course horribly oversimplified, but you can get more information at the link in my answer or in any of the wonderful SO Questions addressing this same topic.

randcd
  • 2,263
  • 1
  • 19
  • 21
  • 1
    While correct, please try to include a brief explanation/summary as well rather than simply linking to an article – valverij Oct 09 '13 at 19:07
2

There are two kinds of types in .NET, value types and reference types. Int is a value type, double[] is an array, which is a reference type.

Your first a = b assigns b (which just holds the value 5) to a. In your second example b holds a reference to an array in memory, and the a = b simply points a to the same array.

Chris
  • 5,442
  • 17
  • 30
1

In your first example, int is a value type and each gets assigned a memory location. In your second example, an array is a complex data type, or reference type, so a gets allocated as a pointer.

1

Standard array behavior.

if you use the simple = operator, a reference will get passed and you then manipulate the original object.

to prevent this behaviour use built-in function .CopyTo() or Array.Clone()

a.CopyTo(b, 0)
b = a.Clone();

when you now change b there will be no changes to a.

Caution:

use this function only after making sure b can contain all elements of a (b.Length >= a.Length), else a index out of range exception will get thrown

Vogel612
  • 5,620
  • 5
  • 48
  • 73