我有以下问题:我想制作一个 Lambda,它应该打印 Person 类的结果。打印内容应如下所示:
ResetName, after (Name ==null) = True
但我在第 4 页上收到错误。现在打印出来的是:NAAM PERSOON VOOR = ALFREDNAAM PERSOON NA =
Person per = new Person();
per.Name = "ALFRED";
output.AppendFormat("NAAM PERSOON VOOR = " + per.Name);
Action<Person> resetName = (Person p) => { p.Name = null; };
resetName(per);
output.AppendFormat("NAAM PERSOON NA = " + per.Name);
Person p1 = new Person();
Person p2 = new Person();
p1.Name = "KAREL";
p2.Name = "FREEK";
Methods.ResetName(p1);
resetName(p2);
if (p1.Name != p2.Name) { passed = false; Console.WriteLine("ERROR IN resetperson"); }
return output.ToString();
如果您能提供一个好的、最小、完整代码示例以及更清晰的问题陈述,将会有所帮助。特别是:
Methods.ResetName()
的实现,因为您的 resetName
委托显然应该具有与该方法相同的效果。ResetName, after (Name ==null) = True
- 对我来说看起来不太像真实的输出,但相反看起来更像是对代码的效果)也就是说,您真正想要的似乎是这样的:
Action<Person> resetName = (Person p) =>
{
p.Name = null;
output.AppendFormat("NAAM PERSOON NA = " + p.Name);
};
resetName(per);