I don't understand this:- Why is the initial value & order (in case you want to change) of the array also changes when I am passing it through a class method.
Here's my code
Main method
class Program
{
static void Main(string[] args)
{
int[] val = new int[2];
val[0] = 12;
val[1] = 32;
int[] res;
HelperClass obj = new HelperClass();
obj.MyProperty = val;
Console.WriteLine("{0} {1}",val[0],val[1]);
obj.ValueChange();
res = obj.MyProperty;
Console.WriteLine("{0} {1}", val[0], val[1]);
Console.WriteLine("{0} {1}", res[0], res[1]);
}
}
helper class
public class HelperClass
{
private int[] myVar;
public int[] MyProperty
{
get { return myVar; }
set { myVar = value; }
}
public void ValueChange()
{
MyProperty[0] = MyProperty[0] + 100;
MyProperty[1] = MyProperty[1] + 100;
}
}
Expected Output
12 32
12 32
112 132
Getting Output
12 32
112 132
112 132
What do i need to do to make the initial array constant?
Also this does not happen if the values are some other type such as int, double etc..