我想看看我是否能用时装写出C#代码, 但我有出乎意料的行为。我知道C#没有被打破, 但我不明白什么是错的,为什么它不做我认为应该做的事。
< 强度 > Code 强度 > :
class Program
{
static void Main(string[] args)
{
var person = CreatePerson(new{
FirstName = "Lucas",
LastName = "Ontivero",
Age = 34
});
Console.WriteLine("Name....... : {0}", person.FullName);
Console.WriteLine("IsJuvenile. : {0}", person.IsJuvenile);
person.SetFirstName("Pablo");
Console.WriteLine("Name....... : {0}", person.FullName);
Console.ReadKey();
}
static private dynamic CreatePerson(dynamic cfg)
{
string firstName = cfg.FirstName;
string lastName = cfg.LastName;
var age = cfg.Age;
return new{
FullName = string.Format("{0} {1}", firstName, lastName),
FirstName = firstName,
LastName = lastName,
SetFirstName = new Action<string>(fn => firstName= fn),
SetLastName = new Action<string>(ln => lastName = ln),
IsJuvenile = age < 18
};
}
}
<强度 > 输出 强度 > :
Name....... : Lucas Ontivero
IsJuvenile. : False
Name....... : Lucas Ontivero
" 强" 但我期望: "/强":
Name....... : Lucas Ontivero
IsJuvenile. : False
Name....... : Pablo Ontivero
为什么第一个名字没有改变?