考虑以下代码:
public class MyClass
{
public delegate string PrintHelloType(string greeting);
public void Execute()
{
Type[] types = new Type[] { typeof(string), typeof(float), typeof(int)};
List<PrintHelloType> helloMethods = new List<PrintHelloType>();
foreach (var type in types)
{
var sayHello =
new PrintHelloType(greeting => SayGreetingToType(type, greeting));
helloMethods.Add(sayHello);
}
foreach (var helloMethod in helloMethods)
{
Console.WriteLine(helloMethod("Hi"));
}
}
public string SayGreetingToType(Type type, string greetingText)
{
return greetingText + " " + type.Name;
}
...
}
调用之后myClass.Execute(),代码将输出以下意外响应:
嗨Int32
嗨Int32
嗨Int32
很显然,我希望"Hi String","Hi Single","Hi Int32",但显然并非如此。为什么在所有3种方法中都使用了迭代数组的最后一个元素而不是适当的方法?
您将如何重写代码以实现期望的目标?
缥缈止盈
Qyouu
隔江千里
相关分类