The output of the following code surprised me. I think "a" should hold a reference to the newly created object. Can someone explain why the result is not 2?
class Program
{
static void Main(string[] args)
{
aclass a = new aclass();
Process(a);
Console.WriteLine(a.number);
Console.ReadLine();
}
static void Process(aclass a)
{
aclass temp = new aclass();
temp.number++;
//Console.WriteLine(temp.number);
a = temp;
a.number++;
//Console.WriteLine(a.number);
}
}
class aclass
{
public int number = 0;
}
Edit: This is an interview question. I just realized I had misunderstood the concept for long time. The argument a is different to the original a although they reference to the same address.