无法为委托分配参数类型不太具体的匿名方法
public class Program
{
delegate void Srini(string param);
static void Main(string[] args)
{
Srini sr = new Srini(PrintHello1);
sr += new Srini(PrintHello2); //case 2:
sr += new Srini(delegate(string o) { Console.WriteLine(o); });
sr += new Srini(delegate(object o) { Console.WriteLine(o.ToString()); }); //case 4:
sr += new Srini(delegate { Console.WriteLine(“This line is accepted,though the method signature is not Comp”); });//case 5
sr("Hello World");
Console.Read();
}
static void PrintHello1(string param)
{
Console.WriteLine(param);
}
static void PrintHello2(object param)
{
Console.WriteLine(param);
}
}
编译器不会抱怨情况 2(请参阅注释),原因很简单,因为 string 继承自 object。同样,为什么它会抱怨匿名方法类型(请参阅注释 //case 4:)
无法将匿名方法转换为委托类型“DelegateTest.Program.Srini”,因为参数类型与委托参数类型不匹配
而在正常方法的情况下则不匹配?或者我正在将苹果与橙子进行比较? 另一种情况是为什么它接受不带参数的匿名方法?
public class Program
{
delegate void Srini(string param);
static void Main(string[] args)
{
Srini sr = new Srini(PrintHello1);
sr += new Srini(PrintHello2); //case 2:
sr += new Srini(delegate(string o) { Console.WriteLine(o); });
sr += new Srini(delegate(object o) { Console.WriteLine(o.ToString()); }); //case 4:
sr += new Srini(delegate { Console.WriteLine(“This line is accepted,though the method signature is not Comp”); });//case 5
sr("Hello World");
Console.Read();
}
static void PrintHello1(string param)
{
Console.WriteLine(param);
}
static void PrintHello2(object param)
{
Console.WriteLine(param);
}
}
Compiler doesn't complain about the case 2 (see the comment), well, the reason is straight forward since string inherits from object. Along the same lines, why is it complaining for anonymous method types (see the comment //case 4:) that
Cannot convert anonymous method to delegate type 'DelegateTest.Program.Srini' because the parameter types do not match the delegate parameter types
where as in case of normal method it doesn't? Or am I comparing apples with oranges?
Another case is why is it accepting anonymous method without parameters?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
方法组转换支持方差(从 C# 2 开始 - 在 C# 1 中不支持),而匿名函数转换则不支持。鉴于每当您编写匿名函数时,您都可以编写适当的参数,为什么不这样做呢?据我所知,允许存在差异没有任何好处,而且会使规则更难正确制定。 (方差最终在规范中变得相当复杂。)
编辑:没有参数列表的匿名方法基本上与任何委托的参数列表兼容,只要它没有
out
参数。基本上,这是一种说法,“我不关心参数。”这是 lambda 表达式所没有的匿名方法的一个特性:)Method group conversions support variance (as of C# 2 - they didn't in C# 1), anonymous function conversions simply don't. Given that whenever you write an anonymous function you can write the appropriate parameter, why not just do so? There would be no benefit in allowing variance there as far as I can see, and it would make the rules harder to get right. (Variance ends up being pretty complex in the spec.)
EDIT: An anonymous method without a parameter list is basically compatible with any delegate's parameter list so long as it doesn't have
out
parameters. Basically it's a way of saying, "I don't care about the parameters." It's the one feature of anonymous methods that lambda expressions don't have :)