public class HeapPerson
{
public int ID;
public string Name;
}
public struct StackPerson
{
public int ID;
public string Name;
}
class Class1
{
[STAThread]
static void Main(string[] args)
{
StackPerson person1 = new StackPerson();
person1.ID = 1;
person1.Name = "joey";
ChangeDetail(person1);
HeapPerson person2 = new HeapPerson();
person2.ID = 2;
person2.Name = "calisay";
ChangeDetail(person2);
}
public static void ChangeDetail(StackPerson person)
{
person = new StackPerson();
person.ID = 0;
person.Name = "null";
}
public static void ChangeDetail(HeapPerson person)
{
person = new HeapPerson();
person.ID = 0;
person.Name = "null";
}
}
We all know that for value types (including structs), a copy is passed to ChangeDetail method which is the reason why the contents of person1 did not change after the call to ChangeDetail. How come for reference type (HeapPerson), the contents are also not changed after a call to ChangeDetail?