Reputation: 275
Suppose I have this code:
struct Normal
{
public float x;
public float y;
}
class NormalContainer
{
public Normal[] Normals
{
get; set;
}
}
class Main
{
void Run( NormalContainer container )
{
Normal[] normals = container.Normals // 1 - see below
normals[5].x = 4; // 3 - see below
container.Normals = normals; // 2 - see below
}
}
Does (1) create a copy of the array or is this a reference to the memory occupied by the array? What about (2) ?
Thanks
Upvotes: 2
Views: 773
Reputation: 24400
(1) copies the array's reference
(2) same
Array variables are reference types, regardless of their underlying element type, so whenever you assign an array variable to another, you are just copying the reference.
Upvotes: 1
Reputation: 160882
Array
is a reference type, so you are just copying the reference to the array instance.
Upvotes: 3
Reputation: 754705
An array in C# is a reference type. Items like assignment create copies of the reference vs. the value. At the end of (1) you end up with a local reference to the array stored in container
Note: In C# it's more proper to say "reference to the object" vs. "reference to the memory"
Upvotes: 2