这里,Person
是一个类,因此是一个引用类型,当我将它作为参数传递给另一个函数并在那里分配它时,它应该被更改。
为什么program.person
调用后这里是 null ChangePerson
?
namespace IndirectlyPropertySet
{
class Program
{
public Person _person;
public Person person
{
get { return _person; }
set
{
if (_person != value)
{
_person = value;
}
}
}
static void Main(string[] args)
{
Program program = new Program();
ChangePerson(program.person);
// Why program.person is null after executing this ?
program.person = new Person() { Name = "b", Age = 2 };
}
static void ChangePerson(Person p)
{
Person pe = new Person() { Name = "a", Age = 1 };
p = pe;
}
}
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
}