有人可以解释一下为什么这段代码不起作用吗?
void Main()
{
var person = new Person
{
Name = "Name",
Age = 26
};
Employee employee = new Employee();
Mapper(person, employee);
//In this point the employee still have the default value on its properties
Console.WriteLine(employee.Name); //This will throw a nullReferenceException
}
static void Mapper(Person source, Employee destination)
{
destination = new Employee
{
Name = source.Name,
Age = source.Age
};
}
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
public class Employee
{
public string Name { get; set; }
public int Age { get; set; }
}
问题,在“映射”方法之后,分配给该雇员的新引用丢失了,但是为什么呢?
如果您对这个问题有一个更好的名字,或者在回答该问题之前还有其他名字,请告诉我,我会更改,将其删除。